forked from bgentry/que-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
que_test.go
63 lines (54 loc) · 1.25 KB
/
que_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package que
import (
"context"
"os"
"testing"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
)
var testConnConfig, _ = pgx.ParseConfig(os.Getenv("PG_CONN_STRING"))
func openTestClientMaxConns(t testing.TB, maxConnections int32) *Client {
connPoolConfig, err := pgxpool.ParseConfig(testConnConfig.ConnString())
if err != nil {
t.Fatal(err)
}
connPoolConfig.MaxConns = maxConnections
connPoolConfig.AfterConnect = PrepareStatements
pool, err := pgxpool.ConnectConfig(context.Background(), connPoolConfig)
if err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(context.Background(), "TRUNCATE TABLE que_jobs"); err != nil {
panic(err)
}
return NewClient(pool)
}
func openTestClient(t testing.TB) *Client {
return openTestClientMaxConns(t, 5)
}
func closePool(pool *pgxpool.Pool) {
pool.Close()
}
func findOneJob(q queryable) (*Job, error) {
findSQL := `
SELECT priority, run_at, job_id, job_class, args, error_count, last_error, queue
FROM que_jobs LIMIT 1`
j := &Job{}
err := q.QueryRow(context.Background(), findSQL).Scan(
&j.Priority,
&j.RunAt,
&j.ID,
&j.Type,
&j.Args,
&j.ErrorCount,
&j.LastError,
&j.Queue,
)
if err == pgx.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return j, nil
}