Skip to content

Commit

Permalink
Better count handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion committed Jun 6, 2024
1 parent 578513c commit ed3d10d
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
Expand Down Expand Up @@ -134,35 +138,36 @@ public void canShutdownAnOfflineClient() {
}

@Test
public void itEmitsReadyEvents() {
public void itEmitsReadyEvents() throws ExecutionException, InterruptedException, TimeoutException {
var provider = new Provider("fake-key", new LDConfig.Builder()
.offline(true).build());

var readyCount = new AtomicInteger();
var errorCount = new AtomicInteger();
var staleCount = new AtomicInteger();
CompletableFuture<Boolean> gotReadyEvent = new CompletableFuture<>();

OpenFeatureAPI.getInstance().on(ProviderEvent.PROVIDER_READY, (detail) -> {
readyCount.getAndIncrement();
gotReadyEvent.complete(true);
});

OpenFeatureAPI.getInstance().on(ProviderEvent.PROVIDER_STALE, (detail) -> {
staleCount.getAndIncrement();
});

OpenFeatureAPI.getInstance().on(ProviderEvent.PROVIDER_ERROR, (detail) -> {
System.out.println("GOT UNEXPECTED ERROR");
System.out.println(detail.getMessage());
errorCount.getAndIncrement();
});

OpenFeatureAPI.getInstance().setProviderAndWait(provider);

OpenFeatureAPI.getInstance().shutdown();

assertTrue(gotReadyEvent.get(1000, TimeUnit.MILLISECONDS));
assertEquals(1, readyCount.get());
assertEquals(0, staleCount.get());
assertEquals(0, errorCount.get());

OpenFeatureAPI.getInstance().shutdown();
}

@Test
Expand Down Expand Up @@ -199,10 +204,10 @@ public void itCanHandleClientThatIsNotInitializedImmediatelyAndErrors() throws E
var provider = new Provider("fake-key", config);
assertEquals(ProviderState.NOT_READY, provider.getState());

var errorCount = new AtomicInteger();
CompletableFuture<Boolean> gotErrorEvent = new CompletableFuture<>();

OpenFeatureAPI.getInstance().on(ProviderEvent.PROVIDER_ERROR, (detail) -> {
errorCount.getAndIncrement();
gotErrorEvent.complete(true);
});

GeneralError error = null;
Expand All @@ -215,7 +220,8 @@ public void itCanHandleClientThatIsNotInitializedImmediatelyAndErrors() throws E
assertNotNull(error);

assertEquals(ProviderState.ERROR, provider.getState());
assertTrue(errorCount.get() >= 1);

assertTrue(gotErrorEvent.get(1000, TimeUnit.MILLISECONDS));

OpenFeatureAPI.getInstance().shutdown();
}
Expand Down

0 comments on commit ed3d10d

Please sign in to comment.