Skip to content

Commit

Permalink
using awaitability to wait for placement
Browse files Browse the repository at this point in the history
  • Loading branch information
salaboy committed Feb 28, 2025
1 parent 421a572 commit 52e35c8
Showing 1 changed file with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
import org.testcontainers.shaded.org.awaitility.core.ConditionTimeoutException;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.any;
Expand All @@ -48,6 +51,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;


@Testcontainers
@WireMockTest(httpPort = 8081)
Expand All @@ -60,7 +65,6 @@ public class DaprContainerIT {
private static final String KEY = "my-key";
private static final String PUBSUB_NAME = "pubsub";
private static final String PUBSUB_TOPIC_NAME = "topic";
private static final String DAPR_INIT_RUNNING_MESSAGE_PATTERN = ".*Placement tables updated.*";

@Container
private static final DaprContainer DAPR_CONTAINER = new DaprContainer("daprio/daprd")
Expand Down Expand Up @@ -127,17 +131,34 @@ public void testStateStore() throws Exception {
@Test
public void testPlacement() throws Exception {

Wait.forLogMessage(DAPR_INIT_RUNNING_MESSAGE_PATTERN, 1).waitUntilReady(DAPR_CONTAINER);
try {
await().atMost(4, TimeUnit.SECONDS)
.pollDelay(400, TimeUnit.MILLISECONDS)
.pollInterval(400, TimeUnit.MILLISECONDS)
.until(() -> {
String metadata = checkSideCarMetadata();
if (metadata.contains("placement: connected")) {
return true;
} else {
return false;
}
});
} catch (ConditionTimeoutException timeoutException) {
fail("The placement server is not connected");
}

}

private String checkSideCarMetadata() throws IOException {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.build();
.build();
Request request = new Request.Builder()
.url(DAPR_CONTAINER.getHttpEndpoint() + "/v1.0/metadata")
.build();
.url(DAPR_CONTAINER.getHttpEndpoint() + "/v1.0/metadata")
.build();

try (Response response = okHttpClient.newCall(request).execute()) {
if (response.isSuccessful() && response.body() != null) {
assertTrue(response.body().string().contains("placement: connected"));
return response.body().string();
} else {
throw new IOException("Unexpected response: " + response.code());
}
Expand Down

0 comments on commit 52e35c8

Please sign in to comment.