Skip to content

rust testcontainer framework#41

Merged
slinkydeveloper merged 12 commits into
restatedev:mainfrom
kpwebb:main
Feb 20, 2025
Merged

rust testcontainer framework#41
slinkydeveloper merged 12 commits into
restatedev:mainfrom
kpwebb:main

Conversation

@kpwebb
Copy link
Copy Markdown
Contributor

@kpwebb kpwebb commented Feb 10, 2025

Here's my initial shot at a testcontainers implementation for the Rust SDK.

I've got the core functionality working, but wanted to share while in progress to see if adding a new "restate-test-utils" crate to the SDK is ok. Also wanted to solicit feedback on ways to extend the test implementation (e.g. mirroring Temporal's approach), per discussions on Discord.

Here's the current test implementation for a service as defined in tests/test_container.rs

#[tokio::test]
async fn test_container_image() {

    let test_container = TestContainer::new().await.unwrap();

    // uses higher port number to avoid collisions
    // with non-test instances running locally
    let host_port:u16 = 19080;
    let host_address = format!("0.0.0.0:{}", host_port);

    println!("starting host");
    // boot restate server
    tokio::spawn(async move {
        HttpServer::new(
            Endpoint::builder()
                .bind(MyServiceImpl.serve())
                .build(),
        ).listen_and_serve(host_address.parse().unwrap()).await;
    });

    use restate_sdk::service::Discoverable;
    let my_service:Service = ServeMyService::<MyServiceImpl>::discover();

    let registered = test_container.register(host_port).await;
    assert!(registered.is_ok());

    TestContainer::delay(1000).await;

    let response = test_container.invoke(my_service, "my_handler".to_string()).await;

}

@slinkydeveloper
Copy link
Copy Markdown
Contributor

This is awesome, i'll give a look in the next days!

Copy link
Copy Markdown
Contributor

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just left few initial comments, thanks for the contribution!

Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread restate-test-utils/Cargo.toml Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
@kpwebb
Copy link
Copy Markdown
Contributor Author

kpwebb commented Feb 11, 2025

@slinkydeveloper thanks for the feedback! Just pushed an update that captures the above. Here's the new test implementation:

#[tokio::test]
async fn test_container_image() {

    let mut test_container = TestContainer::new("docker.io/restatedev/restate", "latest").await.unwrap();

    let endpoint = Endpoint::builder()
                    .bind(MyServiceImpl.serve())
                    .build();

    test_container.serve_endpoint(endpoint).await;

    // optionally insert a delays via tokio sleep
    TestContainer::delay(1000).await;

    // optionally call invoke on service handlers
    use restate_sdk::service::Discoverable;
    let my_service:Service = ServeMyService::<MyServiceImpl>::discover();
    let invoke_response = test_container.invoke(my_service, "my_handler").await;

    assert!(invoke_response.is_ok());

    println!("invoke response:");
    println!("{}", invoke_response.unwrap().text().await.unwrap());

}

Copy link
Copy Markdown
Contributor

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Thanks again for the contribution, I had some time to take a look, it's getting there, I think the API needs another pass

Comment thread tests/test_container.rs Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread tests/test_container.rs Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread restate-test-utils/src/test_utils.rs Outdated
Comment thread restate-test-utils/Cargo.toml Outdated
Comment thread restate-test-utils/Cargo.toml Outdated
Comment thread restate-test-utils/Cargo.toml Outdated
Copy link
Copy Markdown
Contributor

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. Run just check locally to fix linting/formatting issues and then we should be good to go!

Comment thread test-env/src/lib.rs
Comment thread test-env/src/lib.rs Outdated
Comment thread test-env/src/lib.rs Outdated
Comment thread test-env/src/lib.rs Outdated
Comment thread test-env/tests/test_container.rs Outdated
@kpwebb
Copy link
Copy Markdown
Contributor Author

kpwebb commented Feb 19, 2025

thanks again for the recs and sorry for not running lint/fmt! should be good to go now

Copy link
Copy Markdown
Contributor

@slinkydeveloper slinkydeveloper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor nit, and I merge

Comment thread test-env/Cargo.toml Outdated
Comment thread README.md Outdated
Comment thread README.md
@slinkydeveloper slinkydeveloper merged commit a656187 into restatedev:main Feb 20, 2025
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

Successfully merging this pull request may close these issues.

2 participants