Skip to content

Commit

Permalink
quic: add test helpers for acking packets
Browse files Browse the repository at this point in the history
Add connTest methods to send the conn-under-test an ACK
for the latest packet it sent, or for all packets in the
number space it last sent in.

For golang/go#58547

Change-Id: Id35cad9bddf9dd32074dc121fd360a65b989fb4b
Reviewed-on: https://go-review.googlesource.com/c/net/+/522055
Run-TryBot: Damien Neil <dneil@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
  • Loading branch information
neild committed Aug 29, 2023
1 parent 4a2d37e commit 52fbe37
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions internal/quic/conn_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestConnIDPeerRequestsRetirement(t *testing.T) {
packetType1RTT, debugFrameRetireConnectionID{
seq: 0,
})
if got, want := tc.sentFramePacket.dstConnID, testPeerConnID(1); !bytes.Equal(got, want) {
if got, want := tc.lastPacket.dstConnID, testPeerConnID(1); !bytes.Equal(got, want) {
t.Fatalf("used destination conn id {%x}, want {%x}", got, want)
}
}
Expand Down Expand Up @@ -467,7 +467,7 @@ func TestConnIDUsePreferredAddressConnID(t *testing.T) {
packetType1RTT, debugFrameRetireConnectionID{
seq: 0,
})
if got, want := tc.sentFramePacket.dstConnID, cid; !bytes.Equal(got, want) {
if got, want := tc.lastPacket.dstConnID, cid; !bytes.Equal(got, want) {
t.Fatalf("used destination conn id {%x}, want {%x} from preferred address transport parameter", got, want)
}
}
Expand Down
7 changes: 1 addition & 6 deletions internal/quic/conn_loss_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,7 @@ func TestLostStreamPartialLoss(t *testing.T) {
data: data[i : i+1],
})
if i%2 == 0 {
num := tc.sentFramePacket.num
tc.writeFrames(packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{
{num, num + 1},
},
})
tc.writeAckForLatest()
}
}
const pto = false
Expand Down
34 changes: 28 additions & 6 deletions internal/quic/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ type testConn struct {

// Datagrams, packets, and frames sent by the conn,
// but not yet processed by the test.
sentDatagrams [][]byte
sentPackets []*testPacket
sentFrames []debugFrame
sentFramePacket *testPacket
sentDatagrams [][]byte
sentPackets []*testPacket
sentFrames []debugFrame
lastPacket *testPacket

// Frame types to ignore in tests.
ignoreFrames map[byte]bool
Expand Down Expand Up @@ -388,6 +388,28 @@ func (tc *testConn) writeFrames(ptype packetType, frames ...debugFrame) {
tc.write(d)
}

// writeAckForAll sends the Conn a datagram containing an ack for all packets up to the
// last one received.
func (tc *testConn) writeAckForAll() {
if tc.lastPacket == nil {
return
}
tc.writeFrames(tc.lastPacket.ptype, debugFrameAck{
ranges: []i64range[packetNumber]{{0, tc.lastPacket.num + 1}},
})
}

// writeAckForLatest sends the Conn a datagram containing an ack for the
// most recent packet received.
func (tc *testConn) writeAckForLatest() {
if tc.lastPacket == nil {
return
}
tc.writeFrames(tc.lastPacket.ptype, debugFrameAck{
ranges: []i64range[packetNumber]{{tc.lastPacket.num, tc.lastPacket.num + 1}},
})
}

// ignoreFrame hides frames of the given type sent by the Conn.
func (tc *testConn) ignoreFrame(frameType byte) {
tc.ignoreFrames[frameType] = true
Expand Down Expand Up @@ -423,6 +445,7 @@ func (tc *testConn) readPacket() *testPacket {
}
p := tc.sentPackets[0]
tc.sentPackets = tc.sentPackets[1:]
tc.lastPacket = p
return p
}

Expand All @@ -435,12 +458,11 @@ func (tc *testConn) readFrame() (debugFrame, packetType) {
if p == nil {
return nil, packetTypeInvalid
}
tc.sentFramePacket = p
tc.sentFrames = p.frames
}
f := tc.sentFrames[0]
tc.sentFrames = tc.sentFrames[1:]
return f, tc.sentFramePacket.ptype
return f, tc.lastPacket.ptype
}

// wantDatagram indicates that we expect the Conn to send a datagram.
Expand Down
19 changes: 4 additions & 15 deletions internal/quic/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,7 @@ func TestStreamWriteBlockedByWriteBufferLimit(t *testing.T) {
tc.wantIdle("no STREAM_DATA_BLOCKED, we're blocked locally not by flow control")

// ACK for previously-sent data allows making more progress.
tc.writeFrames(packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{{0, tc.sentFramePacket.num + 1}},
})
tc.writeAckForAll()
tc.wantFrame("ACK for previous data allows making progress",
packetType1RTT, debugFrameStream{
id: s.id,
Expand Down Expand Up @@ -968,9 +966,7 @@ func TestStreamCloseWaitsForAcks(t *testing.T) {
if _, err := closing.result(); err != errNotDone {
t.Fatalf("s.CloseContext() = %v, want it to block waiting for acks", err)
}
tc.writeFrames(packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{{0, tc.sentFramePacket.num + 1}},
})
tc.writeAckForAll()
if _, err := closing.result(); err != nil {
t.Fatalf("s.CloseContext() = %v, want nil (all data acked)", err)
}
Expand All @@ -983,9 +979,7 @@ func TestStreamCloseUnblocked(t *testing.T) {
}{{
name: "data received",
unblock: func(tc *testConn, s *Stream) {
tc.writeFrames(packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{{0, tc.sentFramePacket.num + 1}},
})
tc.writeAckForAll()
},
}, {
name: "stop sending received",
Expand Down Expand Up @@ -1135,12 +1129,7 @@ func TestStreamPeerStopSendingForActiveStream(t *testing.T) {
t.Errorf("s.Write() after STOP_SENDING = %v, %v; want error", n, err)
}
// This ack will result in some of the previous frames being marked as lost.
tc.writeFrames(packetType1RTT, debugFrameAck{
ranges: []i64range[packetNumber]{{
tc.sentFramePacket.num,
tc.sentFramePacket.num + 1,
}},
})
tc.writeAckForLatest()
tc.wantIdle("lost STREAM frames for reset stream are not resent")
})
}
Expand Down
2 changes: 1 addition & 1 deletion internal/quic/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (tc *testConn) uncheckedHandshake() {
debugFrameAck{
ackDelay: unscaledAckDelayFromDuration(
maxAckDelay, ackDelayExponent),
ranges: []i64range[packetNumber]{{0, tc.sentFramePacket.num + 1}},
ranges: []i64range[packetNumber]{{0, tc.lastPacket.num + 1}},
})
} else {
tc.wantIdle("initial frames are ignored")
Expand Down

0 comments on commit 52fbe37

Please sign in to comment.