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

Function TestWriteThenWriterContextTakeover

compress_test.go:141–224  ·  view source on GitHub ↗

TestWriteThenWriterContextTakeover verifies that using Conn.Write followed by Conn.Writer works correctly with context takeover enabled. This tests that the flateWriter destination is properly restored after Conn.Write redirects it to a temporary buffer.

(t *testing.T)

Source from the content-addressed store, hash-verified

139// the flateWriter destination is properly restored after Conn.Write redirects
140// it to a temporary buffer.
141func TestWriteThenWriterContextTakeover(t *testing.T) {
142 t.Parallel()
143
144 clientConn, serverConn := net.Pipe()
145 defer clientConn.Close()
146 defer serverConn.Close()
147
148 client := newConn(connConfig{
149 rwc: clientConn,
150 client: true,
151 copts: CompressionContextTakeover.opts(),
152 flateThreshold: 64,
153 br: bufio.NewReader(clientConn),
154 bw: bufio.NewWriterSize(clientConn, 4096),
155 })
156
157 server := newConn(connConfig{
158 rwc: serverConn,
159 client: false,
160 copts: CompressionContextTakeover.opts(),
161 flateThreshold: 64,
162 br: bufio.NewReader(serverConn),
163 bw: bufio.NewWriterSize(serverConn, 4096),
164 })
165
166 ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
167 defer cancel()
168
169 msg1 := []byte(strings.Repeat("first message ", 100))
170 msg2 := []byte(strings.Repeat("second message ", 100))
171
172 type readResult struct {
173 typ MessageType
174 p []byte
175 err error
176 }
177 readCh := make(chan readResult, 3)
178 go func() {
179 for range 3 {
180 typ, p, err := server.Read(ctx)
181 readCh <- readResult{typ, p, err}
182 }
183 }()
184
185 // We want to verify mixing `Write` and `Writer` usages still work.
186 //
187 // To this end, we call them in this order:
188 // - `Write`
189 // - `Writer`
190 // - `Write`
191 //
192 // This verifies that it works for a `Write` followed by a `Writer`
193 // as well as a `Writer` followed by a `Write`.
194
195 // 1. `Write` API
196 err := client.Write(ctx, MessageText, msg1)
197 assert.Success(t, err)
198

Callers

nothing calls this directly

Calls 8

SuccessFunction · 0.92
EqualFunction · 0.92
newConnFunction · 0.85
optsMethod · 0.80
CloseMethod · 0.45
ReadMethod · 0.45
WriteMethod · 0.45
WriterMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…