golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/sent_packet_list_test.go (about) 1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build go1.21 6 7 package quic 8 9 import "testing" 10 11 func TestSentPacketListSlidingWindow(t *testing.T) { 12 // Record 1000 sent packets, acking everything outside the most recent 10. 13 list := &sentPacketList{} 14 const window = 10 15 for i := packetNumber(0); i < 1000; i++ { 16 list.add(&sentPacket{num: i}) 17 if i < window { 18 continue 19 } 20 prev := i - window 21 sent := list.num(prev) 22 if sent == nil { 23 t.Fatalf("packet %v not in list", prev) 24 } 25 if sent.num != prev { 26 t.Fatalf("list.num(%v) = packet %v", prev, sent.num) 27 } 28 if got := list.nth(0); got != sent { 29 t.Fatalf("list.nth(0) != list.num(%v)", prev) 30 } 31 sent.acked = true 32 list.clean() 33 if got := list.num(prev); got != nil { 34 t.Fatalf("list.num(%v) = packet %v, expected it to be discarded", prev, got.num) 35 } 36 if got, want := list.start(), prev+1; got != want { 37 t.Fatalf("list.start() = %v, want %v", got, want) 38 } 39 if got, want := list.end(), i+1; got != want { 40 t.Fatalf("list.end() = %v, want %v", got, want) 41 } 42 if got, want := list.size, window; got != want { 43 t.Fatalf("list.size = %v, want %v", got, want) 44 } 45 } 46 } 47 48 func TestSentPacketListGrows(t *testing.T) { 49 // Record 1000 sent packets. 50 list := &sentPacketList{} 51 const count = 1000 52 for i := packetNumber(0); i < count; i++ { 53 list.add(&sentPacket{num: i}) 54 } 55 if got, want := list.start(), packetNumber(0); got != want { 56 t.Fatalf("list.start() = %v, want %v", got, want) 57 } 58 if got, want := list.end(), packetNumber(count); got != want { 59 t.Fatalf("list.end() = %v, want %v", got, want) 60 } 61 if got, want := list.size, count; got != want { 62 t.Fatalf("list.size = %v, want %v", got, want) 63 } 64 for i := packetNumber(0); i < count; i++ { 65 sent := list.num(i) 66 if sent == nil { 67 t.Fatalf("packet %v not in list", i) 68 } 69 if sent.num != i { 70 t.Fatalf("list.num(%v) = packet %v", i, sent.num) 71 } 72 if got := list.nth(int(i)); got != sent { 73 t.Fatalf("list.nth(%v) != list.num(%v)", int(i), i) 74 } 75 } 76 } 77 78 func TestSentPacketListCleanAll(t *testing.T) { 79 list := &sentPacketList{} 80 // Record 10 sent packets. 81 const count = 10 82 for i := packetNumber(0); i < count; i++ { 83 list.add(&sentPacket{num: i}) 84 } 85 // Mark all the packets as acked. 86 for i := packetNumber(0); i < count; i++ { 87 list.num(i).acked = true 88 } 89 list.clean() 90 if got, want := list.size, 0; got != want { 91 t.Fatalf("list.size = %v, want %v", got, want) 92 } 93 list.add(&sentPacket{num: 10}) 94 if got, want := list.size, 1; got != want { 95 t.Fatalf("list.size = %v, want %v", got, want) 96 } 97 sent := list.num(10) 98 if sent == nil { 99 t.Fatalf("packet %v not in list", 10) 100 } 101 if sent.num != 10 { 102 t.Fatalf("list.num(10) = %v", sent.num) 103 } 104 if got := list.nth(0); got != sent { 105 t.Fatalf("list.nth(0) != list.num(10)") 106 } 107 }