Skip to content

Commit d3d5359

Browse files
authored
Merge pull request #29 from bk-rs/add-tests
Add tests for tokio::spawn session
2 parents df576b7 + f6c6a1f commit d3d5359

File tree

3 files changed

+112
-5
lines changed

3 files changed

+112
-5
lines changed

async-ssh2-lite/tests/integration_tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@ mod integration_tests {
3131

3232
#[cfg(test)]
3333
mod sftp;
34+
35+
#[cfg(test)]
36+
mod tokio_spawn_session;
3437
}

async-ssh2-lite/tests/integration_tests/channel__exec.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,34 @@ async fn __run__session__channel_session__exec<S: AsyncSessionStream + Send + Sy
4545
channel.exec("hostname").await?;
4646
let mut s = String::new();
4747
channel.read_to_string(&mut s).await?;
48-
println!("exec hostname output:{s}");
48+
println!("channel__exec exec hostname output:{s}");
4949
channel.close().await?;
50-
println!("exec hostname exit_status:{}", channel.exit_status()?);
50+
println!(
51+
"channel__exec exec hostname exit_status:{}",
52+
channel.exit_status()?
53+
);
5154

5255
let mut channel = session.channel_session().await?;
5356
channel.exec("date").await?;
5457
let mut s = String::new();
5558
channel.read_to_string(&mut s).await?;
56-
println!("exec date output:{s}");
59+
println!("channel__exec exec date output:{s}");
5760
channel.close().await?;
58-
println!("exec date exit_status:{}", channel.exit_status()?);
61+
println!(
62+
"channel__exec exec date exit_status:{}",
63+
channel.exit_status()?
64+
);
5965

6066
let mut channel = session.channel_session().await?;
6167
channel.exec("head -c 16354 /dev/random").await?;
6268
let mut b = vec![];
6369
channel.read_to_end(&mut b).await?;
6470
assert_eq!(b.len(), 16354);
6571
channel.close().await?;
66-
println!("exec head exit_status:{}", channel.exit_status()?);
72+
println!(
73+
"channel__exec exec head exit_status:{}",
74+
channel.exit_status()?
75+
);
6776

6877
Ok(())
6978
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#![cfg(feature = "tokio")]
2+
3+
use std::{error, sync::Arc};
4+
5+
use async_ssh2_lite::{AsyncSession, AsyncSessionStream};
6+
use futures_util::future::join_all;
7+
#[cfg(not(feature = "_integration_tests_tokio_ext"))]
8+
use futures_util::AsyncReadExt as _;
9+
#[cfg(feature = "_integration_tests_tokio_ext")]
10+
use tokio::io::AsyncReadExt as _;
11+
12+
use super::{
13+
helpers::get_connect_addr, session__userauth_pubkey::__run__session__userauth_pubkey_file,
14+
};
15+
16+
//
17+
#[tokio::test]
18+
async fn simple_with_tokio() -> Result<(), Box<dyn error::Error>> {
19+
let mut session =
20+
AsyncSession::<async_ssh2_lite::TokioTcpStream>::connect(get_connect_addr()?, None).await?;
21+
__run__session__userauth_pubkey_file(&mut session).await?;
22+
let session = Arc::new(session);
23+
24+
let mut handles = vec![];
25+
for i in 0..10 {
26+
let session = session.clone();
27+
let handle = tokio::spawn(async move {
28+
__run__session__channel_session__exec(&session, i)
29+
.await
30+
.unwrap();
31+
});
32+
handles.push(handle);
33+
}
34+
35+
for handle in handles {
36+
handle.await.unwrap();
37+
}
38+
39+
Ok(())
40+
}
41+
42+
//
43+
#[tokio::test]
44+
async fn concurrently_with_tokio() -> Result<(), Box<dyn error::Error>> {
45+
let mut session =
46+
AsyncSession::<async_ssh2_lite::TokioTcpStream>::connect(get_connect_addr()?, None).await?;
47+
__run__session__userauth_pubkey_file(&mut session).await?;
48+
let session = Arc::new(session);
49+
50+
let mut handles = vec![];
51+
for i in 0..10 {
52+
let session = session.clone();
53+
let handle = tokio::spawn(async move {
54+
__run__session__channel_session__exec(&session, i)
55+
.await
56+
.unwrap();
57+
});
58+
handles.push(handle);
59+
}
60+
61+
let rets = join_all(handles).await;
62+
println!("tokio_spawn_session concurrently rets:{rets:?}");
63+
assert!(rets.iter().all(|x| x.is_ok()));
64+
65+
Ok(())
66+
}
67+
68+
async fn __run__session__channel_session__exec<S: AsyncSessionStream + Send + Sync + 'static>(
69+
session: &AsyncSession<S>,
70+
i: usize,
71+
) -> Result<(), Box<dyn error::Error>> {
72+
let mut channel = session.channel_session().await?;
73+
channel.exec("hostname").await?;
74+
let mut s = String::new();
75+
channel.read_to_string(&mut s).await?;
76+
println!("tokio_spawn_session exec hostname output:{s} i:{i}");
77+
channel.close().await?;
78+
println!(
79+
"tokio_spawn_session exec hostname exit_status:{} i:{i}",
80+
channel.exit_status()?
81+
);
82+
83+
let mut channel = session.channel_session().await?;
84+
channel.exec("head -c 16354 /dev/random").await?;
85+
let mut b = vec![];
86+
channel.read_to_end(&mut b).await?;
87+
assert_eq!(b.len(), 16354);
88+
channel.close().await?;
89+
println!(
90+
"tokio_spawn_session exec head exit_status:{} i:{i}",
91+
channel.exit_status()?
92+
);
93+
94+
Ok(())
95+
}

0 commit comments

Comments
 (0)