Skip to content

Commit fc5c528

Browse files
authored
fix: java8 incompatibility in recent change (#152)
* fix java8 compat * lint * check value equals in putIfAbsent logic
1 parent 43c6641 commit fc5c528

File tree

2 files changed

+47
-28
lines changed

2 files changed

+47
-28
lines changed

src/main/java/cloud/eppo/api/AbstractAssignmentCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public boolean putIfAbsent(String cacheKey, @NotNull String serializedEntry) {
4646
boolean hadNoPreviousEntry;
4747
synchronized (delegate) {
4848
String entry = delegate.get(cacheKey);
49-
hadNoPreviousEntry = entry == null;
49+
hadNoPreviousEntry = entry == null || !entry.equals(serializedEntry);
5050
if (hadNoPreviousEntry) {
5151
delegate.put(cacheKey, serializedEntry);
5252
}

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

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -573,36 +573,45 @@ public void testAssignmentEventCorrectlyDeduplicatedFromBackgroundThreads() {
573573
int numThreads = 10;
574574
final CountDownLatch threadStartCountDownLatch = new CountDownLatch(numThreads);
575575
final CountDownLatch getAssignmentStartCountDownLatch = new CountDownLatch(1);
576-
final List<Integer> assignments = Collections.synchronizedList(Arrays.asList(new Integer[numThreads]));
577-
try (ExecutorService pool = Executors.newFixedThreadPool(numThreads, new ThreadFactory() {
578-
private final AtomicInteger threadIndexAtomicInteger = new AtomicInteger(0);
579-
@Override
580-
public Thread newThread(@NotNull Runnable runnable) {
581-
final int threadIndex = threadIndexAtomicInteger.getAndIncrement();
582-
return new Thread(runnable, "testAssignmentEventCorrectlyDeduplicatedFromBackgroundThreads-" + threadIndex);
583-
}
584-
})) {
576+
final List<Integer> assignments =
577+
Collections.synchronizedList(Arrays.asList(new Integer[numThreads]));
578+
ExecutorService pool =
579+
Executors.newFixedThreadPool(
580+
numThreads,
581+
new ThreadFactory() {
582+
private final AtomicInteger threadIndexAtomicInteger = new AtomicInteger(0);
583+
584+
@Override
585+
public Thread newThread(@NotNull Runnable runnable) {
586+
final int threadIndex = threadIndexAtomicInteger.getAndIncrement();
587+
return new Thread(
588+
runnable,
589+
"testAssignmentEventCorrectlyDeduplicatedFromBackgroundThreads-" + threadIndex);
590+
}
591+
});
592+
try {
585593
for (int i = 0; i < numThreads; i += 1) {
586594
final int threadIndex = i;
587595
pool.execute(
588-
() -> {
589-
threadStartCountDownLatch.countDown();
590-
boolean shouldStart;
591-
try {
592-
shouldStart = getAssignmentStartCountDownLatch.await(1000, TimeUnit.SECONDS);
593-
} catch (InterruptedException ignored) {
594-
shouldStart = false;
595-
}
596-
final Integer assignment;
597-
if (shouldStart) {
598-
assignment = eppoClient.getIntegerAssignment("numeric-one-of", "alice", subjectAttributes, 0);
599-
} else {
600-
assignment = null;
601-
}
602-
603-
assignments.set(threadIndex, assignment);
604-
}
605-
);
596+
() -> {
597+
threadStartCountDownLatch.countDown();
598+
boolean shouldStart;
599+
try {
600+
shouldStart = getAssignmentStartCountDownLatch.await(1000, TimeUnit.SECONDS);
601+
} catch (InterruptedException ignored) {
602+
shouldStart = false;
603+
}
604+
final Integer assignment;
605+
if (shouldStart) {
606+
assignment =
607+
eppoClient.getIntegerAssignment(
608+
"numeric-one-of", "alice", subjectAttributes, 0);
609+
} else {
610+
assignment = null;
611+
}
612+
613+
assignments.set(threadIndex, assignment);
614+
});
606615
}
607616

608617
boolean shouldStart;
@@ -614,6 +623,16 @@ public Thread newThread(@NotNull Runnable runnable) {
614623

615624
assertTrue(shouldStart, "All worker threads did not start");
616625
getAssignmentStartCountDownLatch.countDown();
626+
} finally {
627+
pool.shutdown();
628+
try {
629+
if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {
630+
pool.shutdownNow();
631+
}
632+
} catch (InterruptedException e) {
633+
pool.shutdownNow();
634+
Thread.currentThread().interrupt();
635+
}
617636
}
618637

619638
final List<Integer> expectedAssignments;

0 commit comments

Comments
 (0)