Skip to content

Commit

Permalink
fixup! feat: improve wait logic to a more elegant solution open-featu…
Browse files Browse the repository at this point in the history
…re#1160

Signed-off-by: christian.lutnik <[email protected]>
  • Loading branch information
chrfwow committed Jan 22, 2025
1 parent 15af42d commit 03de275
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ EvaluationContext getEnrichedContext() {
private void onProviderEvent(FlagdProviderEvent flagdProviderEvent) {

synchronized (syncResources) {
log.info("FlagdProviderEvent: {} of type {}", flagdProviderEvent, flagdProviderEvent.getClass().getName());
log.info("FlagdProviderEvent event {} ", flagdProviderEvent.getEvent().toString());
log.info(
"FlagdProviderEvent: {} of type {}",
flagdProviderEvent,
flagdProviderEvent.getClass().getName());
log.info(
"FlagdProviderEvent event {} ",
flagdProviderEvent.getEvent().toString());
syncResources.setSyncMetadata(flagdProviderEvent.getSyncMetadata());
if (flagdProviderEvent.getSyncMetadata() != null) {
syncResources.setEnrichedContext(contextEnricher.apply(flagdProviderEvent.getSyncMetadata()));
Expand All @@ -231,6 +236,9 @@ private void onProviderEvent(FlagdProviderEvent flagdProviderEvent) {
break;
}
log.info("config change not ready");
onReady();
syncResources.setPreviousEvent(ProviderEvent.PROVIDER_READY);
break;
// intentional fall through, a not-ready change will trigger a ready.
case PROVIDER_READY:
onReady();
Expand Down Expand Up @@ -270,6 +278,7 @@ private void onReady() {
}
this.emitProviderReady(
ProviderEventDetails.builder().message("connected to flagd").build());
log.info("post onready");
}

private void onError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public synchronized boolean initialize() {
}
this.initialized = true;
this.notifyAll();
log.info("notified all");
return true;
}

Expand Down Expand Up @@ -77,6 +78,7 @@ public void waitForInitialization(long deadline) {
long remaining = end - now;
synchronized (this) {
if (initialized) { // might have changed in the meantime
log.info("post wait for init in loop");
return;
}
if (isShutDown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ void waitForInitialization_waitsApproxForDeadline() {
final AtomicLong start = new AtomicLong();
final AtomicLong end = new AtomicLong();
final long deadline = 45;
Assertions.assertThrows(GeneralError.class, () -> {
start.set(System.currentTimeMillis());
try {
flagdProviderSyncResources.waitForInitialization(deadline);
} catch (Exception e) {
end.set(System.currentTimeMillis());
throw e;
}
});

start.set(System.currentTimeMillis());
Assertions.assertThrows(GeneralError.class, () -> flagdProviderSyncResources.waitForInitialization(deadline));
end.set(System.currentTimeMillis());

final long elapsed = end.get() - start.get();
// should wait at least for the deadline
Assertions.assertTrue(elapsed >= deadline);
Expand All @@ -54,7 +50,9 @@ void interruptingWaitingThread_isIgnored() throws InterruptedException {
Thread waitingThread = new Thread(() -> {
long start = System.currentTimeMillis();
isWaiting.set(true);
flagdProviderSyncResources.waitForInitialization(deadline);
Assertions.assertThrows(
GeneralError.class, () -> flagdProviderSyncResources.waitForInitialization(deadline));

long end = System.currentTimeMillis();
long duration = end - start;
// even though thread was interrupted, it still waited for the deadline
Expand Down Expand Up @@ -110,7 +108,7 @@ void callingShutdown_wakesUpWaitingThreadWithException() throws InterruptedExcep
long start = System.currentTimeMillis();
isWaiting.set(true);
Assertions.assertThrows(
IllegalArgumentException.class, () -> flagdProviderSyncResources.waitForInitialization(10000));
IllegalStateException.class, () -> flagdProviderSyncResources.waitForInitialization(10000));

long end = System.currentTimeMillis();
long duration = end - start;
Expand Down

0 comments on commit 03de275

Please sign in to comment.