fix(tcp): remove correct listener inTransport::remove_listener (#3387)

`libp2p_tcp::Transport::remove_listener` previously removed the first listener that *did not match* the provided `ListenerId`. This small fix brings it inline with other implementations.
This commit is contained in:
Jordan Santell 2023-01-27 09:32:40 -08:00 committed by GitHub
parent ab59af4d46
commit d344406235
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View File

@ -1,6 +1,9 @@
# 0.39.0 [unreleased]
- Update to `libp2p-core` `v0.39.0`.
- Fix a bug where we removed any other listener in `Transport::remove_listener` except for the one with the provided `ListenerId`. See [PR 3387].
[PR 3387]: https://github.com/libp2p/rust-libp2p/pull/3387
# 0.38.0

View File

@ -452,7 +452,7 @@ where
}
fn remove_listener(&mut self, id: ListenerId) -> bool {
if let Some(index) = self.listeners.iter().position(|l| l.listener_id != id) {
if let Some(index) = self.listeners.iter().position(|l| l.listener_id == id) {
self.listeners.remove(index);
self.pending_events
.push_back(TransportEvent::ListenerClosed {
@ -1335,4 +1335,31 @@ mod tests {
.address_translation(&quic_addr, &tcp_observed_addr)
.is_none());
}
#[test]
fn test_remove_listener() {
env_logger::try_init().ok();
async fn cycle_listeners<T: Provider>() -> bool {
let mut tcp = Transport::<T>::default().boxed();
let listener_id = tcp
.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap())
.unwrap();
tcp.remove_listener(listener_id)
}
#[cfg(feature = "async-io")]
{
assert!(async_std::task::block_on(cycle_listeners::<async_io::Tcp>()));
}
#[cfg(feature = "tokio")]
{
let rt = ::tokio::runtime::Builder::new_current_thread()
.enable_io()
.build()
.unwrap();
assert!(rt.block_on(cycle_listeners::<tokio::Tcp>()));
}
}
}