-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathque_test.go
78 lines (69 loc) · 1.72 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
package qg_test
import (
"database/sql"
"database/sql/driver"
"testing"
"time"
"github.com/jackc/pgx/v5"
"github.com/kanmu/qg/v5"
)
var testConnConfig = func() *pgx.ConnConfig {
conn, _ := pgx.ParseConfig("postgres://qgtest@localhost:5432/qgtest")
// conn.LogLevel = pgx.LogLevelDebug
// conn.Logger = log15.New("testlogger", "test/qg")
return conn
}()
const maxConn = 5
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)
}
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
db.SetMaxOpenConns(maxConnections)
db.SetMaxIdleConns(maxConnections)
// make lifetime sufficiently long
db.SetConnMaxLifetime(time.Duration(5 * time.Minute))
c, err := qg.NewClient(db)
if err != nil {
t.Fatal(err)
}
return c
}
func openTestClient(t testing.TB) *qg.Client {
return openTestClientMaxConns(t, maxConn, sql.OpenDB)
}
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 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 := &qg.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
}