Skip to content

Commit 6fd68f1

Browse files
committed
feat: support cancellation in e2e test app result
1 parent 7e4c6d7 commit 6fd68f1

File tree

1 file changed

+32
-29
lines changed
  • mithril-test-lab/mithril-end-to-end/src

1 file changed

+32
-29
lines changed

mithril-test-lab/mithril-end-to-end/src/main.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
use anyhow::{anyhow, Context};
22
use clap::{CommandFactory, Parser, Subcommand};
33
use slog::{Drain, Level, Logger};
4-
use slog_scope::{error, info, warn};
4+
use slog_scope::{error, info};
55
use std::{
66
fmt, fs,
77
path::{Path, PathBuf},
88
process::{ExitCode, Termination},
99
sync::Arc,
1010
time::Duration,
1111
};
12+
use thiserror::Error;
1213
use tokio::{
1314
signal::unix::{signal, SignalKind},
1415
sync::Mutex,
@@ -217,13 +218,14 @@ enum AppResult {
217218
Success(),
218219
UnretryableError(anyhow::Error),
219220
RetryableError(anyhow::Error),
221+
Cancelled(anyhow::Error),
220222
}
221223

222224
impl AppResult {
223225
fn exit_code(&self) -> ExitCode {
224226
match self {
225227
AppResult::Success() => ExitCode::SUCCESS,
226-
AppResult::UnretryableError(_) => ExitCode::FAILURE,
228+
AppResult::UnretryableError(_) | AppResult::Cancelled(_) => ExitCode::FAILURE,
227229
AppResult::RetryableError(_) => ExitCode::from(2),
228230
}
229231
}
@@ -235,6 +237,7 @@ impl fmt::Display for AppResult {
235237
AppResult::Success() => write!(f, "Success"),
236238
AppResult::UnretryableError(error) => write!(f, "Error(Unretryable): {error:?}"),
237239
AppResult::RetryableError(error) => write!(f, "Error(Retryable): {error:?}"),
240+
AppResult::Cancelled(error) => write!(f, "Cancelled: {error:?}"),
238241
}
239242
}
240243
}
@@ -259,6 +262,8 @@ impl From<StdResult<()>> for AppResult {
259262
Err(error) => {
260263
if error.is::<RetryableDevnetError>() {
261264
AppResult::RetryableError(error)
265+
} else if error.is::<SignalError>() {
266+
AppResult::Cancelled(error)
262267
} else {
263268
AppResult::UnretryableError(error)
264269
}
@@ -404,32 +409,30 @@ fn create_workdir_if_not_exist_clean_otherwise(work_dir: &Path) {
404409
fs::create_dir(work_dir).expect("Work dir creation failure");
405410
}
406411

412+
#[derive(Error, Debug, PartialEq, Eq)]
413+
#[error("Signal received: `{0}`")]
414+
pub struct SignalError(pub String);
415+
407416
fn with_gracefull_shutdown(join_set: &mut JoinSet<StdResult<()>>) {
408417
join_set.spawn(async move {
409418
let mut sigterm = signal(SignalKind::terminate()).expect("Failed to create SIGTERM signal");
410-
sigterm
411-
.recv()
412-
.await
413-
.ok_or(anyhow!("Failed to receive SIGTERM"))
414-
.inspect(|()| warn!("Received SIGTERM"))
419+
sigterm.recv().await;
420+
421+
Err(anyhow!(SignalError("SIGTERM".to_string())))
415422
});
416423

417424
join_set.spawn(async move {
418425
let mut sigterm = signal(SignalKind::interrupt()).expect("Failed to create SIGINT signal");
419-
sigterm
420-
.recv()
421-
.await
422-
.ok_or(anyhow!("Failed to receive SIGINT"))
423-
.inspect(|()| warn!("Received SIGINT"))
426+
sigterm.recv().await;
427+
428+
Err(anyhow!(SignalError("SIGINT".to_string())))
424429
});
425430

426431
join_set.spawn(async move {
427432
let mut sigterm = signal(SignalKind::quit()).expect("Failed to create SIGQUIT signal");
428-
sigterm
429-
.recv()
430-
.await
431-
.ok_or(anyhow!("Failed to receive SIGQUIT"))
432-
.inspect(|()| warn!("Received SIGQUIT"))
433+
sigterm.recv().await;
434+
435+
Err(anyhow!(SignalError("SIGQUIT".to_string())))
433436
});
434437
}
435438

@@ -441,24 +444,19 @@ mod tests {
441444
fn app_result_exit_code() {
442445
let expected_exit_code = ExitCode::SUCCESS;
443446
let exit_code = AppResult::Success().exit_code();
444-
assert_eq!(
445-
format!("{:?}", expected_exit_code),
446-
format!("{:?}", exit_code)
447-
);
447+
assert_eq!(expected_exit_code, exit_code);
448448

449449
let expected_exit_code = ExitCode::FAILURE;
450450
let exit_code = AppResult::UnretryableError(anyhow::anyhow!("an error")).exit_code();
451-
assert_eq!(
452-
format!("{:?}", expected_exit_code),
453-
format!("{:?}", exit_code)
454-
);
451+
assert_eq!(expected_exit_code, exit_code);
455452

456453
let expected_exit_code = ExitCode::from(2);
457454
let exit_code = AppResult::RetryableError(anyhow::anyhow!("an error")).exit_code();
458-
assert_eq!(
459-
format!("{:?}", expected_exit_code),
460-
format!("{:?}", exit_code)
461-
);
455+
assert_eq!(expected_exit_code, exit_code);
456+
457+
let expected_exit_code = ExitCode::FAILURE;
458+
let exit_code = AppResult::Cancelled(anyhow::anyhow!("an error")).exit_code();
459+
assert_eq!(expected_exit_code, exit_code);
462460
}
463461

464462
#[test]
@@ -474,5 +472,10 @@ mod tests {
474472
AppResult::from(Err(anyhow!("an error"))),
475473
AppResult::UnretryableError(_)
476474
));
475+
476+
assert!(matches!(
477+
AppResult::from(Err(anyhow!(SignalError("an error".to_string())))),
478+
AppResult::Cancelled(_)
479+
));
477480
}
478481
}

0 commit comments

Comments
 (0)