-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgq_test.go
160 lines (122 loc) · 4.16 KB
/
pgq_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package pgq_test
import (
"context"
"database/sql"
"fmt"
"os"
"testing"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
a "github.com/james-elicx/go-utils/assert"
"github.com/james-elicx/pgq"
)
var db *sql.DB
func TestMain(m *testing.M) {
var database *embeddedpostgres.EmbeddedPostgres
connUrl := os.Getenv("TEST_POSTGRES_URL")
if connUrl == "" {
cfg := embeddedpostgres.DefaultConfig()
connUrl = cfg.GetConnectionURL()
database = embeddedpostgres.NewDatabase(cfg)
if err := database.Start(); err != nil {
panic(err)
}
}
config, err := pgx.ParseConfig(connUrl)
if err != nil {
panic(err)
}
db = stdlib.OpenDB(*config)
code := m.Run()
if database != nil {
if err := database.Stop(); err != nil {
panic(err)
}
}
os.Exit(code)
}
func setupQueue(t *testing.T) (*pgq.Queue, context.Context) {
q := pgq.NewQueue(db)
ctx := context.Background()
if err := q.SetupDatabase(ctx); err != nil {
t.Fatal(err)
}
return q, ctx
}
func TestRegisterHandler(t *testing.T) {
q, _ := setupQueue(t)
handler := func(job pgq.Job) error {
return nil
}
// passes when no handler is registered for job type
err := q.RegisterHandler("test_type", handler)
a.Equals(t, err, nil)
// fails when handler is already registered
err = q.RegisterHandler("test_type", handler)
a.EqualsErrorMessage(t, err, "queue: handler already registered for job type test_type")
}
func TestPut(t *testing.T) {
q, ctx := setupQueue(t)
handler := func(job pgq.Job) error {
return nil
}
// fails when no handler is registered for job type
err := q.Put(ctx, "test_type", "test_data")
a.EqualsErrorMessage(t, err, "queue: no handler registered for job type test_type")
// register the handler for the job type
err = q.RegisterHandler("test_type", handler)
a.Equals(t, err, nil)
// passes when handler is registered for job type
err = q.Put(ctx, "test_type", "test_data")
}
func TestPop(t *testing.T) {
q, ctx := setupQueue(t)
handler := func(job pgq.Job) error {
return nil
}
// fails when no job type is specified
err := q.Pop(ctx, []string{})
a.EqualsErrorMessage(t, err, "queue: no job type specified")
// fails when no handler is registered for job type
err = q.Pop(ctx, []string{"test_type"})
a.EqualsErrorMessage(t, err, "queue: no handler registered for job type test_type")
// register the handler for the job type
err = q.RegisterHandler("test_type", handler)
a.Equals(t, err, nil)
// pops entry successfully
err = q.Pop(ctx, []string{"test_type"})
a.Equals(t, err, nil)
// updates entry status to done
var job pgq.Job
row := db.QueryRowContext(ctx, fmt.Sprintf("SELECT id, job_type, data, status, error, attempt, created_at, started_at, finished_at FROM %s WHERE job_type = 'test_type' ORDER BY id ASC LIMIT 1", pgq.TableName))
row.Scan(&job.ID, &job.Type, &job.Data, &job.Status, &job.Error, &job.Attempt, &job.CreatedAt, &job.StartedAt, &job.FinishedAt)
a.Equals(t, job.Type, "test_type")
a.Equals(t, job.Data, "test_data")
a.Equals(t, job.Status, pgq.JobStatusDone)
a.Equals(t, job.Error.Valid, false)
}
func TestPopHandlerError(t *testing.T) {
q, ctx := setupQueue(t)
handler := func(job pgq.Job) error {
return fmt.Errorf("test error")
}
// register the handler for the job type
err := q.RegisterHandler("test_err_type", handler)
a.Equals(t, err, nil)
// adds some entries to the queue
err = q.Put(ctx, "test_err_type", "test_data")
a.Equals(t, err, nil)
// pops entry successfully
err = q.Pop(ctx, []string{"test_err_type"})
a.Equals(t, err, nil)
// updates entry status to error and adds error message
var job pgq.Job
row := db.QueryRowContext(ctx, fmt.Sprintf("SELECT id, job_type, data, status, error, attempt, created_at, started_at, finished_at FROM %s WHERE job_type = 'test_err_type' ORDER BY id ASC LIMIT 1", pgq.TableName))
row.Scan(&job.ID, &job.Type, &job.Data, &job.Status, &job.Error, &job.Attempt, &job.CreatedAt, &job.StartedAt, &job.FinishedAt)
a.Equals(t, job.Type, "test_err_type")
a.Equals(t, job.Data, "test_data")
a.Equals(t, job.Status, pgq.JobStatusError)
a.Equals(t, job.Error.Valid, true)
a.Equals(t, job.Error.String, "test error")
}