Skip to content

Commit 28e6fd8

Browse files
committed
Asserts packets are sent in TCP tests
- Assert the emit callback is invoked exactly once. - Add a separate macro for checking that no packets were sent.
1 parent 7d22445 commit 28e6fd8

File tree

1 file changed

+44
-25
lines changed

1 file changed

+44
-25
lines changed

src/socket/tcp.rs

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2425,6 +2425,7 @@ mod test {
24252425
{
24262426
socket.cx.set_now(timestamp);
24272427

2428+
let mut sent = 0;
24282429
let result = socket
24292430
.socket
24302431
.dispatch(&mut socket.cx, |_, (ip_repr, tcp_repr)| {
@@ -2434,14 +2435,27 @@ mod test {
24342435
assert_eq!(ip_repr.payload_len(), tcp_repr.buffer_len());
24352436

24362437
net_trace!("recv: {}", tcp_repr);
2438+
sent += 1;
24372439
Ok(f(Ok(tcp_repr)))
24382440
});
24392441
match result {
2440-
Ok(()) => (),
2442+
Ok(()) => assert_eq!(sent, 1, "Exactly one packet should be sent"),
24412443
Err(e) => f(Err(e)),
24422444
}
24432445
}
24442446

2447+
fn recv_nothing(socket: &mut TestSocket, timestamp: Instant) {
2448+
socket.cx.set_now(timestamp);
2449+
2450+
let result: Result<(), ()> = socket
2451+
.socket
2452+
.dispatch(&mut socket.cx, |_, (_ip_repr, _tcp_repr)| {
2453+
panic!("Should not send a packet")
2454+
});
2455+
2456+
assert_eq!(result, Ok(()))
2457+
}
2458+
24452459
macro_rules! send {
24462460
($socket:ident, $repr:expr) =>
24472461
(send!($socket, time 0, $repr));
@@ -2456,7 +2470,7 @@ mod test {
24562470
macro_rules! recv {
24572471
($socket:ident, [$( $repr:expr ),*]) => ({
24582472
$( recv!($socket, Ok($repr)); )*
2459-
recv!($socket, Err(Error::Exhausted))
2473+
recv_nothing!($socket)
24602474
});
24612475
($socket:ident, $result:expr) =>
24622476
(recv!($socket, time 0, $result));
@@ -2473,6 +2487,11 @@ mod test {
24732487
(recv(&mut $socket, Instant::from_millis($time), |repr| assert_eq!(repr, $result)));
24742488
}
24752489

2490+
macro_rules! recv_nothing {
2491+
($socket:ident) => (recv_nothing!($socket, time 0));
2492+
($socket:ident, time $time:expr) => (recv_nothing(&mut $socket, Instant::from_millis($time)));
2493+
}
2494+
24762495
macro_rules! sanity {
24772496
($socket1:expr, $socket2:expr) => {{
24782497
let (s1, s2) = ($socket1, $socket2);
@@ -3201,7 +3220,7 @@ mod test {
32013220
..RECV_TEMPL
32023221
}]
32033222
);
3204-
recv!(s, time 1000, Err(Error::Exhausted));
3223+
recv_nothing!(s, time 1000);
32053224
assert_eq!(s.state, State::Established);
32063225
sanity!(s, socket_established());
32073226
}
@@ -4376,7 +4395,7 @@ mod test {
43764395
}]
43774396
);
43784397
assert_eq!(s.state, State::TimeWait);
4379-
recv!(s, time 60_000, Err(Error::Exhausted));
4398+
recv_nothing!(s, time 60_000);
43804399
assert_eq!(s.state, State::Closed);
43814400
}
43824401

@@ -4910,7 +4929,7 @@ mod test {
49104929
payload: &b"abcdef"[..],
49114930
..RECV_TEMPL
49124931
}));
4913-
recv!(s, time 1050, Err(Error::Exhausted));
4932+
recv_nothing!(s, time 1050);
49144933
recv!(s, time 2000, Ok(TcpRepr {
49154934
seq_number: LOCAL_SEQ + 1,
49164935
ack_number: Some(REMOTE_SEQ + 1),
@@ -4939,9 +4958,9 @@ mod test {
49394958
payload: &b"012345"[..],
49404959
..RECV_TEMPL
49414960
}), exact);
4942-
recv!(s, time 0, Err(Error::Exhausted));
4961+
recv_nothing!(s, time 0);
49434962

4944-
recv!(s, time 50, Err(Error::Exhausted));
4963+
recv_nothing!(s, time 50);
49454964

49464965
recv!(s, time 1000, Ok(TcpRepr {
49474966
control: TcpControl::None,
@@ -4957,7 +4976,7 @@ mod test {
49574976
payload: &b"012345"[..],
49584977
..RECV_TEMPL
49594978
}), exact);
4960-
recv!(s, time 1550, Err(Error::Exhausted));
4979+
recv_nothing!(s, time 1550);
49614980
}
49624981

49634982
#[test]
@@ -5534,7 +5553,7 @@ mod test {
55345553

55355554
// even though we're in "fast retransmit", we shouldn't
55365555
// force-send anything because the remote's window is full.
5537-
recv!(s, Err(Error::Exhausted));
5556+
recv_nothing!(s);
55385557
}
55395558

55405559
// =========================================================================================//
@@ -5616,7 +5635,7 @@ mod test {
56165635
assert_eq!(s.recv_slice(rx_buf), Ok(4));
56175636

56185637
// check that we do NOT send a window update even if it has changed.
5619-
recv!(s, Err(Error::Exhausted));
5638+
recv_nothing!(s);
56205639
}
56215640

56225641
#[test]
@@ -5649,7 +5668,7 @@ mod test {
56495668
assert_eq!(s.recv_slice(rx_buf), Ok(4));
56505669

56515670
// check that we do NOT send a window update even if it has changed.
5652-
recv!(s, Err(Error::Exhausted));
5671+
recv_nothing!(s);
56535672
}
56545673

56555674
// =========================================================================================//
@@ -5765,7 +5784,7 @@ mod test {
57655784
..RECV_TEMPL
57665785
}]
57675786
);
5768-
recv!(s, time 0, Err(Error::Exhausted));
5787+
recv_nothing!(s, time 0);
57695788
s.recv(|buffer| {
57705789
assert_eq!(&buffer[..3], b"abc");
57715790
(3, ())
@@ -5777,7 +5796,7 @@ mod test {
57775796
window_len: 3,
57785797
..RECV_TEMPL
57795798
}));
5780-
recv!(s, time 0, Err(Error::Exhausted));
5799+
recv_nothing!(s, time 0);
57815800
s.recv(|buffer| {
57825801
assert_eq!(buffer, b"def");
57835802
(buffer.len(), ())
@@ -5941,7 +5960,7 @@ mod test {
59415960
fn test_established_timeout() {
59425961
let mut s = socket_established();
59435962
s.set_timeout(Some(Duration::from_millis(1000)));
5944-
recv!(s, time 250, Err(Error::Exhausted));
5963+
recv_nothing!(s, time 250);
59455964
assert_eq!(
59465965
s.socket.poll_at(&mut s.cx),
59475966
PollAt::Time(Instant::from_millis(1250))
@@ -5988,7 +6007,7 @@ mod test {
59886007
payload: &[0],
59896008
..RECV_TEMPL
59906009
}));
5991-
recv!(s, time 100, Err(Error::Exhausted));
6010+
recv_nothing!(s, time 100);
59926011
assert_eq!(
59936012
s.socket.poll_at(&mut s.cx),
59946013
PollAt::Time(Instant::from_millis(150))
@@ -6008,19 +6027,19 @@ mod test {
60086027
payload: &[0],
60096028
..RECV_TEMPL
60106029
}));
6011-
recv!(s, time 155, Err(Error::Exhausted));
6030+
recv_nothing!(s, time 155);
60126031
assert_eq!(
60136032
s.socket.poll_at(&mut s.cx),
60146033
PollAt::Time(Instant::from_millis(205))
60156034
);
6016-
recv!(s, time 200, Err(Error::Exhausted));
6035+
recv_nothing!(s, time 200);
60176036
recv!(s, time 205, Ok(TcpRepr {
60186037
control: TcpControl::Rst,
60196038
seq_number: LOCAL_SEQ + 1,
60206039
ack_number: Some(REMOTE_SEQ + 1),
60216040
..RECV_TEMPL
60226041
}));
6023-
recv!(s, time 205, Err(Error::Exhausted));
6042+
recv_nothing!(s, time 205);
60246043
assert_eq!(s.state, State::Closed);
60256044
}
60266045

@@ -6118,7 +6137,7 @@ mod test {
61186137
s.socket.poll_at(&mut s.cx),
61196138
PollAt::Time(Instant::from_millis(100))
61206139
);
6121-
recv!(s, time 95, Err(Error::Exhausted));
6140+
recv_nothing!(s, time 95);
61226141
recv!(s, time 100, Ok(TcpRepr {
61236142
seq_number: LOCAL_SEQ,
61246143
ack_number: Some(REMOTE_SEQ + 1),
@@ -6130,7 +6149,7 @@ mod test {
61306149
s.socket.poll_at(&mut s.cx),
61316150
PollAt::Time(Instant::from_millis(200))
61326151
);
6133-
recv!(s, time 195, Err(Error::Exhausted));
6152+
recv_nothing!(s, time 195);
61346153
recv!(s, time 200, Ok(TcpRepr {
61356154
seq_number: LOCAL_SEQ,
61366155
ack_number: Some(REMOTE_SEQ + 1),
@@ -6147,7 +6166,7 @@ mod test {
61476166
s.socket.poll_at(&mut s.cx),
61486167
PollAt::Time(Instant::from_millis(350))
61496168
);
6150-
recv!(s, time 345, Err(Error::Exhausted));
6169+
recv_nothing!(s, time 345);
61516170
recv!(s, time 350, Ok(TcpRepr {
61526171
seq_number: LOCAL_SEQ,
61536172
ack_number: Some(REMOTE_SEQ + 1),
@@ -6512,7 +6531,7 @@ mod test {
65126531
);
65136532

65146533
// No ACK is immediately sent.
6515-
recv!(s, Err(Error::Exhausted));
6534+
recv_nothing!(s);
65166535

65176536
// After 10ms, it is sent.
65186537
recv!(s, time 11, Ok(TcpRepr {
@@ -6545,7 +6564,7 @@ mod test {
65456564
.unwrap();
65466565

65476566
// However, no ACK or window update is immediately sent.
6548-
recv!(s, Err(Error::Exhausted));
6567+
recv_nothing!(s);
65496568

65506569
// After 10ms, it is sent.
65516570
recv!(s, time 11, Ok(TcpRepr {
@@ -6605,7 +6624,7 @@ mod test {
66056624
);
66066625

66076626
// No ACK is immediately sent.
6608-
recv!(s, Err(Error::Exhausted));
6627+
recv_nothing!(s);
66096628

66106629
send!(
66116630
s,
@@ -6644,7 +6663,7 @@ mod test {
66446663
);
66456664

66466665
// No ACK is immediately sent.
6647-
recv!(s, Err(Error::Exhausted));
6666+
recv_nothing!(s);
66486667

66496668
send!(
66506669
s,

0 commit comments

Comments
 (0)