Skip to content

Commit 09e23c8

Browse files
committed
server: Make some of the graphman tests less flaky
1 parent 6cc3a2d commit 09e23c8

File tree

1 file changed

+95
-71
lines changed

1 file changed

+95
-71
lines changed

server/graphman/tests/deployment_mutation.rs

Lines changed: 95 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@ use self::util::server::VALID_TOKEN;
1616

1717
const TEST_SUBGRAPH_SCHEMA: &str = "type User @entity { id: ID!, name: String }";
1818

19+
async fn is_deployment_paused(hash: &str) -> bool {
20+
let query = r#"query DeploymentStatus($hash: String!) {
21+
deployment {
22+
info(deployment: { hash: $hash }) {
23+
status {
24+
isPaused
25+
}
26+
}
27+
}
28+
}"#;
29+
30+
let resp = send_graphql_request(
31+
json!({
32+
"query": query,
33+
"variables": {
34+
"hash": hash
35+
}
36+
}),
37+
VALID_TOKEN,
38+
)
39+
.await;
40+
41+
resp["data"]["deployment"]["info"][0]["status"]["isPaused"]
42+
.as_bool()
43+
.unwrap()
44+
}
45+
1946
async fn assert_deployment_paused(hash: &str, should_be_paused: bool) {
2047
let query = r#"query DeploymentStatus($hash: String!) {
2148
deployment {
@@ -156,18 +183,77 @@ fn graphql_can_restart_deployments() {
156183
)
157184
.await;
158185

159-
assert_deployment_paused("subgraph_2", true).await;
160-
assert_deployment_paused("subgraph_1", false).await;
161-
162-
sleep(Duration::from_secs(5)).await;
163-
164-
assert_deployment_paused("subgraph_2", false).await;
165-
assert_deployment_paused("subgraph_1", false).await;
186+
let start = tokio::time::Instant::now();
187+
let mut was_paused = false;
188+
loop {
189+
let paused = is_deployment_paused("subgraph_2").await;
190+
if paused {
191+
was_paused = true;
192+
}
193+
if was_paused && !paused {
194+
// Successfully restarted
195+
break;
196+
}
197+
if start.elapsed() > Duration::from_secs(30) {
198+
panic!("Deployment 'subgraph_2' was not restarted within 30 seconds");
199+
}
200+
sleep(Duration::from_millis(200)).await;
201+
}
166202
});
167203
}
168204

169205
#[test]
170206
fn graphql_allows_tracking_restart_deployment_executions() {
207+
async fn execution_status(execution_id: &str) -> String {
208+
let query = r#"query TrackRestartDeployment($id: String!) {
209+
execution {
210+
info(id: $id) {
211+
id
212+
kind
213+
status
214+
errorMessage
215+
}
216+
}
217+
}"#;
218+
219+
let resp = send_graphql_request(
220+
json!({
221+
"query": query,
222+
"variables": {
223+
"id": execution_id
224+
}
225+
}),
226+
VALID_TOKEN,
227+
)
228+
.await;
229+
230+
let info = &resp["data"]["execution"]["info"];
231+
assert_eq!(execution_id, info["id"].as_str().unwrap());
232+
assert_eq!("RESTART_DEPLOYMENT", info["kind"].as_str().unwrap());
233+
assert!(info["errorMessage"].is_null());
234+
235+
let status = info["status"].as_str().unwrap();
236+
237+
status.to_string()
238+
}
239+
240+
async fn wait_for_status(execution_id: &str, desired_status: &str) {
241+
let start = tokio::time::Instant::now();
242+
loop {
243+
let status = execution_status(execution_id).await;
244+
if status == desired_status {
245+
break;
246+
}
247+
if start.elapsed() > Duration::from_secs(30) {
248+
panic!(
249+
"Execution '{}' did not enter {} state within 30 seconds",
250+
execution_id, desired_status
251+
);
252+
}
253+
sleep(Duration::from_millis(200)).await;
254+
}
255+
}
256+
171257
run_test(|| async {
172258
let deployment_hash = DeploymentHash::new("subgraph_1").unwrap();
173259
create_test_subgraph(&deployment_hash, TEST_SUBGRAPH_SCHEMA).await;
@@ -202,70 +288,8 @@ fn graphql_allows_tracking_restart_deployment_executions() {
202288
let resp: Response = serde_json::from_value(resp).expect("response is valid");
203289
let execution_id = resp.data.deployment.restart;
204290

205-
let query = r#"query TrackRestartDeployment($id: String!) {
206-
execution {
207-
info(id: $id) {
208-
id
209-
kind
210-
status
211-
errorMessage
212-
}
213-
}
214-
}"#;
215-
216-
let resp = send_graphql_request(
217-
json!({
218-
"query": query,
219-
"variables": {
220-
"id": execution_id
221-
}
222-
}),
223-
VALID_TOKEN,
224-
)
225-
.await;
226-
227-
let expected_resp = json!({
228-
"data": {
229-
"execution": {
230-
"info": {
231-
"id": execution_id,
232-
"kind": "RESTART_DEPLOYMENT",
233-
"status": "RUNNING",
234-
"errorMessage": null,
235-
}
236-
}
237-
}
238-
});
239-
240-
assert_eq!(resp, expected_resp);
241-
242-
sleep(Duration::from_secs(5)).await;
243-
244-
let resp = send_graphql_request(
245-
json!({
246-
"query": query,
247-
"variables": {
248-
"id": execution_id
249-
}
250-
}),
251-
VALID_TOKEN,
252-
)
253-
.await;
254-
255-
let expected_resp = json!({
256-
"data": {
257-
"execution": {
258-
"info": {
259-
"id": execution_id,
260-
"kind": "RESTART_DEPLOYMENT",
261-
"status": "SUCCEEDED",
262-
"errorMessage": null,
263-
}
264-
}
265-
}
266-
});
267-
268-
assert_eq!(resp, expected_resp);
291+
wait_for_status(&execution_id, "RUNNING").await;
292+
wait_for_status(&execution_id, "SUCCEEDED").await;
269293
});
270294
}
271295

0 commit comments

Comments
 (0)