-
Notifications
You must be signed in to change notification settings - Fork 5
/
que_test.go
95 lines (86 loc) · 2.01 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package qg
import (
"database/sql"
"testing"
"time"
"github.com/jackc/pgx"
"github.com/jackc/pgx/stdlib"
)
var testConnConfig = pgx.ConnConfig{
Host: "localhost",
Database: "qgtest",
User: "qgtest",
// LogLevel: pgx.LogLevelDebug,
// Logger: log15.New("testlogger", "test/qg"),
}
const maxConn = 5
func openTestClientMaxConns(t testing.TB, maxConnections int) *Client {
// connPoolConfig := pgx.ConnPoolConfig{
// ConnConfig: testConnConfig,
// MaxConnections: maxConnections,
// AfterConnect: PrepareStatements,
// }
// pool, err := pgx.NewConnPool(connPoolConfig)
// if err != nil {
// t.Fatal(err)
// }
driverConfig := stdlib.DriverConfig{
ConnConfig: pgx.ConnConfig{
Host: "localhost",
Database: "qgtest",
User: "qgtest",
},
AfterConnect: PrepareStatements,
}
stdlib.RegisterDriverConfig(&driverConfig)
db, err := sql.Open("pgx", driverConfig.ConnectionString(""))
if err != nil {
t.Fatal(err)
}
// using stdlib, it's difficult to open max conn from the begining
// if we want to open connections till its limit, need to use go routine to
// concurrently open connections
db.SetMaxOpenConns(maxConnections)
db.SetMaxIdleConns(maxConnections)
// make lifetime sufficiently long
db.SetConnMaxLifetime(time.Duration(5 * time.Minute))
c, err := NewClient2(db)
if err != nil {
t.Fatal(err)
}
return c
}
func openTestClient(t testing.TB) *Client {
return openTestClientMaxConns(t, maxConn)
}
func truncateAndClose(c *Client) {
pool := c.pool
c.Close()
if _, err := pool.Exec("TRUNCATE TABLE que_jobs"); err != nil {
panic(err)
}
pool.Close()
}
func findOneJob(q Queryer) (*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(findSQL).Scan(
&j.Priority,
&j.RunAt,
&j.ID,
&j.Type,
&j.Args,
&j.ErrorCount,
&j.LastError,
&j.Queue,
)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return j, nil
}