Skip to content

Commit 47459f6

Browse files
committed
test: fix flaky time-based test
1 parent f3d7689 commit 47459f6

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

testcontainers/tests/async_runner.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -272,17 +272,17 @@ async fn async_copy_files_to_container() -> anyhow::Result<()> {
272272
async fn async_container_exit_code() -> anyhow::Result<()> {
273273
let _ = pretty_env_logger::try_init();
274274

275-
// Container that should run for 1 second and exit with a specific doe
276-
let container = GenericImage::new("alpine", "latest")
277-
.with_cmd(vec!["/bin/sh", "-c", "sleep 1 && exit 4"])
275+
// Container that should run until manually quit
276+
let container = GenericImage::new("simple_web_server", "latest")
277+
.with_wait_for(WaitFor::message_on_stdout("server is ready"))
278278
.start()
279279
.await?;
280280

281281
assert_eq!(container.exit_code().await?, None);
282282

283-
// After waiting for two seconds it shouldn't be running anymore
284-
tokio::time::sleep(Duration::from_secs(2)).await;
285-
assert_eq!(container.exit_code().await?, Some(4));
283+
container.stop().await?;
284+
285+
assert_eq!(container.exit_code().await?, Some(0));
286286

287287
Ok(())
288288
}

testcontainers/tests/sync_runner.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,16 +292,16 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> {
292292
fn sync_container_exit_code() -> anyhow::Result<()> {
293293
let _ = pretty_env_logger::try_init();
294294

295-
// Container that should run for 1 second and exit with a specific doe
296-
let container = GenericImage::new("alpine", "latest")
297-
.with_cmd(vec!["/bin/sh", "-c", "sleep 1 && exit 4"])
295+
// Container that should run until manually quit
296+
let container = GenericImage::new("simple_web_server", "latest")
297+
.with_wait_for(WaitFor::message_on_stdout("server is ready"))
298298
.start()?;
299299

300300
assert_eq!(container.exit_code()?, None);
301301

302-
// After waiting for two seconds it shouldn't be running anymore
303-
std::thread::sleep(Duration::from_secs(2));
304-
assert_eq!(container.exit_code()?, Some(4));
302+
container.stop()?;
303+
304+
assert_eq!(container.exit_code()?, Some(0));
305305

306306
Ok(())
307307
}

0 commit comments

Comments
 (0)