|
| 1 | +/* |
| 2 | + * Copyright 2025 The Dapr Authors |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package io.dapr.testcontainers.wait.strategy; |
| 15 | + |
| 16 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 17 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 18 | +import io.dapr.testcontainers.wait.strategy.metadata.Metadata; |
| 19 | +import org.testcontainers.containers.ContainerLaunchException; |
| 20 | +import org.testcontainers.containers.wait.strategy.AbstractWaitStrategy; |
| 21 | +import org.testcontainers.shaded.org.awaitility.Awaitility; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.net.HttpURLConnection; |
| 25 | +import java.net.URL; |
| 26 | +import java.time.Duration; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.function.Predicate; |
| 29 | + |
| 30 | +/** |
| 31 | + * Base wait strategy for Dapr containers that polls the metadata endpoint. |
| 32 | + * Subclasses implement specific conditions to wait for. |
| 33 | + */ |
| 34 | +public abstract class AbstractDaprWaitStrategy extends AbstractWaitStrategy { |
| 35 | + |
| 36 | + private static final int DAPR_HTTP_PORT = 3500; |
| 37 | + private static final String METADATA_ENDPOINT = "/v1.0/metadata"; |
| 38 | + private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper() |
| 39 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 40 | + |
| 41 | + private Duration pollInterval = Duration.ofMillis(500); |
| 42 | + |
| 43 | + /** |
| 44 | + * Sets the poll interval for checking the metadata endpoint. |
| 45 | + * |
| 46 | + * @param pollInterval the interval between polling attempts |
| 47 | + * @return this strategy for chaining |
| 48 | + */ |
| 49 | + public AbstractDaprWaitStrategy withPollInterval(Duration pollInterval) { |
| 50 | + this.pollInterval = pollInterval; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void waitUntilReady() { |
| 56 | + String host = waitStrategyTarget.getHost(); |
| 57 | + Integer port = waitStrategyTarget.getMappedPort(DAPR_HTTP_PORT); |
| 58 | + String metadataUrl = String.format("http://%s:%d%s", host, port, METADATA_ENDPOINT); |
| 59 | + |
| 60 | + try { |
| 61 | + Awaitility.await() |
| 62 | + .atMost(startupTimeout.getSeconds(), TimeUnit.SECONDS) |
| 63 | + .pollInterval(pollInterval.toMillis(), TimeUnit.MILLISECONDS) |
| 64 | + .ignoreExceptions() |
| 65 | + .until(() -> checkCondition(metadataUrl)); |
| 66 | + } catch (Exception e) { |
| 67 | + throw new ContainerLaunchException( |
| 68 | + String.format("Timed out waiting for Dapr condition: %s", getConditionDescription()), e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Checks if the wait condition is satisfied. |
| 74 | + * |
| 75 | + * @param metadataUrl the URL to the metadata endpoint |
| 76 | + * @return true if the condition is met |
| 77 | + * @throws IOException if there's an error fetching metadata |
| 78 | + */ |
| 79 | + protected boolean checkCondition(String metadataUrl) throws IOException { |
| 80 | + Metadata metadata = fetchMetadata(metadataUrl); |
| 81 | + return isConditionMet(metadata); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Fetches metadata from the Dapr sidecar. |
| 86 | + * |
| 87 | + * @param metadataUrl the URL to fetch metadata from |
| 88 | + * @return the parsed metadata |
| 89 | + * @throws IOException if there's an error fetching or parsing |
| 90 | + */ |
| 91 | + protected Metadata fetchMetadata(String metadataUrl) throws IOException { |
| 92 | + HttpURLConnection connection = (HttpURLConnection) new URL(metadataUrl).openConnection(); |
| 93 | + connection.setRequestMethod("GET"); |
| 94 | + connection.setConnectTimeout(1000); |
| 95 | + connection.setReadTimeout(1000); |
| 96 | + |
| 97 | + try { |
| 98 | + int responseCode = connection.getResponseCode(); |
| 99 | + if (responseCode != 200) { |
| 100 | + throw new IOException("Metadata endpoint returned status: " + responseCode); |
| 101 | + } |
| 102 | + return OBJECT_MAPPER.readValue(connection.getInputStream(), Metadata.class); |
| 103 | + } finally { |
| 104 | + connection.disconnect(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Checks if the specific wait condition is met based on the metadata. |
| 110 | + * |
| 111 | + * @param metadata the current Dapr metadata |
| 112 | + * @return true if the condition is satisfied |
| 113 | + */ |
| 114 | + protected abstract boolean isConditionMet(Metadata metadata); |
| 115 | + |
| 116 | + /** |
| 117 | + * Returns a description of what this strategy is waiting for. |
| 118 | + * |
| 119 | + * @return a human-readable description of the condition |
| 120 | + */ |
| 121 | + protected abstract String getConditionDescription(); |
| 122 | + |
| 123 | + /** |
| 124 | + * Creates a predicate-based wait strategy for custom conditions. |
| 125 | + * |
| 126 | + * @param predicate the predicate to test against metadata |
| 127 | + * @param description a description of what the predicate checks |
| 128 | + * @return a new wait strategy |
| 129 | + */ |
| 130 | + public static AbstractDaprWaitStrategy forCondition(Predicate<Metadata> predicate, String description) { |
| 131 | + return new AbstractDaprWaitStrategy() { |
| 132 | + @Override |
| 133 | + protected boolean isConditionMet(Metadata metadata) { |
| 134 | + return predicate.test(metadata); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + protected String getConditionDescription() { |
| 139 | + return description; |
| 140 | + } |
| 141 | + }; |
| 142 | + } |
| 143 | +} |
0 commit comments