Skip to content

Commit 82b542b

Browse files
committed
fixup! feat: improve wait logic to a more elegant solution open-feature#1160
Signed-off-by: christian.lutnik <[email protected]>
1 parent 12e889f commit 82b542b

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

providers/flagd/src/main/java/dev/openfeature/contrib/providers/flagd/FlagdProviderSyncResources.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.openfeature.contrib.providers.flagd;
22

3+
import com.google.common.annotations.VisibleForTesting;
34
import dev.openfeature.sdk.EvaluationContext;
45
import dev.openfeature.sdk.ImmutableContext;
56
import dev.openfeature.sdk.ImmutableStructure;
@@ -11,9 +12,10 @@
1112

1213
/**
1314
* Contains all fields we need to worry about locking, used as intrinsic lock
14-
* for sync blocks.
15+
* for sync blocks in the {@link FlagdProvider}.
1516
*/
1617
@Getter
18+
@VisibleForTesting
1719
public class FlagdProviderSyncResources {
1820
@Setter
1921
private volatile ProviderEvent previousEvent = null;

providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/resolver/common/EventsLockTest.java renamed to providers/flagd/src/test/java/dev/openfeature/contrib/providers/flagd/resolver/common/FlagdProviderSyncResourcesTest.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99
import org.junit.jupiter.api.Test;
1010
import org.junit.jupiter.api.Timeout;
1111

12-
class EventsLockTest {
12+
class FlagdProviderSyncResourcesTest {
1313
private static final long PERMISSIBLE_EPSILON = 20;
1414

15-
private FlagdProviderSyncResources eventsLock;
15+
private FlagdProviderSyncResources flagdProviderSyncResources;
1616

1717
@BeforeEach
1818
void setUp() {
19-
eventsLock = new FlagdProviderSyncResources();
19+
flagdProviderSyncResources = new FlagdProviderSyncResources();
2020
}
2121

2222
@Timeout(2)
2323
@Test
2424
void waitForInitialization_failsWhenDeadlineElapses() {
25-
Assertions.assertThrows(GeneralError.class, () -> eventsLock.waitForInitialization(2));
25+
Assertions.assertThrows(GeneralError.class, () -> flagdProviderSyncResources.waitForInitialization(2));
2626
}
2727

2828
@Timeout(2)
@@ -34,7 +34,7 @@ void waitForInitialization_waitsApproxForDeadline() {
3434
Assertions.assertThrows(GeneralError.class, () -> {
3535
start.set(System.currentTimeMillis());
3636
try {
37-
eventsLock.waitForInitialization(deadline);
37+
flagdProviderSyncResources.waitForInitialization(deadline);
3838
} catch (Exception e) {
3939
end.set(System.currentTimeMillis());
4040
throw e;
@@ -55,7 +55,7 @@ void interruptingWaitingThread_isIgnored() throws InterruptedException {
5555
Thread waitingThread = new Thread(() -> {
5656
long start = System.currentTimeMillis();
5757
isWaiting.set(true);
58-
eventsLock.waitForInitialization(deadline);
58+
flagdProviderSyncResources.waitForInitialization(deadline);
5959
long end = System.currentTimeMillis();
6060
long duration = end - start;
6161
// even though thread was interrupted, it still waited for the deadline
@@ -85,7 +85,7 @@ void callingInitialize_wakesUpWaitingThread() throws InterruptedException {
8585
Thread waitingThread = new Thread(() -> {
8686
long start = System.currentTimeMillis();
8787
isWaiting.set(true);
88-
eventsLock.waitForInitialization(10000);
88+
flagdProviderSyncResources.waitForInitialization(10000);
8989
long end = System.currentTimeMillis();
9090
long duration = end - start;
9191
Assertions.assertTrue(duration < PERMISSIBLE_EPSILON);
@@ -98,7 +98,7 @@ void callingInitialize_wakesUpWaitingThread() throws InterruptedException {
9898

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

101-
eventsLock.initialize();
101+
flagdProviderSyncResources.initialize();
102102

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

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

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

127-
eventsLock.shutdown();
127+
flagdProviderSyncResources.shutdown();
128128

129129
waitingThread.join();
130130
}
131131

132132
@Timeout(2)
133133
@Test
134134
void waitForInitializationAfterCallingInitialize_returnsInstantly() {
135-
eventsLock.initialize();
135+
flagdProviderSyncResources.initialize();
136136
long start = System.currentTimeMillis();
137-
eventsLock.waitForInitialization(10000);
137+
flagdProviderSyncResources.waitForInitialization(10000);
138138
long end = System.currentTimeMillis();
139139
// do not use PERMISSIBLE_EPSILON here, this should happen faster than that
140140
Assertions.assertTrue(start + 1 >= end);

0 commit comments

Comments
 (0)