mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-01 20:21:21 +00:00
refactor(deflate): implement tests with futures_ringbuf
Previously, we used a TCP transport to test the deflate compression. Really, all we need is an in-memory ringbuffer that we can plug in at both ends. This PR uses `futures_ringbuf` for that. Related #3748. Pull-Request: #3771.
This commit is contained in:
parent
ed392af864
commit
7fa8beff21
96
Cargo.lock
generated
96
Cargo.lock
generated
@ -631,6 +631,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cache-padded"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c1db59621ec70f09c5e9b597b220c7a2b43611f4710dc03ceb8748637775692c"
|
||||
|
||||
[[package]]
|
||||
name = "cast"
|
||||
version = "0.3.0"
|
||||
@ -785,7 +791,7 @@ dependencies = [
|
||||
"anstyle",
|
||||
"bitflags",
|
||||
"clap_lex 0.4.1",
|
||||
"strsim",
|
||||
"strsim 0.10.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1126,14 +1132,38 @@ dependencies = [
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
|
||||
dependencies = [
|
||||
"darling_core 0.10.2",
|
||||
"darling_macro 0.10.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling"
|
||||
version = "0.14.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b0dd3cd20dc6b5a876612a6e5accfe7f3dd883db6d07acfbf14c128f61550dfa"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_macro",
|
||||
"darling_core 0.14.2",
|
||||
"darling_macro 0.14.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_core"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
|
||||
dependencies = [
|
||||
"fnv",
|
||||
"ident_case",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"strsim 0.9.3",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1146,7 +1176,18 @@ dependencies = [
|
||||
"ident_case",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"strsim",
|
||||
"strsim 0.10.0",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_macro"
|
||||
version = "0.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
|
||||
dependencies = [
|
||||
"darling_core 0.10.2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
@ -1156,7 +1197,7 @@ version = "0.14.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7618812407e9402654622dd402b0a89dff9ba93badd6540781526117b92aab7e"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_core 0.14.2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
@ -1253,7 +1294,7 @@ version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1f91d4cfa921f1c05904dc3c57b4a32c38aed3340cce209f3a6fd1478babafc4"
|
||||
dependencies = [
|
||||
"darling",
|
||||
"darling 0.14.2",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
@ -1646,6 +1687,19 @@ dependencies = [
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures_ringbuf"
|
||||
version = "0.3.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b905098b5519bd63b2a1f9f4615198b0e38a473ce201ffdbd4dea6eb63087ddc"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"log",
|
||||
"log-derive",
|
||||
"ringbuf",
|
||||
"rustc_version",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.6"
|
||||
@ -2384,11 +2438,10 @@ dependencies = [
|
||||
name = "libp2p-deflate"
|
||||
version = "0.39.0"
|
||||
dependencies = [
|
||||
"async-std",
|
||||
"flate2",
|
||||
"futures",
|
||||
"futures_ringbuf",
|
||||
"libp2p-core",
|
||||
"libp2p-tcp",
|
||||
"quickcheck-ext",
|
||||
"rand 0.8.5",
|
||||
]
|
||||
@ -3125,6 +3178,18 @@ dependencies = [
|
||||
"value-bag",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log-derive"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6a42526bb432bcd1b43571d5f163984effa25409a29f1a3242a54d0577d55bcf"
|
||||
dependencies = [
|
||||
"darling 0.10.2",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lru"
|
||||
version = "0.10.0"
|
||||
@ -4165,6 +4230,15 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ringbuf"
|
||||
version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f65af18d50f789e74aaf23bbb3f65dcd22a3cb6e029b5bced149f6bd57c5c2a2"
|
||||
dependencies = [
|
||||
"cache-padded",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rmp"
|
||||
version = "0.8.11"
|
||||
@ -4631,6 +4705,12 @@ version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.10.0"
|
||||
|
@ -16,10 +16,9 @@ libp2p-core = { version = "0.39.0", path = "../../core" }
|
||||
flate2 = "1.0"
|
||||
|
||||
[dev-dependencies]
|
||||
async-std = "1.6.2"
|
||||
libp2p-tcp = { path = "../tcp", features = ["async-io"] }
|
||||
quickcheck = { package = "quickcheck-ext", path = "../../misc/quickcheck-ext" }
|
||||
rand = "0.8"
|
||||
futures_ringbuf = "0.3.1"
|
||||
|
||||
# Passing arguments to the docsrs builder in order to properly document cfg's.
|
||||
# More information: https://docs.rs/about/builds#cross-compiling
|
||||
|
@ -18,10 +18,9 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use futures::{future, prelude::*};
|
||||
use libp2p_core::{transport::Transport, upgrade};
|
||||
use futures::prelude::*;
|
||||
use libp2p_core::OutboundUpgrade;
|
||||
use libp2p_deflate::DeflateConfig;
|
||||
use libp2p_tcp as tcp;
|
||||
use quickcheck::{QuickCheck, TestResult};
|
||||
use rand::RngCore;
|
||||
|
||||
@ -31,7 +30,7 @@ fn deflate() {
|
||||
if message.is_empty() {
|
||||
return TestResult::discard();
|
||||
}
|
||||
async_std::task::block_on(run(message));
|
||||
futures::executor::block_on(run(message));
|
||||
TestResult::passed()
|
||||
}
|
||||
QuickCheck::new().quickcheck(prop as fn(Vec<u8>) -> TestResult)
|
||||
@ -41,68 +40,41 @@ fn deflate() {
|
||||
fn lot_of_data() {
|
||||
let mut v = vec![0; 2 * 1024 * 1024];
|
||||
rand::thread_rng().fill_bytes(&mut v);
|
||||
async_std::task::block_on(run(v))
|
||||
futures::executor::block_on(run(v));
|
||||
}
|
||||
|
||||
async fn run(message1: Vec<u8>) {
|
||||
let new_transport = || {
|
||||
tcp::async_io::Transport::default()
|
||||
.and_then(|conn, endpoint| {
|
||||
upgrade::apply(
|
||||
conn,
|
||||
DeflateConfig::default(),
|
||||
endpoint,
|
||||
upgrade::Version::V1,
|
||||
)
|
||||
})
|
||||
.boxed()
|
||||
};
|
||||
let mut listener_transport = new_transport();
|
||||
listener_transport
|
||||
.listen_on("/ip4/0.0.0.0/tcp/0".parse().expect("multiaddr"))
|
||||
.expect("listener");
|
||||
|
||||
let listen_addr = listener_transport
|
||||
.next()
|
||||
.await
|
||||
.expect("some event")
|
||||
.into_new_address()
|
||||
.expect("new address");
|
||||
let (server, client) = futures_ringbuf::Endpoint::pair(100, 100);
|
||||
|
||||
let message2 = message1.clone();
|
||||
|
||||
let listener_task = async_std::task::spawn(async move {
|
||||
let mut conn = listener_transport
|
||||
.filter(|e| future::ready(e.is_upgrade()))
|
||||
.next()
|
||||
let client_task = async move {
|
||||
let mut client = DeflateConfig::default()
|
||||
.upgrade_outbound(client, b"")
|
||||
.await
|
||||
.expect("some event")
|
||||
.into_incoming()
|
||||
.expect("upgrade")
|
||||
.0
|
||||
.await
|
||||
.expect("connection");
|
||||
.unwrap();
|
||||
|
||||
let mut buf = vec![0; message2.len()];
|
||||
conn.read_exact(&mut buf).await.expect("read_exact");
|
||||
client.read_exact(&mut buf).await.expect("read_exact");
|
||||
assert_eq!(&buf[..], &message2[..]);
|
||||
|
||||
conn.write_all(&message2).await.expect("write_all");
|
||||
conn.close().await.expect("close")
|
||||
});
|
||||
client.write_all(&message2).await.expect("write_all");
|
||||
client.close().await.expect("close")
|
||||
};
|
||||
|
||||
let mut dialer_transport = new_transport();
|
||||
let mut conn = dialer_transport
|
||||
.dial(listen_addr)
|
||||
.expect("dialer")
|
||||
.await
|
||||
.expect("connection");
|
||||
conn.write_all(&message1).await.expect("write_all");
|
||||
conn.close().await.expect("close");
|
||||
let server_task = async move {
|
||||
let mut server = DeflateConfig::default()
|
||||
.upgrade_outbound(server, b"")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let mut buf = Vec::new();
|
||||
conn.read_to_end(&mut buf).await.expect("read_to_end");
|
||||
assert_eq!(&buf[..], &message1[..]);
|
||||
server.write_all(&message1).await.expect("write_all");
|
||||
server.close().await.expect("close");
|
||||
|
||||
listener_task.await
|
||||
let mut buf = Vec::new();
|
||||
server.read_to_end(&mut buf).await.expect("read_to_end");
|
||||
assert_eq!(&buf[..], &message1[..]);
|
||||
};
|
||||
|
||||
futures::future::join(server_task, client_task).await;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user