Skip to content

Commit fb60c98

Browse files
committed
feat: Adds option to stop containers with a timeout
Previously containers were stopped with no timeout (SIGKILL) which was interfering with clean shutdown of processes being tested. This commit adds a new API of stop_with_timeout(timeout_secconds) in addition to the original stop(). The original stop() interface calls stop_with_timeout using the default '0'.
1 parent 9121760 commit fb60c98

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

testcontainers/src/core/client.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,12 @@ impl Client {
162162
.map_err(ClientError::RemoveContainer)
163163
}
164164

165-
pub(crate) async fn stop(&self, id: &str) -> Result<(), ClientError> {
165+
pub(crate) async fn stop(&self, id: &str, timeout_seconds: i64) -> Result<(), ClientError> {
166166
self.bollard
167-
.stop_container(id, None)
167+
.stop_container(
168+
id,
169+
Some(bollard::container::StopContainerOptions { t: timeout_seconds }),
170+
)
168171
.await
169172
.map_err(ClientError::StopContainer)
170173
}

testcontainers/src/core/containers/async_container.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,22 @@ where
282282

283283
/// Stops the container (not the same with `pause`).
284284
pub async fn stop(&self) -> Result<()> {
285-
log::debug!("Stopping docker container {}", self.id);
285+
self.stop_with_timeout(0).await?;
286+
Ok(())
287+
}
288+
289+
/// Stops the container with timeout before issuing SIGKILL (not the same with `pause`).
290+
///
291+
/// Set -1 for immediate SIGKILL, otherwise the runtime will issue SIGINT and then wait
292+
/// the specified number of seconds for process to stop before issunging SIGKILL.
293+
pub async fn stop_with_timeout(&self, timeout_seconds: i64) -> Result<()> {
294+
log::debug!(
295+
"Stopping docker container {} with {} second timeout",
296+
self.id,
297+
timeout_seconds
298+
);
286299

287-
self.docker_client.stop(&self.id).await?;
300+
self.docker_client.stop(&self.id, timeout_seconds).await?;
288301
Ok(())
289302
}
290303

testcontainers/src/core/containers/sync_container.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ where
132132
self.rt().block_on(self.async_impl().stop())
133133
}
134134

135+
/// Stops the container with timeout before issuing SIGKILL (not the same with `pause`).
136+
///
137+
/// Set -1 for immediate SIGKILL, otherwise the runtime will issue SIGINT and then wait
138+
/// the specified number of seconds for process to stop before issunging SIGKILL.
139+
pub fn stop_with_timeout(&self, timeout_seconds: i64) -> Result<()> {
140+
self.rt().block_on(self.async_impl().stop_with_timeout(timeout_seconds))
141+
}
142+
135143
/// Starts the container.
136144
pub fn start(&self) -> Result<()> {
137145
self.rt().block_on(self.async_impl().start())

testcontainers/src/watchdog.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static WATCHDOG: Lazy<Mutex<Watchdog>> = Lazy::new(|| {
3434
.unwrap_or_default()
3535
{
3636
signal_docker
37-
.stop(&container_id)
37+
.stop(&container_id, 0)
3838
.await
3939
.expect("failed to stop container");
4040
signal_docker

0 commit comments

Comments
 (0)