Skip to content

Container doesn't stop after test with std::sync::OnceLock #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nearwall opened this issue Oct 18, 2023 · 2 comments
Closed

Container doesn't stop after test with std::sync::OnceLock #528

nearwall opened this issue Oct 18, 2023 · 2 comments

Comments

@nearwall
Copy link

Hello for everyone!

I'm trying to use testcontiners for tests with static container.

testcontainers = "0.15.0"
rust-version = "1.70.0"

I've chosen std::sync::OnceLock for such purpose. There is my simple code snippet:

use std::sync::OnceLock;

use testcontainers::{clients, core::WaitFor, Container, GenericImage};

static DOCKER_CLI: OnceLock<clients::Cli> = OnceLock::new();
static DOCKER_CONTAINER: OnceLock<Container<'_, GenericImage>> = OnceLock::new();

const DOCKER_IMAGE_NAME: &str = "postgres";
const DOCKER_IMAGE_TAG: &str = "16.0-alpine3.18";
const DB_NAME: &str = "postgres";
const DB_USER: &str = "postgres";
const DB_PASSWORD: &str = "postgres";

fn get_static_container() -> &'static Container<'static, GenericImage> {
    let cli = DOCKER_CLI.get_or_init(clients::Cli::default);
    DOCKER_CONTAINER.get_or_init(|| {
        let image = GenericImage::new(DOCKER_IMAGE_NAME, DOCKER_IMAGE_TAG)
            .with_env_var("POSTGRES_DB".to_string(), DB_NAME)
            .with_env_var("POSTGRES_USER".to_string(), DB_USER)
            .with_env_var("POSTGRES_PASSWORD".to_string(), DB_PASSWORD)
            .with_wait_for(WaitFor::message_on_stdout("database system is ready to accept connections"));
        cli.run(image)
    })
}

#[tokio::test(flavor = "multi_thread")]
async fn test_async() {
    let _container = get_static_container();
   // `true` to `false` doesn't matter, container will be alive after test
    assert!(true, "test finished");
}

#[test]
fn test_sync() {
    let _container = get_static_container();
   // `true` to `false` doesn't matter, container will be alive after test
    assert!(true, "test finished");
}

But it doesn't work properly. The static initialized container is still alive after tests are finished. I've tested it with the whole test file and both testcases separately

Am I doing something wrong testcontainers or OnceLock ?

@thomaseizinger
Copy link
Collaborator

Yep that is currently not really well supported. We need to rework the API for this to work well: #386. Contributions welcome!

@DDtKey
Copy link
Collaborator

DDtKey commented Apr 7, 2024

I'm closing this issue in favor of #386. Please track that issue instead.

However I don't think this will be supported this way, because it's static item, see the doc

Static items do not call drop at the end of the program.

But something similar can be achieved by watchdog / ryuk

@DDtKey DDtKey closed this as completed Apr 7, 2024
@DDtKey DDtKey closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants