Skip to content

Commit

Permalink
Update pgx to v4
Browse files Browse the repository at this point in the history
  • Loading branch information
michiomochi committed Jul 11, 2024
1 parent 2999bea commit 62ce854
Show file tree
Hide file tree
Showing 10 changed files with 519 additions and 366 deletions.
70 changes: 39 additions & 31 deletions enqueue_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package qg

import (
"context"
"testing"
"time"
)

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

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

j, err := findOneJob(c.pool)
j, err := findOneJob(ctx, c.pool)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -46,15 +48,16 @@ func TestEnqueueOnlyType(t *testing.T) {
}

func TestEnqueueWithPriority(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
ctx := context.Background()
c := openTestClient(ctx, t)
defer truncateAndClose(ctx, c)

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

j, err := findOneJob(c.pool)
j, err := findOneJob(ctx, c.pool)
if err != nil {
t.Fatal(err)
}
Expand All @@ -65,15 +68,16 @@ func TestEnqueueWithPriority(t *testing.T) {
}

func TestEnqueueWithRunAt(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
ctx := context.Background()
c := openTestClient(ctx, t)
defer truncateAndClose(ctx, c)

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

j, err := findOneJob(c.pool)
j, err := findOneJob(ctx, c.pool)
if err != nil {
t.Fatal(err)
}
Expand All @@ -86,15 +90,16 @@ func TestEnqueueWithRunAt(t *testing.T) {
}

func TestEnqueueWithArgs(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
ctx := context.Background()
c := openTestClient(ctx, t)
defer truncateAndClose(ctx, c)

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

j, err := findOneJob(c.pool)
j, err := findOneJob(ctx, c.pool)
if err != nil {
t.Fatal(err)
}
Expand All @@ -105,15 +110,16 @@ func TestEnqueueWithArgs(t *testing.T) {
}

func TestEnqueueWithQueue(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
ctx := context.Background()
c := openTestClient(ctx, t)
defer truncateAndClose(ctx, c)

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

j, err := findOneJob(c.pool)
j, err := findOneJob(ctx, c.pool)
if err != nil {
t.Fatal(err)
}
Expand All @@ -124,41 +130,43 @@ func TestEnqueueWithQueue(t *testing.T) {
}

func TestEnqueueWithEmptyType(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
ctx := context.Background()
c := openTestClient(ctx, t)
defer truncateAndClose(ctx, c)

if err := c.Enqueue(&Job{Type: ""}); err != ErrMissingType {
if err := c.Enqueue(ctx, &Job{Type: ""}); err != ErrMissingType {
t.Fatalf("want ErrMissingType, got %v", err)
}
}

func TestEnqueueInTx(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
ctx := context.Background()
c := openTestClient(ctx, t)
defer truncateAndClose(ctx, c)

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

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

j, err := findOneJob(tx)
j, err := findOneJob(ctx, tx)
if err != nil {
t.Fatal(err)
}
if j == nil {
t.Fatal("want job, got none")
}

if err = tx.Rollback(); err != nil {
if err = tx.Rollback(ctx); err != nil {
t.Fatal(err)
}

j, err = findOneJob(c.pool)
j, err = findOneJob(ctx, c.pool)
if err != nil {
t.Fatal(err)
}
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module github.com/kanmu/qg
go 1.16

require (
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/jackc/fake v0.0.0-20150926172116-812a484cc733 // indirect
github.com/jackc/pgx v3.0.1-0.20170728201109-511b90478f17+incompatible
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgx/v4 v4.18.3
github.com/lib/pq v1.10.3 // indirect
github.com/pkg/errors v0.8.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
gopkg.in/guregu/null.v3 v3.0.2-0.20160228005316-41961cea0328
)
Loading

0 comments on commit 62ce854

Please sign in to comment.