Skip to content

Commit

Permalink
Split test package
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Oct 3, 2024
1 parent fddafb2 commit e22b314
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 114 deletions.
32 changes: 17 additions & 15 deletions enqueue_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package qg
package qg_test

import (
"testing"
"time"

"github.com/kanmu/qg/v4"
)

func TestEnqueueOnlyType(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)

if err := c.Enqueue(&Job{Type: "MyJob"}); err != nil {
if err := c.Enqueue(&qg.Job{Type: "MyJob"}); err != nil {
t.Fatal(err)
}

j, err := findOneJob(c.pool)
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -50,11 +52,11 @@ func TestEnqueueWithPriority(t *testing.T) {
defer truncateAndClose(c)

want := int16(99)
if err := c.Enqueue(&Job{Type: "MyJob", Priority: want}); err != nil {
if err := c.Enqueue(&qg.Job{Type: "MyJob", Priority: want}); err != nil {
t.Fatal(err)
}

j, err := findOneJob(c.pool)
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
Expand All @@ -69,11 +71,11 @@ func TestEnqueueWithRunAt(t *testing.T) {
defer truncateAndClose(c)

want := time.Now().Add(2 * time.Minute)
if err := c.Enqueue(&Job{Type: "MyJob", RunAt: want}); err != nil {
if err := c.Enqueue(&qg.Job{Type: "MyJob", RunAt: want}); err != nil {
t.Fatal(err)
}

j, err := findOneJob(c.pool)
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
Expand All @@ -90,11 +92,11 @@ func TestEnqueueWithArgs(t *testing.T) {
defer truncateAndClose(c)

want := `{"arg1":0, "arg2":"a string"}`
if err := c.Enqueue(&Job{Type: "MyJob", Args: []byte(want)}); err != nil {
if err := c.Enqueue(&qg.Job{Type: "MyJob", Args: []byte(want)}); err != nil {
t.Fatal(err)
}

j, err := findOneJob(c.pool)
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
Expand All @@ -109,11 +111,11 @@ func TestEnqueueWithQueue(t *testing.T) {
defer truncateAndClose(c)

want := "special-work-queue"
if err := c.Enqueue(&Job{Type: "MyJob", Queue: want}); err != nil {
if err := c.Enqueue(&qg.Job{Type: "MyJob", Queue: want}); err != nil {
t.Fatal(err)
}

j, err := findOneJob(c.pool)
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
Expand All @@ -127,7 +129,7 @@ func TestEnqueueWithEmptyType(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)

if err := c.Enqueue(&Job{Type: ""}); err != ErrMissingType {
if err := c.Enqueue(&qg.Job{Type: ""}); err != qg.ErrMissingType {
t.Fatalf("want ErrMissingType, got %v", err)
}
}
Expand All @@ -136,13 +138,13 @@ func TestEnqueueInTx(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)

tx, err := c.pool.Begin()
tx, err := c.TestGetPool().Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback() //nolint:errcheck

if err = c.EnqueueInTx(&Job{Type: "MyJob"}, tx); err != nil {
if err = c.EnqueueInTx(&qg.Job{Type: "MyJob"}, tx); err != nil {
t.Fatal(err)
}

Expand All @@ -158,7 +160,7 @@ func TestEnqueueInTx(t *testing.T) {
t.Fatal(err)
}

j, err = findOneJob(c.pool)
j, err = findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 19 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package qg

import (
"database/sql"
)

var RawConn = rawConn

func (c *Client) TestGetPool() *sql.DB {
return c.pool
}

func (j *Job) TestGetClient() *Client {
return j.c
}

func (w *Worker) TestGetDone() bool {
return w.done
}
21 changes: 11 additions & 10 deletions que_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package qg
package qg_test

import (
"database/sql"
Expand All @@ -7,6 +7,7 @@ import (
"time"

"github.com/jackc/pgx/v4"
"github.com/kanmu/qg/v4"
sqltracer "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql"
)

Expand All @@ -19,8 +20,8 @@ var testConnConfig = func() *pgx.ConnConfig {

const maxConn = 5

func openTestClientMaxConns(t testing.TB, maxConnections int, openDB func(driver.Connector) *sql.DB) *Client {
connector, err := GetConnector("localhost", 5432, "qgtest", "", "qgtest")
func openTestClientMaxConns(t testing.TB, maxConnections int, openDB func(driver.Connector) *sql.DB) *qg.Client {
connector, err := qg.GetConnector("localhost", 5432, "qgtest", "", "qgtest")
if err != nil {
t.Fatal(err)
}
Expand All @@ -32,38 +33,38 @@ func openTestClientMaxConns(t testing.TB, maxConnections int, openDB func(driver
db.SetMaxIdleConns(maxConnections)
// make lifetime sufficiently long
db.SetConnMaxLifetime(time.Duration(5 * time.Minute))
c, err := NewClient(db)
c, err := qg.NewClient(db)
if err != nil {
t.Fatal(err)
}
return c
}

func openTestClient(t testing.TB) *Client {
func openTestClient(t testing.TB) *qg.Client {
return openTestClientMaxConns(t, maxConn, sql.OpenDB)
}

func openTestClientWithTracer(t testing.TB) *Client {
func openTestClientWithTracer(t testing.TB) *qg.Client {
return openTestClientMaxConns(t, maxConn, func(c driver.Connector) *sql.DB {
return sqltracer.OpenDB(c)
})
}

func truncateAndClose(c *Client) {
pool := c.pool
func truncateAndClose(c *qg.Client) {
pool := c.TestGetPool()
c.Close()
if _, err := pool.Exec("TRUNCATE TABLE que_jobs"); err != nil {
panic(err)
}
pool.Close()
}

func findOneJob(q Queryer) (*Job, error) {
func findOneJob(q qg.Queryer) (*qg.Job, error) {
findSQL := `
SELECT priority, run_at, job_id, job_class, args, error_count, last_error, queue
FROM que_jobs LIMIT 1`

j := &Job{}
j := &qg.Job{}
err := q.QueryRow(findSQL).Scan(
&j.Priority,
&j.RunAt,
Expand Down
12 changes: 7 additions & 5 deletions stats_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package qg
package qg_test

import (
"testing"

"github.com/kanmu/qg/v4"
)

func TestStats(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)

if err := c.Enqueue(&Job{Queue: "Q1", Type: "MyJob"}); err != nil {
if err := c.Enqueue(&qg.Job{Queue: "Q1", Type: "MyJob"}); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -49,7 +51,7 @@ func TestStats(t *testing.T) {
t.Errorf("stats[0].OldestRunAt.IsZero() != false (got %v)", stats[0].OldestRunAt.IsZero())
}

if err := c.Enqueue(&Job{Queue: "Q1", Type: "MyJob"}); err != nil {
if err := c.Enqueue(&qg.Job{Queue: "Q1", Type: "MyJob"}); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -90,7 +92,7 @@ func TestStats(t *testing.T) {
t.Errorf("stats[0].OldestRunAt.IsZero() != false (got %v)", stats[0].OldestRunAt.IsZero())
}

if err := c.Enqueue(&Job{Queue: "Q2", Type: "MyJob"}); err != nil {
if err := c.Enqueue(&qg.Job{Queue: "Q2", Type: "MyJob"}); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -159,7 +161,7 @@ func TestStats(t *testing.T) {
t.Errorf("stats[1].OldestRunAt.IsZero() != false (got %v)", stats[1].OldestRunAt.IsZero())
}

if err := c.Enqueue(&Job{Queue: "Q1", Type: "AnotherJob"}); err != nil {
if err := c.Enqueue(&qg.Job{Queue: "Q1", Type: "AnotherJob"}); err != nil {
t.Fatal(err)
}

Expand Down
Loading

0 comments on commit e22b314

Please sign in to comment.