Skip to content

Commit

Permalink
Add TestWorkerWorkOneWithTracer
Browse files Browse the repository at this point in the history
  • Loading branch information
winebarrel committed Oct 3, 2024
1 parent 71ee50d commit 81d61e1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
14 changes: 11 additions & 3 deletions que_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package qg

import (
"database/sql"
"database/sql/driver"
"testing"
"time"

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

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

const maxConn = 5

func openTestClientMaxConns(t testing.TB, maxConnections int) *Client {
func openTestClientMaxConns(t testing.TB, maxConnections int, openDB func(driver.Connector) *sql.DB) *Client {
connector, err := GetConnector("localhost", 5432, "qgtest", "", "qgtest")
if err != nil {
t.Fatal(err)
}
db := sql.OpenDB(connector)
db := openDB(connector)
// using stdlib, it's difficult to open max conn from the beginning
// if we want to open connections till its limit, need to use go routine to
// concurrently open connections
Expand All @@ -38,7 +40,13 @@ func openTestClientMaxConns(t testing.TB, maxConnections int) *Client {
}

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

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

func truncateAndClose(c *Client) {
Expand Down
3 changes: 2 additions & 1 deletion work_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package qg

import (
"context"
"database/sql"
"fmt"
"sync"
"testing"
Expand Down Expand Up @@ -209,7 +210,7 @@ func TestJobConnRace(t *testing.T) {

// Test the race condition in LockJob
func TestLockJobAdvisoryRace(t *testing.T) {
c := openTestClientMaxConns(t, 4)
c := openTestClientMaxConns(t, 4, sql.OpenDB)
defer truncateAndClose(c)
ctx := context.Background()

Expand Down
31 changes: 31 additions & 0 deletions worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ func TestWorkerWorkOne(t *testing.T) {
}
}

func TestWorkerWorkOneWithTracer(t *testing.T) {
c := openTestClientWithTracer(t)
defer truncateAndClose(c)

success := false
wm := WorkMap{
"MyJob": func(j *Job) error {
success = true
return nil
},
}
w := NewWorker(c, wm)

didWork := w.WorkOne()
if didWork {
t.Errorf("want didWork=false when no job was queued")
}

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

didWork = w.WorkOne()
if !didWork {
t.Errorf("want didWork=true")
}
if !success {
t.Errorf("want success=true")
}
}

func TestWorkerShutdown(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
Expand Down

0 comments on commit 81d61e1

Please sign in to comment.