MCPcopy
hub / github.com/coder/websocket / Test_chatServer

Function Test_chatServer

internal/examples/chat/chat_test.go:20–114  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

18)
19
20func Test_chatServer(t *testing.T) {
21 t.Parallel()
22
23 // This is a simple echo test with a single client.
24 // The client sends a message and ensures it receives
25 // it on its WebSocket.
26 t.Run("simple", func(t *testing.T) {
27 t.Parallel()
28
29 url, closeFn := setupTest(t)
30 defer closeFn()
31
32 ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
33 defer cancel()
34
35 cl, err := newClient(ctx, url)
36 assertSuccess(t, err)
37 defer cl.Close()
38
39 expMsg := randString(512)
40 err = cl.publish(ctx, expMsg)
41 assertSuccess(t, err)
42
43 msg, err := cl.nextMessage()
44 assertSuccess(t, err)
45
46 if expMsg != msg {
47 t.Fatalf("expected %v but got %v", expMsg, msg)
48 }
49 })
50
51 // This test is a complex concurrency test.
52 // 10 clients are started that send 128 different
53 // messages of max 128 bytes concurrently.
54 //
55 // The test verifies that every message is seen by every client
56 // and no errors occur anywhere.
57 t.Run("concurrency", func(t *testing.T) {
58 t.Parallel()
59
60 const nmessages = 128
61 const maxMessageSize = 128
62 const nclients = 16
63
64 url, closeFn := setupTest(t)
65 defer closeFn()
66
67 ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
68 defer cancel()
69
70 var clients []*client
71 var clientMsgs []map[string]struct{}
72 for i := 0; i < nclients; i++ {
73 cl, err := newClient(ctx, url)
74 assertSuccess(t, err)
75 defer cl.Close()
76
77 clients = append(clients, cl)

Callers

nothing calls this directly

Calls 10

setupTestFunction · 0.85
newClientFunction · 0.85
assertSuccessFunction · 0.85
randStringFunction · 0.85
randMessagesFunction · 0.85
testAllMessagesReceivedFunction · 0.85
nextMessageMethod · 0.80
publishMsgsMethod · 0.80
CloseMethod · 0.45
publishMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…