Skip to content

Commit 0598e2f

Browse files
committed
always return a listening task, even in tests
1 parent 16f0bf3 commit 0598e2f

4 files changed

Lines changed: 28 additions & 27 deletions

File tree

bfd-async/src/dispatcher.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,14 +191,12 @@ struct Listener {
191191
/// listening for and the channel on which we should send any incoming
192192
/// packets from that peer.
193193
sessions: SharedSessions,
194-
listen_task: Option<JoinHandle<()>>,
194+
listen_task: JoinHandle<()>,
195195
}
196196

197197
impl Drop for Listener {
198198
fn drop(&mut self) {
199-
if let Some(listen_task) = self.listen_task.take() {
200-
listen_task.abort();
201-
}
199+
self.listen_task.abort();
202200
}
203201
}
204202

@@ -223,12 +221,10 @@ impl Listener {
223221
}
224222

225223
async fn shutdown(mut self) {
226-
if let Some(listen_task) = self.listen_task.take() {
227-
listen_task.abort();
228-
// Discard the result; this can only fail if the listen task
229-
// panicked, but we're shutting it down anyway.
230-
let _: Result<_, _> = listen_task.await;
231-
}
224+
self.listen_task.abort();
225+
// Discard the result; this can only fail if the listen task
226+
// panicked, but we're shutting it down anyway.
227+
let _: Result<_, _> = (&mut self.listen_task).await;
232228
}
233229

234230
fn remove_peer(&self, peer: IpAddr) -> ListenerRemovePeerResult {
@@ -269,15 +265,13 @@ trait ListenerBackend: Send + Sync + 'static {
269265
/// Bind a socket at `listen_addr` and spawn a task that reads packets and
270266
/// dispatches them to the per-peer channels in `sessions`.
271267
///
272-
/// Returns the spawned task's handle, or `None` for test fakes that don't
273-
/// bind a real socket. A `Listener` holding `None` is shut down/dropped as
274-
/// a no-op.
268+
/// Returns the spawned task's handle.
275269
fn spawn(
276270
&self,
277271
listen_addr: SocketAddr,
278272
sessions: SharedSessions,
279273
log: Logger,
280-
) -> Result<Option<JoinHandle<()>>, AddPeerError>;
274+
) -> Result<JoinHandle<()>, AddPeerError>;
281275
}
282276

283277
/// Production [`ListenerBackend`]: binds a real UDP socket and spawns a
@@ -290,7 +284,7 @@ impl ListenerBackend for TokioUdpBinder {
290284
listen_addr: SocketAddr,
291285
sessions: SharedSessions,
292286
log: Logger,
293-
) -> Result<Option<JoinHandle<()>>, AddPeerError> {
287+
) -> Result<JoinHandle<()>, AddPeerError> {
294288
// This is pretty spicy: we're using std's `UdpSocket` so we can
295289
// _synchronously_ bind, even though this function is ultimately called
296290
// from an async context. The arguments for this are:
@@ -313,7 +307,7 @@ impl ListenerBackend for TokioUdpBinder {
313307
UdpSocket::from_std(socket).map_err(AddPeerError::StdToTokio)?;
314308

315309
let listen_task = ListenerTask::new(socket, sessions, log);
316-
Ok(Some(tokio::spawn(listen_task.run())))
310+
Ok(tokio::spawn(listen_task.run()))
317311
}
318312
}
319313

bfd-async/src/dispatcher/end_to_end.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,15 @@ impl ListenerBackend for PreBoundSocketBackend {
7777
_listen_addr: SocketAddr,
7878
sessions: SharedSessions,
7979
log: Logger,
80-
) -> Result<Option<JoinHandle<()>>, AddPeerError> {
80+
) -> Result<JoinHandle<()>, AddPeerError> {
8181
let socket = self
8282
.socket
8383
.lock()
8484
.unwrap()
8585
.take()
8686
.expect("PreBoundSocketBackend::spawn called more than once");
8787
let listen_task = ListenerTask::new(socket, sessions, log);
88-
Ok(Some(tokio::spawn(listen_task.run())))
88+
Ok(tokio::spawn(listen_task.run()))
8989
}
9090
}
9191

bfd-async/src/dispatcher/proptests.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl ListenerBackend for FakeBackend {
4141
listen_addr: SocketAddr,
4242
_sessions: SharedSessions,
4343
_log: Logger,
44-
) -> Result<Option<JoinHandle<()>>, AddPeerError> {
44+
) -> Result<JoinHandle<()>, AddPeerError> {
4545
if self.fail_addrs.contains(&listen_addr) {
4646
Err(AddPeerError::Bind {
4747
addr: listen_addr,
@@ -51,10 +51,9 @@ impl ListenerBackend for FakeBackend {
5151
),
5252
})
5353
} else {
54-
// No real socket and no task: the dispatcher only needs the
55-
// bookkeeping, and a `Listener` holding `None` shuts down as a
56-
// no-op.
57-
Ok(None)
54+
// No real socket and no meaningful task: the dispatcher only needs
55+
// the bookkeeping.
56+
Ok(tokio::spawn(async {}))
5857
}
5958
}
6059
}
@@ -211,7 +210,11 @@ fn run(ops: Vec<Op>, fail_addrs: HashSet<SocketAddr>) {
211210

212211
#[proptest]
213212
fn state_machine(#[strategy(vec(any::<Op>(), 0..40))] ops: Vec<Op>) {
214-
run(ops, HashSet::new());
213+
// `run()` isn't itself async, but it does spawn (no-op) tokio tasks so
214+
// needs to execute in the context of a runtime.
215+
tokio::runtime::Runtime::new().unwrap().block_on(async {
216+
run(ops, HashSet::new());
217+
});
215218
}
216219

217220
#[proptest]
@@ -220,5 +223,9 @@ fn state_machine_with_bind_failures(
220223
#[strategy(proptest::collection::hash_set(arb_listen_addr(), 0..=3))]
221224
fail_addrs: HashSet<SocketAddr>,
222225
) {
223-
run(ops, fail_addrs);
226+
// `run()` isn't itself async, but it does spawn (no-op) tokio tasks so
227+
// needs to execute in the context of a runtime.
228+
tokio::runtime::Runtime::new().unwrap().block_on(async {
229+
run(ops, fail_addrs);
230+
});
224231
}

bfd-async/src/dispatcher/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl ListenerBackend for TestBackend {
6868
listen_addr: SocketAddr,
6969
sessions: super::SharedSessions,
7070
log: Logger,
71-
) -> Result<Option<tokio::task::JoinHandle<()>>, crate::AddPeerError> {
71+
) -> Result<tokio::task::JoinHandle<()>, crate::AddPeerError> {
7272
assert_eq!(listen_addr, LISTEN_ADDR);
7373

7474
let mut bound_address = self.bound_address.lock().unwrap();
@@ -103,7 +103,7 @@ impl ListenerBackend for TestBackend {
103103
}
104104
});
105105

106-
Ok(Some(listen_task))
106+
Ok(listen_task)
107107
}
108108
}
109109

0 commit comments

Comments
 (0)