Skip to content

Commit 058d941

Browse files
authored
fix: Catch timer exceptions (#85)
* fix: catch timer exception * test timer exception
1 parent 03ff8e9 commit 058d941

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/main/java/cloud/eppo/FetchConfigurationTask.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ public void scheduleNext() {
3232
long delay = this.intervalInMillis - jitter;
3333
FetchConfigurationTask nextTask =
3434
new FetchConfigurationTask(runnable, timer, intervalInMillis, jitterInMillis);
35-
timer.schedule(nextTask, delay);
35+
36+
try {
37+
timer.schedule(nextTask, delay);
38+
} catch (IllegalStateException e) {
39+
log.error("[Eppo SDK] Error scheduling next fetch task", e);
40+
}
3641
}
3742

3843
@Override

src/test/java/cloud/eppo/BaseEppoClientTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.*;
2323
import java.util.concurrent.CompletableFuture;
2424
import java.util.concurrent.CompletionException;
25+
import java.util.concurrent.atomic.AtomicInteger;
2526
import java.util.stream.Stream;
2627
import okhttp3.mockwebserver.MockResponse;
2728
import okhttp3.mockwebserver.MockWebServer;
@@ -570,6 +571,33 @@ public void testAssignmentLogErrorNonFatal() {
570571
verify(mockAssignmentLogger, times(1)).logAssignment(assignmentLogCaptor.capture());
571572
}
572573

574+
@Test
575+
public void testGracefulPolling() {
576+
Timer pollTimer = new Timer();
577+
final AtomicInteger callCount = new AtomicInteger(0);
578+
Runnable runnerTask =
579+
new Runnable() {
580+
@Override
581+
public void run() {
582+
callCount.incrementAndGet();
583+
}
584+
};
585+
586+
FetchConfigurationTask task = new FetchConfigurationTask(runnerTask, pollTimer, 50, 5);
587+
588+
// Trigger an unexpected state; the timer is cancelled but the FetchConfigurationTask attempts
589+
// to schedule a runnable.
590+
pollTimer.cancel();
591+
task.scheduleNext();
592+
593+
sleepUninterruptedly(50);
594+
595+
// If the Timer has been cancelled, FetchConfigurationTask doesn't attempt to reschedule.
596+
assertEquals(0, callCount.get());
597+
598+
// No exception to be thrown if illegal timer state is properly caught.
599+
}
600+
573601
@Test
574602
public void testPolling() {
575603
EppoHttpClient httpClient = mockHttpResponse(BOOL_FLAG_CONFIG);

0 commit comments

Comments
 (0)