Skip to content

Commit b26d1e2

Browse files
author
Alexei Pastuchov
committed
run round_trip's assertions in separate threads
This approach ensures the test reachs killing of client's thread. This commit aims to solve issue #50
1 parent 6c8cb7b commit b26d1e2

File tree

1 file changed

+35
-24
lines changed
  • wundergraph_cli/src/print_schema

1 file changed

+35
-24
lines changed

wundergraph_cli/src/print_schema/mod.rs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::database::InferConnection;
22
use crate::infer_schema_internals::*;
33
use std::error::Error;
44
use std::io::Write;
5+
use std::thread;
56

67
mod print_helper;
78
use self::print_helper::*;
@@ -311,41 +312,51 @@ mod tests {
311312
std::thread::sleep(std::time::Duration::from_secs(1));
312313

313314
let query = "{\"query\": \"{ Users { id name } } \"}";
314-
let mut r = client
315-
.post(&format!("http://{}/graphql", listen_url))
316-
.body(query)
317-
.header(
318-
reqwest::header::CONTENT_TYPE,
319-
reqwest::header::HeaderValue::from_static("application/json"),
320-
)
321-
.send()
322-
.unwrap();
323-
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());
324-
325315
let mutation = r#"{"query":"mutation CreateUser {\n CreateUser(NewUser: {name: \"Max\"}) {\n id\n name\n }\n}","variables":null,"operationName":"CreateUser"}"#;
316+
let t1 = request_test(&client, &listen_url, query);
317+
let t2 = request_test(&client, &listen_url, mutation);
318+
let t3 = request_test(&client, &listen_url, query);
319+
320+
child.kill().unwrap();
321+
child.wait().unwrap();
322+
323+
if !t1 || !t2 || !t3 {
324+
panic!("round_trip failed")
325+
}
326+
}
327+
328+
fn request_test(
329+
client: &reqwest::Client,
330+
listen_url: &str,
331+
body: &'static str,
332+
) -> bool {
326333
let mut r = client
327334
.post(&format!("http://{}/graphql", listen_url))
328-
.body(mutation)
335+
.body(body)
329336
.header(
330337
reqwest::header::CONTENT_TYPE,
331338
reqwest::header::HeaderValue::from_static("application/json"),
332339
)
333340
.send()
334341
.unwrap();
335-
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());
336342

337-
let mut r = client
338-
.post(&format!("http://{}/graphql", listen_url))
339-
.body(query)
340-
.header(
341-
reqwest::header::CONTENT_TYPE,
342-
reqwest::header::HeaderValue::from_static("application/json"),
343-
)
344-
.send()
343+
let builder =
344+
thread::Builder::new().name("insta-assert-json-snapshot".into());
345+
let handler = builder
346+
.spawn(move || {
347+
let js = r.json::<serde_json::Value>().unwrap();
348+
insta::assert_json_snapshot!(js.clone())
349+
})
345350
.unwrap();
346-
insta::assert_json_snapshot!(r.json::<serde_json::Value>().unwrap());
347351

348-
child.kill().unwrap();
349-
child.wait().unwrap();
352+
match handler.join() {
353+
Err(e) => {
354+
if cfg!(feature = "debug") {
355+
println!("{:?}", e)
356+
}
357+
false
358+
}
359+
_ => true,
360+
}
350361
}
351362
}

0 commit comments

Comments
 (0)