Skip to content

Commit cc71ca6

Browse files
committed
optimize unit test.
1 parent 8e7d276 commit cc71ca6

File tree

6 files changed

+57
-26
lines changed

6 files changed

+57
-26
lines changed

examples/echo/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::env;
77
use std::error::Error;
88

99
#[tokio::main]
10-
async fn main() -> Result<(), Box<dyn Error>> {
10+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1111
env_logger::builder().format_timestamp_millis().init();
1212
let addr = env::args()
1313
.nth(1)

examples/proxy/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rsocket_rust::prelude::*;
1010
use std::error::Error;
1111

1212
#[tokio::main]
13-
async fn main() -> Result<(), Box<dyn Error>> {
13+
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
1414
env_logger::builder().format_timestamp_millis().init();
1515

1616
RSocketFactory::receive()

src/x/client.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ impl ClientBuilder {
9494
self.responder = Some(acceptor);
9595
self
9696
}
97-
pub async fn start(self) -> Result<Client, Box<dyn Error>> {
97+
98+
pub async fn start(self) -> Result<Client, Box<dyn Error + Send + Sync>> {
9899
// TODO: process error
99100
let uri = self.uri.unwrap();
100101
match URI::parse(&uri) {
@@ -111,7 +112,7 @@ impl ClientBuilder {
111112
addr: SocketAddr,
112113
responder: Option<fn() -> Box<dyn RSocket>>,
113114
sb: SetupPayloadBuilder,
114-
) -> Result<Client, Box<dyn Error>> {
115+
) -> Result<Client, Box<dyn Error + Send + Sync>> {
115116
let socket = transport::tcp::connect(&addr);
116117
let (rcv_tx, rcv_rx) = mpsc::unbounded_channel::<Frame>();
117118
let (snd_tx, snd_rx) = mpsc::unbounded_channel::<Frame>();

src/x/server.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl ServerBuilder {
4949
self
5050
}
5151

52-
pub async fn serve(self) -> Result<(), Box<dyn Error>> {
52+
pub async fn serve(self) -> Result<(), Box<dyn Error + Send + Sync>> {
5353
// TODO: process error
5454
let s = self.uri.unwrap();
5555
match URI::parse(&s) {
@@ -66,7 +66,7 @@ impl ServerBuilder {
6666
addr: SocketAddr,
6767
on_setup: FnAcceptorWithSetup,
6868
on_start: Option<FnStart>,
69-
) -> Result<(), Box<dyn Error>> {
69+
) -> Result<(), Box<dyn Error + Send + Sync>> {
7070
let mut listener = TcpListener::bind(&addr).await.unwrap();
7171
debug!("listening on: {}", addr);
7272
if let Some(it) = on_start {

src/x/uri.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ pub(crate) enum URI {
1515
}
1616

1717
impl URI {
18-
pub(crate) fn parse(s: &str) -> Result<URI, Box<dyn Error>> {
18+
pub(crate) fn parse(s: &str) -> Result<URI, Box<dyn Error + Send + Sync>> {
1919
match Url::parse(s) {
2020
Ok(u) => Self::from_url(u),
2121
Err(e) => Err(Box::new(e)),
2222
}
2323
}
2424

2525
#[inline]
26-
fn from_url(u: Url) -> Result<URI, Box<dyn Error>> {
26+
fn from_url(u: Url) -> Result<URI, Box<dyn Error + Send + Sync>> {
2727
let domain = u.domain().unwrap_or("0.0.0.0");
2828
let schema = u.scheme();
2929
match schema.to_lowercase().as_ref() {

tests/clients.rs

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,57 @@ extern crate log;
33

44
use futures::stream;
55
use rsocket_rust::prelude::*;
6+
use std::thread::sleep;
7+
use std::time::Duration;
8+
use tokio::runtime::Runtime;
9+
10+
fn init() {
11+
let _ = env_logger::builder()
12+
.format_timestamp_millis()
13+
.is_test(true)
14+
.try_init();
15+
}
616

7-
#[tokio::main]
817
#[test]
9-
#[ignore]
10-
async fn test_client() {
11-
env_logger::builder().init();
12-
let cli = RSocketFactory::connect()
13-
.acceptor(|| Box::new(EchoRSocket))
14-
.transport("tcp://127.0.0.1:7878")
15-
.setup(Payload::from("READY!"))
16-
.mime_type("text/plain", "text/plain")
17-
.start()
18-
.await
19-
.unwrap();
18+
fn test_client() {
19+
init();
20+
21+
let server_runtime = Runtime::new().unwrap();
22+
23+
// spawn a server
24+
server_runtime.spawn(async move {
25+
RSocketFactory::receive()
26+
.transport("tcp://127.0.0.1:7878")
27+
.acceptor(|setup, _socket| {
28+
info!("accept setup: {:?}", setup);
29+
Ok(Box::new(EchoRSocket))
30+
})
31+
.on_start(|| info!("+++++++ echo server started! +++++++"))
32+
.serve()
33+
.await
34+
});
35+
36+
sleep(Duration::from_millis(500));
37+
38+
let mut client_runtime = Runtime::new().unwrap();
39+
40+
client_runtime.block_on(async {
41+
let cli = RSocketFactory::connect()
42+
.acceptor(|| Box::new(EchoRSocket))
43+
.transport("tcp://127.0.0.1:7878")
44+
.setup(Payload::from("READY!"))
45+
.mime_type("text/plain", "text/plain")
46+
.start()
47+
.await
48+
.unwrap();
2049

21-
exec_metadata_push(&cli).await;
22-
exec_fire_and_forget(&cli).await;
23-
exec_request_response(&cli).await;
24-
exec_request_stream(&cli).await;
25-
exec_request_channel(&cli).await;
26-
cli.close();
50+
exec_metadata_push(&cli).await;
51+
exec_fire_and_forget(&cli).await;
52+
exec_request_response(&cli).await;
53+
exec_request_stream(&cli).await;
54+
exec_request_channel(&cli).await;
55+
cli.close();
56+
});
2757
}
2858

2959
#[tokio::main]

0 commit comments

Comments
 (0)