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 21, 2025
1 parent 12e889f commit 82b542b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.openfeature.contrib.providers.flagd;

import com.google.common.annotations.VisibleForTesting;
import dev.openfeature.sdk.EvaluationContext;
import dev.openfeature.sdk.ImmutableContext;
import dev.openfeature.sdk.ImmutableStructure;
Expand All @@ -11,9 +12,10 @@

/**
* Contains all fields we need to worry about locking, used as intrinsic lock
* for sync blocks.
* for sync blocks in the {@link FlagdProvider}.
*/
@Getter
@VisibleForTesting
public class FlagdProviderSyncResources {
@Setter
private volatile ProviderEvent previousEvent = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

class EventsLockTest {
class FlagdProviderSyncResourcesTest {
private static final long PERMISSIBLE_EPSILON = 20;

private FlagdProviderSyncResources eventsLock;
private FlagdProviderSyncResources flagdProviderSyncResources;

@BeforeEach
void setUp() {
eventsLock = new FlagdProviderSyncResources();
flagdProviderSyncResources = new FlagdProviderSyncResources();
}

@Timeout(2)
@Test
void waitForInitialization_failsWhenDeadlineElapses() {
Assertions.assertThrows(GeneralError.class, () -> eventsLock.waitForInitialization(2));
Assertions.assertThrows(GeneralError.class, () -> flagdProviderSyncResources.waitForInitialization(2));
}

@Timeout(2)
Expand All @@ -34,7 +34,7 @@ void waitForInitialization_waitsApproxForDeadline() {
Assertions.assertThrows(GeneralError.class, () -> {
start.set(System.currentTimeMillis());
try {
eventsLock.waitForInitialization(deadline);
flagdProviderSyncResources.waitForInitialization(deadline);
} catch (Exception e) {
end.set(System.currentTimeMillis());
throw e;
Expand All @@ -55,7 +55,7 @@ void interruptingWaitingThread_isIgnored() throws InterruptedException {
Thread waitingThread = new Thread(() -> {
long start = System.currentTimeMillis();
isWaiting.set(true);
eventsLock.waitForInitialization(deadline);
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 @@ -85,7 +85,7 @@ void callingInitialize_wakesUpWaitingThread() throws InterruptedException {
Thread waitingThread = new Thread(() -> {
long start = System.currentTimeMillis();
isWaiting.set(true);
eventsLock.waitForInitialization(10000);
flagdProviderSyncResources.waitForInitialization(10000);
long end = System.currentTimeMillis();
long duration = end - start;
Assertions.assertTrue(duration < PERMISSIBLE_EPSILON);
Expand All @@ -98,7 +98,7 @@ void callingInitialize_wakesUpWaitingThread() throws InterruptedException {

Thread.sleep(PERMISSIBLE_EPSILON); // waitingThread should have started waiting in the meantime

eventsLock.initialize();
flagdProviderSyncResources.initialize();

waitingThread.join();
}
Expand All @@ -110,7 +110,7 @@ void callingShutdown_wakesUpWaitingThreadWithException() throws InterruptedExcep
Thread waitingThread = new Thread(() -> {
long start = System.currentTimeMillis();
isWaiting.set(true);
Assertions.assertThrows(IllegalArgumentException.class, () -> eventsLock.waitForInitialization(10000));
Assertions.assertThrows(IllegalArgumentException.class, () -> flagdProviderSyncResources.waitForInitialization(10000));

long end = System.currentTimeMillis();
long duration = end - start;
Expand All @@ -124,17 +124,17 @@ void callingShutdown_wakesUpWaitingThreadWithException() throws InterruptedExcep

Thread.sleep(PERMISSIBLE_EPSILON); // waitingThread should have started waiting in the meantime

eventsLock.shutdown();
flagdProviderSyncResources.shutdown();

waitingThread.join();
}

@Timeout(2)
@Test
void waitForInitializationAfterCallingInitialize_returnsInstantly() {
eventsLock.initialize();
flagdProviderSyncResources.initialize();
long start = System.currentTimeMillis();
eventsLock.waitForInitialization(10000);
flagdProviderSyncResources.waitForInitialization(10000);
long end = System.currentTimeMillis();
// do not use PERMISSIBLE_EPSILON here, this should happen faster than that
Assertions.assertTrue(start + 1 >= end);
Expand Down

0 comments on commit 82b542b

Please sign in to comment.