forked from DuoSRX/gokiq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gokiq_test.go
157 lines (121 loc) · 3.59 KB
/
gokiq_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
package gokiq
import (
"encoding/json"
"fmt"
"math"
"testing"
"time"
"github.com/gomodule/redigo/redis"
)
var pool = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", ":6379")
},
}
func resetRedis(conn redis.Conn) {
conn.Do("FLUSHDB")
conn.Close()
}
var args []interface{} = make([]interface{}, 0)
var job = NewJob("HardWorker", "default", args, 0)
func TestEnqueue(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
job.Enqueue(pool)
expected := fmt.Sprintf(`{"jid":"%s","retry":0,"queue":"default","class":"HardWorker","args":[],"enqueued_at":%d}`,
job.JID,
job.EnqueuedAt)
actual, _ := json.Marshal(job)
if expected != string(actual) {
t.Errorf("Excepted JSON to be %s, got %s", expected, actual)
}
count, _ := redis.Int(conn.Do("SISMEMBER", "queues", job.Queue))
if count != 1 {
t.Error("Expected queues list to have the correct queue but didn't found it.")
}
count, _ = redis.Int(conn.Do("LLEN", "queue:default"))
if count != 1 {
t.Errorf("Expected the queue to have exactly one job but found %d", count)
}
}
func TestEnqueueAt(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
now := time.Now()
job.EnqueueAt(now, pool)
b, _ := json.Marshal(job)
score, _ := redis.Int64(conn.Do("ZSCORE", "schedule", b))
if score != now.Unix() {
t.Errorf("Expected the timestamp to be %d but got %d", now.Unix(), score)
}
}
func TestEnqueueIn(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
now := time.Now()
duration := time.Hour
job.EnqueueIn(duration, pool)
b, _ := json.Marshal(job)
score, _ := redis.Int64(conn.Do("ZSCORE", "schedule", b))
after := now.Add(duration).Unix()
if score != after {
t.Errorf("Expected the timestamp to be %d but got %d", after, score)
}
}
func TestMultipleEnqueue(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
job.Enqueue(pool)
if err := job.Enqueue(pool); err != nil {
t.Errorf("Expected enqueue to succeed but got %s", err)
}
}
func TestMultipleEnqueueAt(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
job.EnqueueAt(time.Now(), pool)
if err := job.EnqueueAt(time.Now(), pool); err != nil {
t.Errorf("Expected enqueue to succeed but got %s", err)
}
}
func TestMultipleEnqueueIn(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
job.EnqueueIn(5*time.Second, pool)
if err := job.EnqueueIn(5*time.Second, pool); err != nil {
t.Errorf("Expected enqueue to succeed but got %s", err)
}
}
func TestEnqueueReturnsMarshalError(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
job := NewJob("foo", "bar", []interface{}{map[float32]string{
0: "Do you pronounce JSON as jay-sun or jay-さん?",
}}, 0)
err := job.Enqueue(pool)
if _, ok := err.(*json.UnsupportedTypeError); !ok {
t.Errorf("unexpected error: got %T, wanted *json.UnsupportedTypeError", err)
}
}
func TestEnqueueAtReturnsMarshalError(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
job := NewJob("foo", "bar", []interface{}{math.NaN()}, 0)
err := job.EnqueueAt(time.Now(), pool)
if _, ok := err.(*json.UnsupportedValueError); !ok {
t.Errorf("unexpected error: got %T, wanted *json.UnsupportedValueError", err)
}
}
func TestEnqueueInReturnsMarshalError(t *testing.T) {
conn := pool.Get()
defer resetRedis(conn)
job := NewJob("foo", "bar", []interface{}{map[string]interface{}{
"rpc magic": func(a, b int) int { return a + b },
}}, 0)
err := job.EnqueueAt(time.Now(), pool)
if _, ok := err.(*json.UnsupportedTypeError); !ok {
t.Errorf("unexpected error: got %T, wanted *json.UnsupportedTypeError", err)
}
}