Skip to content

Commit 812a7cd

Browse files
hanabi1224jxs
andauthored
feat: make runtime features optional in swarm-test (#5551)
## Description <!-- Please write a summary of your changes and why you made them. This section will appear as the commit message after merging. Please craft it accordingly. For a quick primer on good commit messages, check out this blog post: https://cbea.ms/git-commit/ Please include any relevant issues in here, for example: Related https://github.com/libp2p/rust-libp2p/issues/ABCD. Fixes https://github.com/libp2p/rust-libp2p/issues/XYZ. --> Sometimes a test uses custom swarm building logic and doesn't need `fn new_ephemeral`, and sometimes a test uses `tokio` runtime other than `async-std`. This PR adds the `tokio` runtime support and makes both `async-std` and `tokio` runtimes optional behind features to make it more flexible. ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR which don't need to go into the final commit message. --> ## Change checklist <!-- Please add a Changelog entry in the appropriate crates and bump the crate versions if needed. See <https://github.com/libp2p/rust-libp2p/blob/master/docs/release.md#development-between-releases>--> - [x] I have performed a self-review of my own code - [x] I have made corresponding changes to the documentation - [ ] I have added tests that prove my fix is effective or that my feature works - [x] A changelog entry has been made in the appropriate crates --------- Co-authored-by: João Oliveira <[email protected]>
1 parent d3228ad commit 812a7cd

File tree

5 files changed

+72
-20
lines changed

5 files changed

+72
-20
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ libp2p-server = { version = "0.12.8", path = "misc/server" }
104104
libp2p-stream = { version = "0.2.0-alpha", path = "protocols/stream" }
105105
libp2p-swarm = { version = "0.45.2", path = "swarm" }
106106
libp2p-swarm-derive = { version = "=0.35.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required.
107-
libp2p-swarm-test = { version = "0.4.0", path = "swarm-test" }
107+
libp2p-swarm-test = { version = "0.4.1", path = "swarm-test" }
108108
libp2p-tcp = { version = "0.42.0", path = "transports/tcp" }
109109
libp2p-tls = { version = "0.5.0", path = "transports/tls" }
110110
libp2p-uds = { version = "0.41.0", path = "transports/uds" }

swarm-test/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.4.1
2+
3+
- Add `tokio` runtime support and make `tokio` and `async-std` runtimes optional behind features.
4+
See [PR 5551].
5+
6+
[PR 5551]: https://github.com/libp2p/rust-libp2p/pull/5551
7+
18
## 0.4.0
29

310
<!-- Update to libp2p-swarm v0.45.0 -->

swarm-test/Cargo.toml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libp2p-swarm-test"
3-
version = "0.4.0"
3+
version = "0.4.1"
44
edition = "2021"
55
rust-version = { workspace = true }
66
license = "MIT"
@@ -16,13 +16,19 @@ async-trait = "0.1.80"
1616
libp2p-core = { workspace = true }
1717
libp2p-identity = { workspace = true, features = ["rand"] }
1818
libp2p-plaintext = { workspace = true }
19-
libp2p-swarm = { workspace = true, features = ["async-std"] }
20-
libp2p-tcp = { workspace = true, features = ["async-io"] }
19+
libp2p-swarm = { workspace = true }
20+
libp2p-tcp = { workspace = true }
2121
libp2p-yamux = { workspace = true }
2222
futures = { workspace = true }
2323
rand = "0.8.5"
2424
tracing = { workspace = true }
2525
futures-timer = "3.0.3"
2626

27+
[features]
28+
default = ["async-std"]
29+
30+
async-std = ["libp2p-swarm/async-std", "libp2p-tcp/async-io"]
31+
tokio = ["libp2p-swarm/tokio", "libp2p-tcp/tokio"]
32+
2733
[lints]
2834
workspace = true

swarm-test/src/lib.rs

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,10 @@
2121
use async_trait::async_trait;
2222
use futures::future::{BoxFuture, Either};
2323
use futures::{FutureExt, StreamExt};
24-
use libp2p_core::{
25-
multiaddr::Protocol, transport::MemoryTransport, upgrade::Version, Multiaddr, Transport,
26-
};
27-
use libp2p_identity::{Keypair, PeerId};
28-
use libp2p_plaintext as plaintext;
24+
use libp2p_core::{multiaddr::Protocol, Multiaddr};
25+
use libp2p_identity::PeerId;
2926
use libp2p_swarm::dial_opts::PeerCondition;
30-
use libp2p_swarm::{self as swarm, dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent};
31-
use libp2p_yamux as yamux;
27+
use libp2p_swarm::{dial_opts::DialOpts, NetworkBehaviour, Swarm, SwarmEvent};
3228
use std::fmt::Debug;
3329
use std::future::IntoFuture;
3430
use std::time::Duration;
@@ -38,12 +34,23 @@ use std::time::Duration;
3834
pub trait SwarmExt {
3935
type NB: NetworkBehaviour;
4036

41-
/// Create a new [`Swarm`] with an ephemeral identity.
37+
/// Create a new [`Swarm`] with an ephemeral identity and the `async-std` runtime.
4238
///
43-
/// The swarm will use a [`MemoryTransport`] together with a [`plaintext::Config`] authentication layer and
44-
/// [`yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test
39+
/// The swarm will use a [`libp2p_core::transport::MemoryTransport`] together with a [`libp2p_plaintext::Config`] authentication layer and
40+
/// [`libp2p_yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test
4541
/// and may change at any time.
46-
fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self
42+
#[cfg(feature = "async-std")]
43+
fn new_ephemeral(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self
44+
where
45+
Self: Sized;
46+
47+
/// Create a new [`Swarm`] with an ephemeral identity and the `tokio` runtime.
48+
///
49+
/// The swarm will use a [`libp2p_core::transport::MemoryTransport`] together with a [`libp2p_plaintext::Config`] authentication layer and
50+
/// [`libp2p_yamux::Config`] as the multiplexer. However, these details should not be relied upon by the test
51+
/// and may change at any time.
52+
#[cfg(feature = "tokio")]
53+
fn new_ephemeral_tokio(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self
4754
where
4855
Self: Sized;
4956

@@ -200,26 +207,58 @@ where
200207
{
201208
type NB = B;
202209

203-
fn new_ephemeral(behaviour_fn: impl FnOnce(Keypair) -> Self::NB) -> Self
210+
#[cfg(feature = "async-std")]
211+
fn new_ephemeral(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self
204212
where
205213
Self: Sized,
206214
{
215+
use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _};
216+
use libp2p_identity::Keypair;
217+
207218
let identity = Keypair::generate_ed25519();
208219
let peer_id = PeerId::from(identity.public());
209220

210221
let transport = MemoryTransport::default()
211222
.or_transport(libp2p_tcp::async_io::Transport::default())
212223
.upgrade(Version::V1)
213-
.authenticate(plaintext::Config::new(&identity))
214-
.multiplex(yamux::Config::default())
224+
.authenticate(libp2p_plaintext::Config::new(&identity))
225+
.multiplex(libp2p_yamux::Config::default())
226+
.timeout(Duration::from_secs(20))
227+
.boxed();
228+
229+
Swarm::new(
230+
transport,
231+
behaviour_fn(identity),
232+
peer_id,
233+
libp2p_swarm::Config::with_async_std_executor()
234+
.with_idle_connection_timeout(Duration::from_secs(5)), // Some tests need connections to be kept alive beyond what the individual behaviour configures.,
235+
)
236+
}
237+
238+
#[cfg(feature = "tokio")]
239+
fn new_ephemeral_tokio(behaviour_fn: impl FnOnce(libp2p_identity::Keypair) -> Self::NB) -> Self
240+
where
241+
Self: Sized,
242+
{
243+
use libp2p_core::{transport::MemoryTransport, upgrade::Version, Transport as _};
244+
use libp2p_identity::Keypair;
245+
246+
let identity = Keypair::generate_ed25519();
247+
let peer_id = PeerId::from(identity.public());
248+
249+
let transport = MemoryTransport::default()
250+
.or_transport(libp2p_tcp::tokio::Transport::default())
251+
.upgrade(Version::V1)
252+
.authenticate(libp2p_plaintext::Config::new(&identity))
253+
.multiplex(libp2p_yamux::Config::default())
215254
.timeout(Duration::from_secs(20))
216255
.boxed();
217256

218257
Swarm::new(
219258
transport,
220259
behaviour_fn(identity),
221260
peer_id,
222-
swarm::Config::with_async_std_executor()
261+
libp2p_swarm::Config::with_tokio_executor()
223262
.with_idle_connection_timeout(Duration::from_secs(5)), // Some tests need connections to be kept alive beyond what the individual behaviour configures.,
224263
)
225264
}

0 commit comments

Comments
 (0)