forked from btubbs/pgq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
queries_test.go
218 lines (204 loc) · 5.12 KB
/
queries_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package pgq
import (
"errors"
"testing"
"time"
"github.com/guregu/null"
"github.com/jmoiron/sqlx"
"github.com/stretchr/testify/assert"
)
func TestEnqueueJob(t *testing.T) {
tt := []struct {
desc string
options []JobOption
makeAssertions func(*testing.T, *sqlx.Tx, int, error)
}{
{
desc: "simple success",
makeAssertions: func(t *testing.T, tx *sqlx.Tx, id int, err error) {
assert.Nil(t, err)
// grab job from DB and compare it.
job := &Job{}
err = tx.Get(job, "SELECT * from pgq_jobs WHERE id = $1", id)
assert.Equal(t, "someQueue", job.QueueName)
assert.Equal(t, []byte("some data"), job.Data)
assert.Equal(t, Durations{
time.Minute,
time.Minute * 10,
time.Minute * 30,
}, job.RetryWaits)
},
},
{
desc: "job options called",
options: []JobOption{func(job *Job) { panic("boom") }},
makeAssertions: func(t *testing.T, tx *sqlx.Tx, id int, err error) {
assert.Equal(t, "error/panic while applying option, cause: boom", err.Error())
},
},
}
db := getTestDB()
defer db.Close()
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
tx, _ := db.Beginx()
defer tx.Rollback()
id, err := enqueueJob(tx, "someQueue", []byte("some data"), tc.options...)
tc.makeAssertions(t, tx, id, err)
})
}
}
func TestGetNextJob(t *testing.T) {
tt := []struct {
desc string
enqueuedJobs []Job
getQueueNames []string
expectedData string
jobExpected bool
errExpected bool
}{
{
desc: "single, ready job in queue",
enqueuedJobs: []Job{
{
QueueName: "foo",
Data: []byte("bar"),
RunAfter: time.Now(),
RetryWaits: []time.Duration{},
},
},
getQueueNames: []string{"foo"},
jobExpected: true,
expectedData: "bar",
},
{
desc: "other queues excluded",
enqueuedJobs: []Job{
{
QueueName: "foo",
Data: []byte("bar"),
RunAfter: time.Now(),
RetryWaits: []time.Duration{},
},
},
getQueueNames: []string{"other foo"},
expectedData: "bar",
},
{
desc: "unready job in queue",
enqueuedJobs: []Job{
{
QueueName: "foo",
Data: []byte("bar"),
RunAfter: time.Now().Add(time.Minute * 1),
RetryWaits: []time.Duration{},
},
},
getQueueNames: []string{"foo"},
},
{
desc: "no jobs",
enqueuedJobs: []Job{},
getQueueNames: []string{"foo"},
},
{
desc: "old completed jobs in queue",
enqueuedJobs: []Job{
{
QueueName: "foo",
Data: []byte("bar"),
RunAfter: time.Now(),
RetryWaits: []time.Duration{},
RanAt: null.TimeFrom(time.Now().Add(time.Minute * -1)),
},
},
getQueueNames: []string{"foo"},
},
}
db := getTestDB()
defer db.Close()
for _, tc := range tt {
t.Run(tc.desc, func(t *testing.T) {
tx, _ := db.Beginx()
defer tx.Rollback()
// set up fixtures
for _, j := range tc.enqueuedJobs {
tx.NamedExec(`
INSERT into pgq_jobs (
queue_name, data, run_after, retry_waits, ran_at, error
) VALUES (
:queue_name, :data, :run_after, :retry_waits, :ran_at, :error
);
`, j)
}
job, err := getNextJob(tx, tc.getQueueNames)
if tc.errExpected {
assert.NotNil(t, err)
}
if tc.jobExpected {
assert.Nil(t, err)
assert.NotNil(t, job)
assert.Equal(t, tc.expectedData, string(job.Data))
}
})
}
}
func TestUpdateJob(t *testing.T) {
db := getTestDB()
defer db.Close()
tx, _ := db.Beginx()
defer tx.Rollback()
var insertedJobID int64
insertedJob := &Job{
QueueName: "foo",
Data: []byte("update bar"),
RunAfter: time.Now(),
}
err := tx.QueryRow(`
INSERT into pgq_jobs (
queue_name, data, run_after, retry_waits
) VALUES (
$1, $2, $3, $4
) RETURNING id;
`, insertedJob.QueueName, insertedJob.Data, insertedJob.RunAfter, insertedJob.RetryWaits).Scan(&insertedJobID)
assert.Nil(t, err)
insertedJob.ID = insertedJobID
ranAt := time.Now()
err = updateJob(tx, insertedJob, ranAt, errors.New("boom"))
assert.Nil(t, err)
fetchedJob := &Job{}
err = tx.Get(fetchedJob, `SELECT * from pgq_jobs;`)
assert.Nil(t, err)
assert.Equal(t, "update bar", string(fetchedJob.Data))
// we lose a tiny amount of precision round tripping a timestamp through the DB
assert.WithinDuration(t, null.TimeFrom(ranAt).Time, fetchedJob.RanAt.Time, time.Millisecond)
assert.Equal(t, "boom", fetchedJob.Error.String)
}
func TestDeleteJob(t *testing.T) {
db := getTestDB()
defer db.Close()
tx, _ := db.Beginx()
defer tx.Rollback()
var insertedJobID int64
insertedJob := &Job{
QueueName: "foo",
Data: []byte("update bar"),
RunAfter: time.Now(),
}
err := tx.QueryRow(`
INSERT into pgq_jobs (
queue_name, data, run_after, retry_waits
) VALUES (
$1, $2, $3, $4
) RETURNING id;
`, insertedJob.QueueName, insertedJob.Data, insertedJob.RunAfter, insertedJob.RetryWaits).Scan(&insertedJobID)
assert.Nil(t, err)
insertedJob.ID = insertedJobID
err = deleteJob(tx, insertedJob)
assert.Nil(t, err)
// should be no jobs in DB
var count int
err = tx.QueryRow("SELECT count(*) from pgq_jobs;").Scan(&count)
assert.Nil(t, err)
assert.Equal(t, 0, count)
}