MCPcopy Index your code
hub / github.com/coder/coder / TestWebsocketNetConn_LargeWrites

Function TestWebsocketNetConn_LargeWrites

codersdk/websocket_test.go:21–80  ·  view source on GitHub ↗

TestWebsocketNetConn_LargeWrites tests that we can write large amounts of data thru the netconn in a single write. Without specifically setting the read limit, the websocket library limits the amount of data that can be read in a single message to 32kiB. Even after raising the limit, curiously, it

(t *testing.T)

Source from the content-addressed store, hash-verified

19// the amount of data that can be read in a single message to 32kiB. Even after raising the limit,
20// curiously, it still only reads 32kiB per Read(), but allows the large write to go thru.
21func TestWebsocketNetConn_LargeWrites(t *testing.T) {
22 t.Parallel()
23 ctx := testutil.Context(t, testutil.WaitShort)
24 n := 4 * 1024 * 1024 // 4 MiB
25 svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26 sws, err := websocket.Accept(w, r, nil)
27 if !assert.NoError(t, err) {
28 return
29 }
30 _, nc := codersdk.WebsocketNetConn(r.Context(), sws, websocket.MessageBinary)
31 defer nc.Close()
32
33 // Although the writes are all in one go, the reads get broken up by
34 // the library.
35 j := 0
36 b := make([]byte, n)
37 for j < n {
38 k, err := nc.Read(b[j:])
39 if !assert.NoError(t, err) {
40 return
41 }
42 j += k
43 t.Logf("server read %d bytes, total %d", k, j)
44 }
45 assert.Equal(t, n, j)
46 j, err = nc.Write(b)
47 assert.Equal(t, n, j)
48 if !assert.NoError(t, err) {
49 return
50 }
51 }))
52
53 // use of random data is worst case scenario for compression
54 cb := make([]byte, n)
55 rk, err := rand.Read(cb)
56 require.NoError(t, err)
57 require.Equal(t, n, rk)
58
59 // nolint: bodyclose
60 cws, _, err := websocket.Dial(ctx, svr.URL, nil)
61 require.NoError(t, err)
62 _, cnc := codersdk.WebsocketNetConn(ctx, cws, websocket.MessageBinary)
63 ck, err := cnc.Write(cb)
64 require.NoError(t, err)
65 require.Equal(t, n, ck)
66
67 cb2 := make([]byte, n)
68 j := 0
69 for j < n {
70 k, err := cnc.Read(cb2[j:])
71 if !assert.NoError(t, err) {
72 return
73 }
74 j += k
75 t.Logf("client read %d bytes, total %d", k, j)
76 }
77 require.NoError(t, err)
78 require.Equal(t, n, j)

Callers

nothing calls this directly

Calls 10

ContextFunction · 0.92
WebsocketNetConnFunction · 0.92
ContextMethod · 0.65
CloseMethod · 0.65
ReadMethod · 0.65
LogfMethod · 0.65
WriteMethod · 0.65
DialMethod · 0.65
AcceptMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected