-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmultiple_conns.rs
63 lines (53 loc) · 1.63 KB
/
multiple_conns.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::env::temp_dir;
use busd::bus::Bus;
use futures_util::future::join_all;
use ntest::timeout;
use rand::{
distr::{Alphanumeric, SampleString},
rng,
};
use tokio::{select, sync::oneshot::channel};
use tracing::instrument;
use zbus::connection;
#[tokio::test(flavor = "multi_thread", worker_threads = 8)]
#[instrument]
#[timeout(15000)]
async fn multi_conenct() {
busd::tracing_subscriber::init();
// Unix socket
let s = Alphanumeric.sample_string(&mut rng(), 10);
let path = temp_dir().join(s);
let address = format!("unix:path={}", path.display());
multi_conenct_(&address).await;
// TCP socket
let address = "tcp:host=127.0.0.1,port=4246".to_string();
multi_conenct_(&address).await;
}
async fn multi_conenct_(socket_addr: &str) {
let mut bus = Bus::for_address(Some(socket_addr)).await.unwrap();
let (tx, rx) = channel();
let handle = tokio::spawn(async move {
select! {
_ = rx => (),
res = bus.run() => match res {
Ok(()) => panic!("Bus exited unexpectedly"),
Err(e) => panic!("Bus exited with an error: {}", e),
}
}
bus
});
let ret = multi_clients_connect(socket_addr).await;
let _ = tx.send(());
let bus = handle.await.unwrap();
bus.cleanup().await.unwrap();
ret.unwrap();
}
#[instrument]
async fn multi_clients_connect(socket_addr: &str) -> anyhow::Result<()> {
// Create 10 connections simultaneously.
let conns: Vec<_> = (0..10)
.map(|_| connection::Builder::address(socket_addr).unwrap().build())
.collect();
join_all(conns).await;
Ok(())
}