-
Notifications
You must be signed in to change notification settings - Fork 297
/
chat_test.go
277 lines (233 loc) · 5.81 KB
/
chat_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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package main
import (
"context"
"crypto/rand"
"fmt"
"math/big"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"golang.org/x/time/rate"
"github.com/coder/websocket"
)
func Test_chatServer(t *testing.T) {
t.Parallel()
// This is a simple echo test with a single client.
// The client sends a message and ensures it receives
// it on its WebSocket.
t.Run("simple", func(t *testing.T) {
t.Parallel()
url, closeFn := setupTest(t)
defer closeFn()
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
cl, err := newClient(ctx, url)
assertSuccess(t, err)
defer cl.Close()
expMsg := randString(512)
err = cl.publish(ctx, expMsg)
assertSuccess(t, err)
msg, err := cl.nextMessage()
assertSuccess(t, err)
if expMsg != msg {
t.Fatalf("expected %v but got %v", expMsg, msg)
}
})
// This test is a complex concurrency test.
// 10 clients are started that send 128 different
// messages of max 128 bytes concurrently.
//
// The test verifies that every message is seen by every client
// and no errors occur anywhere.
t.Run("concurrency", func(t *testing.T) {
t.Parallel()
const nmessages = 128
const maxMessageSize = 128
const nclients = 16
url, closeFn := setupTest(t)
defer closeFn()
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
var clients []*client
var clientMsgs []map[string]struct{}
for i := 0; i < nclients; i++ {
cl, err := newClient(ctx, url)
assertSuccess(t, err)
defer cl.Close()
clients = append(clients, cl)
clientMsgs = append(clientMsgs, randMessages(nmessages, maxMessageSize))
}
allMessages := make(map[string]struct{})
for _, msgs := range clientMsgs {
for m := range msgs {
allMessages[m] = struct{}{}
}
}
var wg sync.WaitGroup
for i, cl := range clients {
i := i
cl := cl
wg.Add(1)
go func() {
defer wg.Done()
err := cl.publishMsgs(ctx, clientMsgs[i])
if err != nil {
t.Errorf("client %d failed to publish all messages: %v", i, err)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
err := testAllMessagesReceived(cl, nclients*nmessages, allMessages)
if err != nil {
t.Errorf("client %d failed to receive all messages: %v", i, err)
}
}()
}
wg.Wait()
})
}
// setupTest sets up chatServer that can be used
// via the returned url.
//
// Defer closeFn to ensure everything is cleaned up at
// the end of the test.
//
// chatServer logs will be logged via t.Logf.
func setupTest(t *testing.T) (url string, closeFn func()) {
cs := newChatServer()
cs.logf = t.Logf
// To ensure tests run quickly under even -race.
cs.subscriberMessageBuffer = 4096
cs.publishLimiter.SetLimit(rate.Inf)
s := httptest.NewServer(cs)
return s.URL, func() {
s.Close()
}
}
// testAllMessagesReceived ensures that after n reads, all msgs in msgs
// have been read.
func testAllMessagesReceived(cl *client, n int, msgs map[string]struct{}) error {
msgs = cloneMessages(msgs)
for i := 0; i < n; i++ {
msg, err := cl.nextMessage()
if err != nil {
return err
}
delete(msgs, msg)
}
if len(msgs) != 0 {
return fmt.Errorf("did not receive all expected messages: %q", msgs)
}
return nil
}
func cloneMessages(msgs map[string]struct{}) map[string]struct{} {
msgs2 := make(map[string]struct{}, len(msgs))
for m := range msgs {
msgs2[m] = struct{}{}
}
return msgs2
}
func randMessages(n, maxMessageLength int) map[string]struct{} {
msgs := make(map[string]struct{})
for i := 0; i < n; i++ {
m := randString(randInt(maxMessageLength))
if _, ok := msgs[m]; ok {
i--
continue
}
msgs[m] = struct{}{}
}
return msgs
}
func assertSuccess(t *testing.T, err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
type client struct {
url string
c *websocket.Conn
}
func newClient(ctx context.Context, url string) (*client, error) {
c, _, err := websocket.Dial(ctx, url+"/subscribe", nil)
if err != nil {
return nil, err
}
cl := &client{
url: url,
c: c,
}
return cl, nil
}
func (cl *client) publish(ctx context.Context, msg string) (err error) {
defer func() {
if err != nil {
cl.c.Close(websocket.StatusInternalError, "publish failed")
}
}()
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, cl.url+"/publish", strings.NewReader(msg))
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusAccepted {
return fmt.Errorf("publish request failed: %v", resp.StatusCode)
}
return nil
}
func (cl *client) publishMsgs(ctx context.Context, msgs map[string]struct{}) error {
for m := range msgs {
err := cl.publish(ctx, m)
if err != nil {
return err
}
}
return nil
}
func (cl *client) nextMessage() (string, error) {
typ, b, err := cl.c.Read(context.Background())
if err != nil {
return "", err
}
if typ != websocket.MessageText {
cl.c.Close(websocket.StatusUnsupportedData, "expected text message")
return "", fmt.Errorf("expected text message but got %v", typ)
}
return string(b), nil
}
func (cl *client) Close() error {
return cl.c.Close(websocket.StatusNormalClosure, "")
}
// randString generates a random string with length n.
func randString(n int) string {
b := make([]byte, n)
_, err := rand.Reader.Read(b)
if err != nil {
panic(fmt.Sprintf("failed to generate rand bytes: %v", err))
}
s := strings.ToValidUTF8(string(b), "_")
s = strings.ReplaceAll(s, "\x00", "_")
if len(s) > n {
return s[:n]
}
if len(s) < n {
// Pad with =
extra := n - len(s)
return s + strings.Repeat("=", extra)
}
return s
}
// randInt returns a randomly generated integer between [0, max).
func randInt(max int) int {
x, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
panic(fmt.Sprintf("failed to get random int: %v", err))
}
return int(x.Int64())
}