Skip to content

Commit 4c0e009

Browse files
author
Mike Davis
committed
Fix blocking behavior when sending update notification. (#313)
1 parent 42f845c commit 4c0e009

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

Diff for: core-api/src/main/java/com/optimizely/ab/config/PollingProjectConfigManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ void setConfig(ProjectConfig projectConfig) {
9999
logger.info("New datafile set with revision: {}. Old revision: {}", projectConfig.getRevision(), previousRevision);
100100

101101
currentProjectConfig.set(projectConfig);
102-
notificationCenter.send(SIGNAL);
103102
countDownLatch.countDown();
103+
notificationCenter.send(SIGNAL);
104104
}
105105

106106
public NotificationCenter getNotificationCenter() {

Diff for: core-api/src/test/java/com/optimizely/ab/config/PollingProjectConfigManagerTest.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.Ignore;
2424
import org.junit.Test;
2525

26+
import java.util.concurrent.CompletableFuture;
2627
import java.util.concurrent.CountDownLatch;
2728
import java.util.concurrent.TimeUnit;
2829
import java.util.concurrent.atomic.AtomicBoolean;
@@ -195,6 +196,20 @@ public void testUpdateConfigNotificationGetsTriggered() throws InterruptedExcept
195196
assertTrue(countDownLatch.await(500, TimeUnit.MILLISECONDS));
196197
}
197198

199+
@Test
200+
public void testUpdateConfigNotificationDoesNotResultInDeadlock() throws Exception {
201+
NotificationCenter notificationCenter = new NotificationCenter();
202+
203+
TestProjectConfigManager testProjectConfigManager = new TestProjectConfigManager(projectConfig, TimeUnit.SECONDS.toMillis(10), notificationCenter);
204+
notificationCenter.getNotificationManager(UpdateConfigNotification.class)
205+
.addHandler(message -> {
206+
assertNotNull(testProjectConfigManager.getConfig());
207+
});
208+
209+
testProjectConfigManager.start();
210+
CompletableFuture.runAsync(testProjectConfigManager::getConfig).get(5, TimeUnit.SECONDS);
211+
}
212+
198213
private static class TestProjectConfigManager extends PollingProjectConfigManager {
199214
private final AtomicInteger counter = new AtomicInteger();
200215

@@ -206,7 +221,11 @@ private TestProjectConfigManager() {
206221
}
207222

208223
private TestProjectConfigManager(ProjectConfig projectConfig) {
209-
super(POLLING_PERIOD, POLLING_UNIT, POLLING_PERIOD / 2, POLLING_UNIT, new NotificationCenter());
224+
this(projectConfig, POLLING_PERIOD / 2, new NotificationCenter());
225+
}
226+
227+
private TestProjectConfigManager(ProjectConfig projectConfig, long blockPeriod, NotificationCenter notificationCenter) {
228+
super(POLLING_PERIOD, POLLING_UNIT, blockPeriod, POLLING_UNIT, notificationCenter);
210229
this.projectConfig = projectConfig;
211230
}
212231

0 commit comments

Comments
 (0)