Skip to content

Commit 59cf3a9

Browse files
committed
free fn to constructor
1 parent 8b8b611 commit 59cf3a9

1 file changed

Lines changed: 50 additions & 48 deletions

File tree

bfd-async/src/session/tests.rs

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,46 @@ struct Harness {
2828
counters: Arc<SessionCounters>,
2929
}
3030

31-
// Spawn a driver task and wrap the extra bits we need in a test harness.
32-
//
33-
// Most tests pass a very large `required_rx` to avoid any flakiness introduced
34-
// by spurious recv timeouts. Only tests exercising recv timeouts specifically
35-
// pass a "reasoanble" value here.
36-
fn spawn_driver(required_rx: Duration) -> Harness {
37-
let (listener_tx, listener_rx) = mpsc::channel(8);
38-
let (egress_tx, egress_rx) = mpsc::channel(EGRESS_CHANNEL_DEPTH);
39-
40-
let sm = StateMachine::start(
41-
PeerInfo::with_random_discriminator(
42-
required_rx,
43-
DEFAULT_DETECT_MULTIPLIER,
44-
),
45-
Instant::now(),
46-
);
47-
let (state_tx, state_rx) = watch::channel(sm.state());
48-
let counters = Arc::new(SessionCounters::default());
49-
50-
let driver = tokio::spawn(
51-
DriverTask {
52-
sm,
53-
counters: Arc::clone(&counters),
54-
remote_addr: sockaddr!("127.0.0.1:3784"),
55-
listener_rx,
56-
egress_tx,
57-
state_tx,
58-
log: Logger::root(Discard, o!()),
31+
impl Harness {
32+
// Spawn a driver task and wrap the extra bits we need in a test harness.
33+
//
34+
// Most tests pass a very large `required_rx` to avoid any flakiness
35+
// introduced by spurious recv timeouts. Only tests exercising recv timeouts
36+
// specifically pass a "reasoanble" value here.
37+
fn spawn(required_rx: Duration) -> Self {
38+
let (listener_tx, listener_rx) = mpsc::channel(8);
39+
let (egress_tx, egress_rx) = mpsc::channel(EGRESS_CHANNEL_DEPTH);
40+
41+
let sm = StateMachine::start(
42+
PeerInfo::with_random_discriminator(
43+
required_rx,
44+
DEFAULT_DETECT_MULTIPLIER,
45+
),
46+
Instant::now(),
47+
);
48+
let (state_tx, state_rx) = watch::channel(sm.state());
49+
let counters = Arc::new(SessionCounters::default());
50+
51+
let driver = tokio::spawn(
52+
DriverTask {
53+
sm,
54+
counters: Arc::clone(&counters),
55+
remote_addr: sockaddr!("127.0.0.1:3784"),
56+
listener_rx,
57+
egress_tx,
58+
state_tx,
59+
log: Logger::root(Discard, o!()),
60+
}
61+
.run(),
62+
);
63+
64+
Self {
65+
driver,
66+
listener_tx,
67+
egress_rx,
68+
state_rx,
69+
counters,
5970
}
60-
.run(),
61-
);
62-
63-
Harness {
64-
driver,
65-
listener_tx,
66-
egress_rx,
67-
state_rx,
68-
counters,
6971
}
7072
}
7173

@@ -105,7 +107,7 @@ async fn bring_to_up(h: &Harness) {
105107

106108
#[tokio::test(flavor = "multi_thread")]
107109
async fn exits_when_listener_channel_closes() {
108-
let h = spawn_driver(Duration::from_secs(3600));
110+
let h = Harness::spawn(Duration::from_secs(3600));
109111
drop(h.listener_tx);
110112
timeout(Duration::from_secs(5), h.driver)
111113
.await
@@ -115,7 +117,7 @@ async fn exits_when_listener_channel_closes() {
115117

116118
#[tokio::test(flavor = "multi_thread")]
117119
async fn emits_outgoing_control_packet() {
118-
let mut h = spawn_driver(Duration::from_secs(3600));
120+
let mut h = Harness::spawn(Duration::from_secs(3600));
119121
// A freshly-started session has a packet to send immediately, so the driver
120122
// should hand a valid control packet to the egress channel.
121123
let bytes = timeout(Duration::from_secs(5), h.egress_rx.recv())
@@ -127,7 +129,7 @@ async fn emits_outgoing_control_packet() {
127129

128130
#[tokio::test(flavor = "multi_thread")]
129131
async fn counts_incoming_packets() {
130-
let h = spawn_driver(Duration::from_secs(3600));
132+
let h = Harness::spawn(Duration::from_secs(3600));
131133

132134
// `Control::default()` carries the peer's `Down` state.
133135
h.listener_tx.send(Control::default()).await.unwrap();
@@ -142,7 +144,7 @@ async fn counts_incoming_packets() {
142144

143145
#[tokio::test(flavor = "multi_thread")]
144146
async fn handshake_down_init_up_publishes_each_state() {
145-
let h = spawn_driver(Duration::from_secs(3600));
147+
let h = Harness::spawn(Duration::from_secs(3600));
146148

147149
// Down: peer Down -> we go Init.
148150
h.listener_tx
@@ -163,7 +165,7 @@ async fn handshake_down_init_up_publishes_each_state() {
163165

164166
#[tokio::test(flavor = "multi_thread")]
165167
async fn down_to_up_directly() {
166-
let h = spawn_driver(Duration::from_secs(3600));
168+
let h = Harness::spawn(Duration::from_secs(3600));
167169

168170
// From Down, a peer advertising Init takes us straight to Up.
169171
h.listener_tx
@@ -177,7 +179,7 @@ async fn down_to_up_directly() {
177179

178180
#[tokio::test(flavor = "multi_thread")]
179181
async fn up_to_down_on_peer_down() {
180-
let h = spawn_driver(Duration::from_secs(3600));
182+
let h = Harness::spawn(Duration::from_secs(3600));
181183
bring_to_up(&h).await;
182184

183185
h.listener_tx
@@ -190,7 +192,7 @@ async fn up_to_down_on_peer_down() {
190192

191193
#[tokio::test(flavor = "multi_thread")]
192194
async fn up_to_down_on_peer_admin_down() {
193-
let h = spawn_driver(Duration::from_secs(3600));
195+
let h = Harness::spawn(Duration::from_secs(3600));
194196
bring_to_up(&h).await;
195197

196198
h.listener_tx
@@ -202,7 +204,7 @@ async fn up_to_down_on_peer_admin_down() {
202204

203205
#[tokio::test(flavor = "multi_thread")]
204206
async fn init_to_down_on_peer_admin_down() {
205-
let h = spawn_driver(Duration::from_secs(3600));
207+
let h = Harness::spawn(Duration::from_secs(3600));
206208

207209
h.listener_tx
208210
.send(peer_packet(BfdPeerState::Down))
@@ -219,7 +221,7 @@ async fn init_to_down_on_peer_admin_down() {
219221

220222
#[tokio::test(flavor = "multi_thread")]
221223
async fn peer_state_that_does_not_advance_is_a_no_op() {
222-
let h = spawn_driver(Duration::from_secs(3600));
224+
let h = Harness::spawn(Duration::from_secs(3600));
223225

224226
// From Down, a peer advertising Up is not a valid advance; we stay Down.
225227
h.listener_tx
@@ -242,7 +244,7 @@ async fn peer_state_that_does_not_advance_is_a_no_op() {
242244
async fn recv_timeout_drives_session_down() {
243245
// ~1.5s detection time (500ms * 3): long enough not to fire during the
244246
// sub-100ms bring-up, short enough to fire well within the 5s wait below.
245-
let h = spawn_driver(Duration::from_millis(500));
247+
let h = Harness::spawn(Duration::from_millis(500));
246248
bring_to_up(&h).await;
247249

248250
// Stop sending; the driver's recv deadline should elapse and transition
@@ -254,7 +256,7 @@ async fn recv_timeout_drives_session_down() {
254256

255257
#[tokio::test(flavor = "multi_thread")]
256258
async fn poll_packet_elicits_final_response_on_egress() {
257-
let mut h = spawn_driver(Duration::from_secs(3600));
259+
let mut h = Harness::spawn(Duration::from_secs(3600));
258260

259261
let mut pkt = peer_packet(BfdPeerState::Down);
260262
pkt.set_poll();

0 commit comments

Comments
 (0)