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

Function TestCompressionDictionaryPreserved

compress_test.go:229–311  ·  view source on GitHub ↗

TestCompressionDictionaryPreserved verifies that context takeover mode preserves the compression dictionary across Conn.Write calls, resulting in better compression for consecutive similar messages.

(t *testing.T)

Source from the content-addressed store, hash-verified

227// preserves the compression dictionary across Conn.Write calls, resulting
228// in better compression for consecutive similar messages.
229func TestCompressionDictionaryPreserved(t *testing.T) {
230 t.Parallel()
231
232 msg := []byte(strings.Repeat(`{"type":"event","data":"value"}`, 50))
233
234 takeoverClient, takeoverServer := net.Pipe()
235 defer takeoverClient.Close()
236 defer takeoverServer.Close()
237
238 withTakeover := newConn(connConfig{
239 rwc: takeoverClient,
240 client: true,
241 copts: CompressionContextTakeover.opts(),
242 flateThreshold: 64,
243 br: bufio.NewReader(takeoverClient),
244 bw: bufio.NewWriterSize(takeoverClient, 4096),
245 })
246
247 noTakeoverClient, noTakeoverServer := net.Pipe()
248 defer noTakeoverClient.Close()
249 defer noTakeoverServer.Close()
250
251 withoutTakeover := newConn(connConfig{
252 rwc: noTakeoverClient,
253 client: true,
254 copts: CompressionNoContextTakeover.opts(),
255 flateThreshold: 64,
256 br: bufio.NewReader(noTakeoverClient),
257 bw: bufio.NewWriterSize(noTakeoverClient, 4096),
258 })
259
260 ctx, cancel := context.WithTimeout(context.Background(), time.Second)
261 defer cancel()
262
263 // Capture compressed sizes for both modes
264 var withTakeoverSizes, withoutTakeoverSizes []int64
265
266 reader1 := bufio.NewReader(takeoverServer)
267 reader2 := bufio.NewReader(noTakeoverServer)
268 readBuf := make([]byte, 8)
269
270 // Send 3 identical messages each
271 for range 3 {
272 // With context takeover
273 writeDone1 := make(chan error, 1)
274 go func() {
275 writeDone1 <- withTakeover.Write(ctx, MessageText, msg)
276 }()
277
278 h1, err := readFrameHeader(reader1, readBuf)
279 assert.Success(t, err)
280
281 _, err = io.CopyN(io.Discard, reader1, h1.payloadLength)
282 assert.Success(t, err)
283
284 withTakeoverSizes = append(withTakeoverSizes, h1.payloadLength)
285 assert.Success(t, <-writeDone1)
286

Callers

nothing calls this directly

Calls 6

SuccessFunction · 0.92
newConnFunction · 0.85
readFrameHeaderFunction · 0.85
optsMethod · 0.80
CloseMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…