-
Notifications
You must be signed in to change notification settings - Fork 9
/
worker_client_test.go
126 lines (101 loc) · 2.86 KB
/
worker_client_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
//go:build redisint
package main
import (
"encoding/json"
"testing"
"github.com/jrallison/go-workers"
"github.com/stretchr/testify/require"
"gopkg.in/redis.v5"
)
// These tests require an existing redis database that matches the config below.
// If one doesn't exist they'll probably fail
var config = RedisConfig{
Host: "localhost:6379",
Namespace: "integration",
Queue: "testq",
Password: "",
}
func TestWorker_Init(t *testing.T) {
_, err := NewRedisWorkerClient(config)
require.NoError(t, err)
// no host
_, err = NewRedisWorkerClient(
RedisConfig{
Host: "",
Namespace: "integration",
Queue: "testq",
},
)
require.Error(t, err)
// no queue
_, err = NewRedisWorkerClient(
RedisConfig{
Host: "localhost:6379",
Namespace: "integration",
Queue: "",
},
)
require.Error(t, err)
// no namespace, this doesn't error
_, err = NewRedisWorkerClient(
RedisConfig{
Host: "localhost:6379",
Namespace: "",
Queue: "testq",
},
)
require.NoError(t, err)
// use auth, this doesn't error
_, err = NewRedisWorkerClient(
RedisConfig{
Host: "localhost:6379",
Namespace: "",
Queue: "testq",
Password: "1234",
},
)
require.NoError(t, err)
}
func TestWorker_Push(t *testing.T) {
redisHandle := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
})
err := redisHandle.Del("integration:queue:testq").Err()
require.NoError(t, err)
client, err := NewRedisWorkerClient(config)
require.NoError(t, err)
fooMessage := `{"msg":"foo"}`
barMessage := `{"msg":"bar"}`
fooJID, err := client.Push("FooWorker", fooMessage)
require.NoError(t, err)
barJID, err := client.Push("BarWorker", barMessage)
require.NoError(t, err)
require.NotEqual(t, fooJID, barJID)
// first thing we enqueued
data, err := redisHandle.LPop("integration:queue:testq").Bytes()
require.NoError(t, err)
fooEnqueued := &workers.EnqueueData{}
err = json.Unmarshal(data, fooEnqueued)
require.NoError(t, err)
require.Equal(t, fooEnqueued.Jid, fooJID)
require.Equal(t, fooEnqueued.Class, "FooWorker")
require.Equal(t, fooEnqueued.Args, []interface{}{map[string]interface{}{"msg": "foo"}})
require.Equal(t, fooEnqueued.EnqueueOptions.Retry, true)
// second thing we enqueued
data, err = redisHandle.LPop("integration:queue:testq").Bytes()
require.NoError(t, err)
barEnqueued := &workers.EnqueueData{}
err = json.Unmarshal(data, barEnqueued)
require.NoError(t, err)
require.Equal(t, barEnqueued.Jid, barJID)
require.Equal(t, barEnqueued.Class, "BarWorker")
require.Equal(t, barEnqueued.Args, []interface{}{map[string]interface{}{"msg": "bar"}})
require.Equal(t, fooEnqueued.EnqueueOptions.Retry, true)
// verify retry is flat
barFlat := make(map[string]interface{})
err = json.Unmarshal(data, &barFlat)
require.NoError(t, err)
require.Equal(t, barFlat["retry"], true)
}