-
Notifications
You must be signed in to change notification settings - Fork 4
/
enqueue_test.go
170 lines (140 loc) · 3.39 KB
/
enqueue_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
package qg_test
import (
"testing"
"time"
"github.com/kanmu/qg/v5"
)
func TestEnqueueOnlyType(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
if err := c.Enqueue(&qg.Job{Type: "MyJob"}); err != nil {
t.Fatal(err)
}
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
// check resulting job
if j.ID == 0 {
t.Errorf("want non-zero ID")
}
if want := ""; j.Queue != want {
t.Errorf("want Queue=%q, got %q", want, j.Queue)
}
if want := int16(100); j.Priority != want {
t.Errorf("want Priority=%d, got %d", want, j.Priority)
}
if j.RunAt.IsZero() {
t.Error("want non-zero RunAt")
}
if want := "MyJob"; j.Type != want {
t.Errorf("want Type=%q, got %q", want, j.Type)
}
if want, got := "[]", string(j.Args); got != want {
t.Errorf("want Args=%s, got %s", want, got)
}
if want := int32(0); j.ErrorCount != want {
t.Errorf("want ErrorCount=%d, got %d", want, j.ErrorCount)
}
if j.LastError.Valid {
t.Errorf("want no LastError, got %v", j.LastError)
}
}
func TestEnqueueWithPriority(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
want := int16(99)
if err := c.Enqueue(&qg.Job{Type: "MyJob", Priority: want}); err != nil {
t.Fatal(err)
}
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
if j.Priority != want {
t.Errorf("want Priority=%d, got %d", want, j.Priority)
}
}
func TestEnqueueWithRunAt(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
want := time.Now().Add(2 * time.Minute)
if err := c.Enqueue(&qg.Job{Type: "MyJob", RunAt: want}); err != nil {
t.Fatal(err)
}
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
// truncate to the microsecond as postgres driver does
want = want.Truncate(time.Microsecond)
if !want.Equal(j.RunAt) {
t.Errorf("want RunAt=%s, got %s", want, j.RunAt)
}
}
func TestEnqueueWithArgs(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
want := `{"arg1":0, "arg2":"a string"}`
if err := c.Enqueue(&qg.Job{Type: "MyJob", Args: []byte(want)}); err != nil {
t.Fatal(err)
}
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
if got := string(j.Args); got != want {
t.Errorf("want Args=%s, got %s", want, got)
}
}
func TestEnqueueWithQueue(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
want := "special-work-queue"
if err := c.Enqueue(&qg.Job{Type: "MyJob", Queue: want}); err != nil {
t.Fatal(err)
}
j, err := findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
if j.Queue != want {
t.Errorf("want Queue=%q, got %q", want, j.Queue)
}
}
func TestEnqueueWithEmptyType(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
if err := c.Enqueue(&qg.Job{Type: ""}); err != qg.ErrMissingType {
t.Fatalf("want ErrMissingType, got %v", err)
}
}
func TestEnqueueInTx(t *testing.T) {
c := openTestClient(t)
defer truncateAndClose(c)
tx, err := c.TestGetPool().Begin()
if err != nil {
t.Fatal(err)
}
defer tx.Rollback() //nolint:errcheck
if err = c.EnqueueInTx(&qg.Job{Type: "MyJob"}, tx); err != nil {
t.Fatal(err)
}
j, err := findOneJob(tx)
if err != nil {
t.Fatal(err)
}
if j == nil {
t.Fatal("want job, got none")
}
if err = tx.Rollback(); err != nil {
t.Fatal(err)
}
j, err = findOneJob(c.TestGetPool())
if err != nil {
t.Fatal(err)
}
if j != nil {
t.Fatalf("wanted job to be rolled back, got %+v", j)
}
}