forked from robinjoseph08/redisqueue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
producer_test.go
78 lines (61 loc) · 1.76 KB
/
producer_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
package redisqueue
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewProducer(t *testing.T) {
t.Run("creates a new producer", func(tt *testing.T) {
p, err := NewProducer()
require.NoError(tt, err)
assert.NotNil(tt, p)
})
}
func TestNewProducerWithOptions(t *testing.T) {
t.Run("creates a new producer", func(tt *testing.T) {
p, err := NewProducerWithOptions(&ProducerOptions{})
require.NoError(tt, err)
assert.NotNil(tt, p)
})
t.Run("allows custom *redis.Client", func(tt *testing.T) {
rc := newRedisClient(nil)
p, err := NewProducerWithOptions(&ProducerOptions{
RedisClient: rc,
})
require.NoError(tt, err)
assert.NotNil(tt, p)
assert.Equal(tt, rc, p.redis)
})
t.Run("bubbles up errors", func(tt *testing.T) {
_, err := NewProducerWithOptions(&ProducerOptions{
RedisOptions: &RedisOptions{Addr: "localhost:0"},
})
require.Error(tt, err)
assert.Contains(tt, err.Error(), "dial tcp")
})
}
func TestEnqueue(t *testing.T) {
t.Run("puts the message in the stream", func(tt *testing.T) {
p, err := NewProducerWithOptions(&ProducerOptions{})
require.NoError(t, err)
msg := &Message{
Stream: tt.Name(),
Values: map[string]interface{}{"test": "value"},
}
err = p.Enqueue(msg)
require.NoError(tt, err)
res, err := p.redis.XRange(msg.Stream, msg.ID, msg.ID).Result()
require.NoError(tt, err)
assert.Equal(tt, "value", res[0].Values["test"])
})
t.Run("bubbles up errors", func(tt *testing.T) {
p, err := NewProducerWithOptions(&ProducerOptions{ApproximateMaxLength: true})
require.NoError(t, err)
msg := &Message{
Stream: tt.Name(),
}
err = p.Enqueue(msg)
require.Error(tt, err)
assert.Contains(tt, err.Error(), "wrong number of arguments")
})
}