MCPcopy Index your code
hub / github.com/wavetermdev/waveterm / TestOutOfOrderPackets

Function TestOutOfOrderPackets

pkg/streamclient/stream_test.go:270–329  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

268}
269
270func TestOutOfOrderPackets(t *testing.T) {
271 transport := newFakeTransport()
272 reader := NewReader("test-ooo", 1024, transport)
273
274 packet0 := wshrpc.CommandStreamData{
275 Id: "test-ooo",
276 Seq: 0,
277 Data64: base64.StdEncoding.EncodeToString([]byte("AAAAA")),
278 }
279 packet5 := wshrpc.CommandStreamData{
280 Id: "test-ooo",
281 Seq: 5,
282 Data64: base64.StdEncoding.EncodeToString([]byte("BBBBB")),
283 }
284 packet10 := wshrpc.CommandStreamData{
285 Id: "test-ooo",
286 Seq: 10,
287 Data64: base64.StdEncoding.EncodeToString([]byte("CCCCC")),
288 }
289 packet15 := wshrpc.CommandStreamData{
290 Id: "test-ooo",
291 Seq: 15,
292 Data64: base64.StdEncoding.EncodeToString([]byte("DDDDD")),
293 }
294
295 // Send packets out of order: 0, 10, 15, 5
296 reader.RecvData(packet0)
297 reader.RecvData(packet10) // OOO - should be buffered
298 reader.RecvData(packet15) // OOO - should be buffered
299 reader.RecvData(packet5) // fills the gap - should trigger processing
300
301 // Read all data
302 buf := make([]byte, 1024)
303 totalRead := 0
304 expectedLen := 20 // 4 packets * 5 bytes each
305
306 readDone := make(chan struct{})
307 go func() {
308 for totalRead < expectedLen {
309 n, err := reader.Read(buf[totalRead:])
310 if err != nil {
311 t.Errorf("Read failed: %v", err)
312 return
313 }
314 totalRead += n
315 }
316 close(readDone)
317 }()
318
319 select {
320 case <-readDone:
321 // Success
322 case <-time.After(2 * time.Second):
323 t.Fatalf("Read didn't complete in time. Read %d bytes, expected %d", totalRead, expectedLen)
324 }
325
326 if totalRead != expectedLen {
327 t.Fatalf("Expected to read %d bytes, got %d", expectedLen, totalRead)

Callers

nothing calls this directly

Calls 4

RecvDataMethod · 0.95
ReadMethod · 0.95
newFakeTransportFunction · 0.85
NewReaderFunction · 0.85

Tested by

no test coverage detected