Skip to content

Commit c178258

Browse files
committed
test: fix flaky time-based test
1 parent 48c05c8 commit c178258

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

testcontainers/tests/async_runner.rs

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

275-
// Container that should run for 1 second
276-
let container = GenericImage::new("alpine", "latest")
277-
.with_cmd(vec!["sleep", "1"])
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!(container.is_running().await?);
282282

283-
// After waiting for two seconds it shouldn't be running anymore
284-
tokio::time::sleep(Duration::from_secs(2)).await;
283+
container.stop().await?;
284+
285285
assert!(!container.is_running().await?);
286286

287287
Ok(())

testcontainers/tests/sync_runner.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,19 @@ fn sync_copy_files_to_container() -> anyhow::Result<()> {
288288
Ok(())
289289
}
290290

291+
#[test]
291292
fn sync_container_is_running() -> anyhow::Result<()> {
292293
let _ = pretty_env_logger::try_init();
293294

294-
// Container that should run for 1 second
295-
let container = GenericImage::new("alpine", "latest")
296-
.with_cmd(vec!["sleep", "1"])
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"))
297298
.start()?;
298299

299300
assert!(container.is_running()?);
300301

301-
// After waiting for two seconds it shouldn't be running anymore
302-
std::thread::sleep(Duration::from_secs(2));
302+
container.stop()?;
303+
303304
assert!(!container.is_running()?);
304305

305306
Ok(())

0 commit comments

Comments
 (0)