Skip to content

Commit cb4d18b

Browse files
committed
Add integration test for sync example
Adds asserts to the example project so it will fail if something isn't work between the client and server. This also adds an integration test that builds and runs the sync example so the changes to the project can be validated e2e. Signed-off-by: James Sturtevant <[email protected]>
1 parent 78de1c5 commit cb4d18b

File tree

2 files changed

+116
-22
lines changed

2 files changed

+116
-22
lines changed

example/client.rs

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515
mod protocols;
1616
mod utils;
1717

18+
use log::LevelFilter;
1819
use protocols::sync::{agent, agent_ttrpc, health, health_ttrpc};
1920
use std::thread;
2021
use ttrpc::context::{self, Context};
22+
use ttrpc::error::Error;
23+
use ttrpc::proto::Code;
2124
use ttrpc::Client;
2225

2326
fn main() {
27+
simple_logging::log_to_stderr(LevelFilter::Trace);
28+
2429
let c = Client::connect(utils::SOCK_ADDR).unwrap();
2530
let hc = health_ttrpc::HealthClient::new(c.clone());
2631
let ac = agent_ttrpc::AgentServiceClient::new(c);
@@ -33,69 +38,97 @@ fn main() {
3338
let t = thread::spawn(move || {
3439
let req = health::CheckRequest::new();
3540
println!(
36-
"OS Thread {:?} - {} started: {:?}",
41+
"OS Thread {:?} - health.check() started: {:?}",
3742
std::thread::current().id(),
38-
"health.check()",
3943
now.elapsed(),
4044
);
45+
46+
let rsp = thc.check(default_ctx(), &req);
47+
match rsp.as_ref() {
48+
Err(Error::RpcStatus(s)) => {
49+
assert_eq!(Code::NOT_FOUND, s.code());
50+
assert_eq!("Just for fun".to_string(), s.message())
51+
}
52+
Err(e) => {
53+
panic!("not expecting an error from the example server: {:?}", e)
54+
}
55+
Ok(x) => {
56+
panic!("not expecting a OK response from the example server: {:?}", x)
57+
}
58+
}
4159
println!(
42-
"OS Thread {:?} - {} -> {:?} ended: {:?}",
60+
"OS Thread {:?} - health.check() -> {:?} ended: {:?}",
4361
std::thread::current().id(),
44-
"health.check()",
45-
thc.check(default_ctx(), &req),
62+
rsp,
4663
now.elapsed(),
4764
);
4865
});
4966

5067
let t2 = thread::spawn(move || {
5168
println!(
52-
"OS Thread {:?} - {} started: {:?}",
69+
"OS Thread {:?} - agent.list_interfaces() started: {:?}",
5370
std::thread::current().id(),
54-
"agent.list_interfaces()",
5571
now.elapsed(),
5672
);
5773

5874
let show = match tac.list_interfaces(default_ctx(), &agent::ListInterfacesRequest::new()) {
59-
Err(e) => format!("{:?}", e),
60-
Ok(s) => format!("{:?}", s),
75+
Err(e) => {
76+
panic!("not expecting an error from the example server: {:?}", e)
77+
}
78+
Ok(s) => {
79+
assert_eq!("first".to_string(), s.Interfaces[0].name);
80+
assert_eq!("second".to_string(), s.Interfaces[1].name);
81+
format!("{s:?}")
82+
}
6183
};
6284

6385
println!(
64-
"OS Thread {:?} - {} -> {} ended: {:?}",
86+
"OS Thread {:?} - agent.list_interfaces() -> {} ended: {:?}",
6587
std::thread::current().id(),
66-
"agent.list_interfaces()",
6788
show,
6889
now.elapsed(),
6990
);
7091
});
7192

7293
println!(
73-
"Main OS Thread - {} started: {:?}",
74-
"agent.online_cpu_mem()",
94+
"Main OS Thread - agent.online_cpu_mem() started: {:?}",
7595
now.elapsed()
7696
);
7797
let show = match ac.online_cpu_mem(default_ctx(), &agent::OnlineCPUMemRequest::new()) {
78-
Err(e) => format!("{:?}", e),
79-
Ok(s) => format!("{:?}", s),
98+
Err(Error::RpcStatus(s)) => {
99+
assert_eq!(Code::NOT_FOUND, s.code());
100+
assert_eq!(
101+
"/grpc.AgentService/OnlineCPUMem is not supported".to_string(),
102+
s.message()
103+
);
104+
format!("{s:?}")
105+
}
106+
Err(e) => {
107+
panic!("not expecting an error from the example server: {:?}", e)
108+
}
109+
Ok(s) => {
110+
panic!("not expecting a OK response from the example server: {:?}", s)
111+
}
80112
};
81113
println!(
82-
"Main OS Thread - {} -> {} ended: {:?}",
83-
"agent.online_cpu_mem()",
114+
"Main OS Thread - agent.online_cpu_mem() -> {} ended: {:?}",
84115
show,
85116
now.elapsed()
86117
);
87118

88119
println!("\nsleep 2 seconds ...\n");
89120
thread::sleep(std::time::Duration::from_secs(2));
121+
122+
let version = hc.version(default_ctx(), &health::CheckRequest::new());
123+
assert_eq!("mock.0.1", version.as_ref().unwrap().agent_version.as_str());
124+
assert_eq!("0.0.1", version.as_ref().unwrap().grpc_version.as_str());
90125
println!(
91-
"Main OS Thread - {} started: {:?}",
92-
"health.version()",
126+
"Main OS Thread - health.version() started: {:?}",
93127
now.elapsed()
94128
);
95129
println!(
96-
"Main OS Thread - {} -> {:?} ended: {:?}",
97-
"health.version()",
98-
hc.version(default_ctx(), &health::CheckRequest::new()),
130+
"Main OS Thread - health.version() -> {:?} ended: {:?}",
131+
version,
99132
now.elapsed()
100133
);
101134

tests/sync-test.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::{
2+
io::{BufRead, BufReader},
3+
process::Command,
4+
time::Duration,
5+
};
6+
7+
#[test]
8+
fn run_sync_example() -> Result<(), Box<dyn std::error::Error>> {
9+
// start the server and give it a moment to start.
10+
let mut server = run_example("server").spawn().unwrap();
11+
std::thread::sleep(Duration::from_secs(2));
12+
13+
let mut client = run_example("client").spawn().unwrap();
14+
let mut client_succeeded = false;
15+
let start = std::time::Instant::now();
16+
let timeout = Duration::from_secs(600);
17+
loop {
18+
if start.elapsed() > timeout {
19+
println!("Running the client timed out. output:");
20+
client.kill().unwrap_or_else(|e| {
21+
println!("This may occur on Windows if the process has exited: {e}");
22+
});
23+
let output = client.stdout.unwrap();
24+
BufReader::new(output).lines().for_each(|line| {
25+
println!("{}", line.unwrap());
26+
});
27+
break;
28+
}
29+
30+
match client.try_wait() {
31+
Ok(Some(status)) => {
32+
client_succeeded = status.success();
33+
break;
34+
}
35+
Ok(None) => {
36+
// still running
37+
continue;
38+
}
39+
Err(e) => {
40+
println!("Error: {e}");
41+
break;
42+
}
43+
}
44+
}
45+
46+
// be sure to clean up the server, the client should have run to completion
47+
server.kill()?;
48+
assert!(client_succeeded);
49+
Ok(())
50+
}
51+
52+
fn run_example(example: &str) -> Command {
53+
let mut cmd = Command::new("cargo");
54+
cmd.arg("run")
55+
.arg("--example")
56+
.arg(example)
57+
.stdout(std::process::Stdio::piped())
58+
.stderr(std::process::Stdio::piped())
59+
.current_dir("example");
60+
cmd
61+
}

0 commit comments

Comments
 (0)