libcnb.rs icon indicating copy to clipboard operation
libcnb.rs copied to clipboard

Automatically wait for services inside the container to bind

Open Malax opened this issue 3 years ago • 5 comments

When forwarding ports to the container, Docker will make the port available immediately even when the service inside the container hasn't bound to its local port yet. This requires integration tests to sometimes sleep for a fixed duration. Ideally, libcnb-test would automatically wait (in a smart way, exponential backoff?) until the forwarded ports are bound to the actual service before passing back control to the test:

context.start_container(&[12345], |container| {
    std::thread::sleep(Duration::from_secs(1));
    // Code that works with the service
}

The solution to this problem might be complex, might differ for certain kinds of services, etc.

Malax avatar Jan 18 '22 11:01 Malax

I feel like maybe this is something that should be handled by the client?

Eg: An optional libcnb-test test helper for making HTTP requests (potentially using ureq) that performs retries, and so can do something similar to: https://github.com/heroku/builder/blob/2bba96165a461b1e8ae881b69efc47889591214f/.circleci/config.yml#L50-L59

edmorley avatar Jul 01 '22 09:07 edmorley

I wrote this (and I'm particularly fond of the name) https://github.com/heroku/buildpacks-ruby/blob/3fcea7d01058c8117ff8a1e98d6275835f6ee5d9/tests/integration_test.rs#L34-L35.

It just repeatedly hits the root for 10 seconds while it's waiting to boot. It also spits out the server logs automatically. It could be a bit smarter or nicer, but it works better than sleeping.

schneems avatar Oct 21 '22 16:10 schneems

Any thoughts on porting this (or something like it to libcnb.rs ?

schneems avatar Dec 14 '22 21:12 schneems

This is what one of libcnb's own tests does: https://github.com/heroku/libcnb.rs/blob/dadd5b51fcc7c32e63bc0256132304a66ddff96f/libcnb-test/tests/integration_test.rs#L74-L84

I've had to do similar for Python.

Having a libcnb utility for this would be great.

It would also be useful to have retries for HTTP requests outside of tests too (for downloads in general). Perhaps the solution for both would be similar enough that they could share a utility function?

edmorley avatar Dec 14 '22 21:12 edmorley

For reference, the Heroku JVM buildpacks use this helper: https://github.com/heroku/buildpacks-jvm/blob/0849c65e08eb9bea6c8d7f2e26c0f61a5c1ce510/shared-test/src/lib.rs#L52-L74

Malax avatar May 23 '23 09:05 Malax