-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathredis_test.go
193 lines (153 loc) · 3.38 KB
/
redis_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
package main
import (
"fmt"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
redcon "github.com/tidwall/redcon"
)
var redcliPath string
func init() {
if runtime.GOOS == "linux" && runtime.GOARCH == "amd64" {
redcliPath = "./testdata/cmd/redcli_linux_amd64"
} else if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
redcliPath = "./testdata/cmd/redcli_darwin_arm64"
} else {
panic("unsupported test platform")
}
}
func TestRedisPublish(t *testing.T) {
t.Run("publish publishes to the respective queue", func(t *testing.T) {
_ = helperNewTestRedisServer(t)
var (
topic = "topic"
val = "value"
)
publishOne(t, topic, val)
res := consumeOne(t, topic)
require.Equal(t, val, res)
})
}
func TestRedisSubscribe(t *testing.T) {
t.Run("subscriber waits for message", func(t *testing.T) {
_ = helperNewTestRedisServer(t)
var (
topic = "topic"
val = "value"
)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
res := consumeOne(t, topic)
require.Equal(t, val, res)
wg.Done()
}()
publishOne(t, topic, val)
wg.Wait()
})
t.Run("subscriber waiting for two messages on same topic", func(t *testing.T) {
_ = helperNewTestRedisServer(t)
var (
topic = "topic"
val1 = "value1"
val2 = "value2"
)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
res := consumeOne(t, topic)
require.Equal(t, val1, res)
res = consumeOne(t, topic)
require.Equal(t, val2, res)
wg.Done()
}()
publishOne(t, topic, val1)
publishOne(t, topic, val2)
wg.Wait()
})
t.Run("subscribers on seperate topics", func(t *testing.T) {
_ = helperNewTestRedisServer(t)
var (
topic1 = "topic1"
topic2 = "topic2"
val1 = "value1"
val2 = "value2"
)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
res := consumeOne(t, topic1)
require.Equal(t, val1, res)
wg.Done()
}()
wg.Add(1)
go func() {
res := consumeOne(t, topic2)
require.Equal(t, val2, res)
wg.Done()
}()
publishOne(t, topic1, val1)
publishOne(t, topic2, val2)
wg.Wait()
})
t.Run("large message quantity, same topic", func(t *testing.T) {
_ = helperNewTestRedisServer(t)
var (
topic = "topic"
)
// Publish n messages
n := 50
for i := 0; i < n; i++ {
publishOne(t, topic, strconv.Itoa(i))
}
for i := 0; i < n; i++ {
res := consumeOne(t, topic)
require.Equal(t, strconv.Itoa(i), res)
}
})
}
// Helpers
func publishOne(t *testing.T, topic, value string) {
t.Helper()
cmd := exec.Command(redcliPath, "publish", topic, value)
raw, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err, string(raw))
}
out := strings.TrimSuffix(string(raw), "\n")
fmt.Println(out)
}
func consumeOne(t *testing.T, topic string) string {
t.Helper()
cmd := exec.Command(redcliPath, "subscribe", "-c", "1", topic)
raw, err := cmd.CombinedOutput()
if err != nil {
t.Fatal(err, string(raw))
}
out := strings.TrimSuffix(string(raw), "\n")
fmt.Println(out)
return out
}
func helperNewTestRedisServer(t *testing.T) *redis {
dir, err := os.MkdirTemp("", "miniqueue_")
require.NoError(t, err)
r := newRedis(newBroker(newStore(dir)))
s := redcon.NewServer("localhost:6379", r.handleCmd, nil, nil)
t.Cleanup(func() {
s.Close()
})
go func() {
err := s.ListenAndServe()
if err != nil {
t.Error(err)
}
}()
time.Sleep(10 * time.Millisecond)
return r
}