Skip to content

Commit 96a22fc

Browse files
committed
flaky tests attempt, changing it to stop flagd all the time, without stopping the container
Signed-off-by: Simon Schrottner <simon.schrottner@dynatrace.com>
1 parent 7fb193e commit 96a22fc

File tree

4 files changed

+52
-36
lines changed

4 files changed

+52
-36
lines changed

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/resolver/common/GrpcConnector.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,45 @@ public GrpcConnector(
117117
*/
118118
public void initialize() throws Exception {
119119
log.info("Initializing GRPC connection...");
120-
ChannelMonitor.monitorChannelState(ConnectivityState.READY, channel, this::onReady, this::onConnectionLost);
120+
121+
monitorChannelState(ConnectivityState.READY, channel, this::onReady, this::onConnectionLost);
122+
}
123+
124+
/**
125+
* Monitors the state of a gRPC channel and triggers the specified callbacks based on state changes.
126+
*
127+
* @param expectedState the initial state to monitor.
128+
* @param channel the ManagedChannel to monitor.
129+
* @param onConnectionReady callback invoked when the channel transitions to a READY state.
130+
* @param onConnectionLost callback invoked when the channel transitions to a FAILURE or SHUTDOWN state.
131+
*/
132+
public void monitorChannelState(
133+
ConnectivityState expectedState,
134+
ManagedChannel channel,
135+
Runnable onConnectionReady,
136+
Runnable onConnectionLost) {
137+
channel.notifyWhenStateChanged(expectedState, () -> {
138+
ConnectivityState currentState = channel.getState(true);
139+
log.debug("Channel state changed to: {}", currentState);
140+
if (currentState == ConnectivityState.READY) {
141+
if (onConnectionReady != null) {
142+
onConnectionReady.run();
143+
} else {
144+
log.debug("onConnectionReady is null");
145+
}
146+
} else if (currentState == ConnectivityState.TRANSIENT_FAILURE
147+
|| currentState == ConnectivityState.SHUTDOWN) {
148+
if (onConnectionLost != null) {
149+
onConnectionLost.run();
150+
} else {
151+
log.debug("onConnectionLost is null");
152+
}
153+
}
154+
if (currentState != ConnectivityState.SHUTDOWN) {
155+
// Re-register the state monitor to watch for the next state transition.
156+
monitorChannelState(currentState, channel, onConnectionReady, onConnectionLost);
157+
}
158+
});
121159
}
122160

123161
/**
@@ -139,7 +177,7 @@ public void shutdown() throws InterruptedException {
139177

140178
if (!channel.isShutdown()) {
141179
channel.shutdownNow();
142-
channel.awaitTermination(deadline, TimeUnit.MILLISECONDS);
180+
channel.awaitTermination(500, TimeUnit.MILLISECONDS);
143181
}
144182
}
145183

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/e2e/steps/EventSteps.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public EventSteps(State state) {
2525
@Given("a {} event handler")
2626
public void a_stale_event_handler(String eventType) {
2727
state.client.on(mapEventType(eventType), eventDetails -> {
28-
log.info("event tracked for {} at {} ms ", eventType, System.currentTimeMillis()%100_000);
28+
log.info("event tracked for {} at {} ms ", eventType, System.currentTimeMillis() % 100_000);
2929
state.events.add(new Event(eventType, eventDetails));
3030
});
3131
}
@@ -58,9 +58,8 @@ public void eventHandlerShouldBeExecuted(String eventType) {
5858
@Then("the {} event handler should have been executed within {int}ms")
5959
public void eventHandlerShouldBeExecutedWithin(String eventType, int ms) {
6060
log.info("waiting for eventtype: {}", eventType);
61-
await().atMost(ms, MILLISECONDS)
62-
.pollInterval(10, MILLISECONDS)
63-
.until(() -> state.events.stream().anyMatch(event -> event.type.equals(eventType)));
61+
await().atMost(ms, MILLISECONDS).pollInterval(10, MILLISECONDS).until(() -> state.events.stream()
62+
.anyMatch(event -> event.type.equals(eventType)));
6463
state.lastEvent = state.events.stream()
6564
.filter(event -> event.type.equals(eventType))
6665
.findFirst();

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/e2e/steps/ProviderSteps.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import lombok.extern.slf4j.Slf4j;
2626
import org.apache.commons.lang3.RandomStringUtils;
2727
import org.junit.jupiter.api.parallel.Isolated;
28-
import org.junit.platform.suite.api.BeforeSuite;
2928
import org.testcontainers.containers.BindMode;
3029
import org.testcontainers.shaded.org.apache.commons.io.FileUtils;
3130

@@ -42,7 +41,6 @@ public ProviderSteps(State state) {
4241
super(state);
4342
}
4443

45-
/*
4644
@BeforeAll
4745
public static void beforeAll() throws IOException {
4846
State.resolverType = Config.Resolver.RPC;
@@ -68,31 +66,9 @@ public void before() throws IOException {
6866
@After
6967
public void tearDown() {
7068
OpenFeatureAPI.getInstance().shutdown();
71-
}
72-
*/
73-
74-
@BeforeAll
75-
public static void beforeAll() {
76-
State.resolverType = Config.Resolver.RPC;
77-
}
78-
79-
@Before
80-
public void before() throws IOException {
81-
state.events.clear();
82-
sharedTempDir = Files.createDirectories(
83-
Paths.get("tmp/" + RandomStringUtils.randomAlphanumeric(8).toLowerCase() + "/"));
84-
container = new FlagdContainer()
85-
.withFileSystemBind(sharedTempDir.toAbsolutePath().toString(), "/tmp", BindMode.READ_WRITE);
86-
if (!container.isRunning()) {
87-
container.start();
88-
}
89-
}
90-
91-
@After
92-
public void tearDown() throws IOException {
93-
OpenFeatureAPI.getInstance().shutdown();
94-
container.stop();
95-
FileUtils.deleteDirectory(sharedTempDir.toFile());
69+
when().post("http://" + container.getLaunchpadUrl() + "/stop")
70+
.then()
71+
.statusCode(200);
9672
}
9773

9874
@Given("a {} flagd provider")
@@ -169,8 +145,11 @@ public void setupProvider(String providerType) throws IOException, InterruptedEx
169145

170146
@When("the connection is lost for {int}s")
171147
public void the_connection_is_lost_for(int seconds) throws InterruptedException {
172-
log.info("Timeout and wait for {} seconds starting at {} ms, should resume at {} ms", seconds,
173-
System.currentTimeMillis() % 100_000, System.currentTimeMillis() % 100_000 + seconds * 1000L);
148+
log.info(
149+
"Timeout and wait for {} seconds starting at {} ms, should resume at {} ms",
150+
seconds,
151+
System.currentTimeMillis() % 100_000,
152+
System.currentTimeMillis() % 100_000 + seconds * 1000L);
174153

175154
when().post("http://" + container.getLaunchpadUrl() + "/restart?seconds={seconds}", seconds)
176155
.then()

0 commit comments

Comments
 (0)