Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"private": true,
"type": "module",
"devDependencies": {
"@intentee/rust-coverage-check": "0.2.0"
"@intentee/rust-coverage-check": "0.3.1"
}
}
98 changes: 98 additions & 0 deletions trzcina/src/service_shutdown_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,101 @@ impl fmt::Display for ServiceShutdownError {
}

impl Error for ServiceShutdownError {}

#[cfg(test)]
mod tests {
use std::fmt;
use std::fmt::Write;

use anyhow::anyhow;

use super::ServiceShutdownError;
use super::ServiceShutdownOutcome;
use super::ServiceShutdownOutcomeWithServiceName;

struct AlwaysFailingWriter;

impl fmt::Write for AlwaysFailingWriter {
fn write_str(&mut self, _written: &str) -> fmt::Result {
Err(fmt::Error)
}
}

struct WriterThatFailsOnSecondWrite {
has_been_called: bool,
}

impl fmt::Write for WriterThatFailsOnSecondWrite {
fn write_str(&mut self, _written: &str) -> fmt::Result {
if self.has_been_called {
return Err(fmt::Error);
}

self.has_been_called = true;
Comment thread
mcharytoniuk marked this conversation as resolved.

Ok(())
}
}

#[test]
fn display_formats_all_failure_variants() {
let outcomes = vec![
ServiceShutdownOutcomeWithServiceName {
name: "completed_service",
outcome: ServiceShutdownOutcome::Completed,
},
ServiceShutdownOutcomeWithServiceName {
name: "errored_service",
outcome: ServiceShutdownOutcome::Errored(anyhow!("service failed")),
},
ServiceShutdownOutcomeWithServiceName {
name: "panicked_service",
outcome: ServiceShutdownOutcome::Panicked("service panicked".to_owned()),
},
ServiceShutdownOutcomeWithServiceName {
name: "aborted_service",
outcome: ServiceShutdownOutcome::AbortedByShutdownDeadline,
},
ServiceShutdownOutcomeWithServiceName {
name: "leaked_service",
outcome: ServiceShutdownOutcome::LeakedBeyondShutdownDeadline,
},
];

let error = ServiceShutdownError::new(outcomes);
let formatted = format!("{error}");

assert!(!formatted.is_empty());
assert_eq!(error.failed_outcomes().len(), 5);
}

#[test]
fn display_propagates_writer_error_from_header_line() {
let shutdown_error =
ServiceShutdownError::new(vec![ServiceShutdownOutcomeWithServiceName {
name: "test_service",
outcome: ServiceShutdownOutcome::AbortedByShutdownDeadline,
}]);

let mut writer = AlwaysFailingWriter;
let write_result = write!(writer, "{shutdown_error}");

assert!(write_result.is_err());
}

#[test]
fn display_propagates_writer_error_from_outcome_line() {
let shutdown_error =
ServiceShutdownError::new(vec![ServiceShutdownOutcomeWithServiceName {
name: "test_service",
outcome: ServiceShutdownOutcome::AbortedByShutdownDeadline,
}]);

let mut writer = WriterThatFailsOnSecondWrite {
has_been_called: false,
};
let write_result = write!(writer, "{shutdown_error}");

assert!(write_result.is_err());
}
}
30 changes: 30 additions & 0 deletions trzcina/src/service_shutdown_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,33 @@ impl Default for ServiceShutdownOptions {
}
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;

use super::ServiceShutdownOptions;

#[test]
fn default_uses_ten_second_deadlines_for_both_phases() {
let ServiceShutdownOptions {
cooperative_deadline,
abort_deadline,
} = ServiceShutdownOptions::default();

assert_eq!(cooperative_deadline, Duration::from_secs(10));
assert_eq!(abort_deadline, Duration::from_secs(10));
}

#[test]
fn can_be_cloned() {
let original = ServiceShutdownOptions {
cooperative_deadline: Duration::from_millis(7),
abort_deadline: Duration::from_millis(11),
};
let cloned = original.clone();

assert_eq!(original.cooperative_deadline, cloned.cooperative_deadline);
assert_eq!(original.abort_deadline, cloned.abort_deadline);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

14 changes: 0 additions & 14 deletions trzcina/tests/service_shutdown_options_can_be_cloned.rs

This file was deleted.

This file was deleted.

Loading