Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/com/flagsmith/threads/AnalyticsProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private HttpUrl getAnalyticsUrl() {
/**
* Push the analytics to the server.
*/
public void flush() {
public synchronized void flush() {
// Make sure analytics data is only flushed once.
if (isFlushing.compareAndSet(false, true)) {
if (analyticsData.isEmpty()) {
Expand Down Expand Up @@ -161,7 +161,12 @@ private void setNextFlush() {
nextFlush = Instant.now().getEpochSecond() + analyticsTimer;
}

public void close() {
/**
* Send the analytics data still buffered, then release the request processor.
* Waits for a flush running on another thread to hand over its request first.
*/
public synchronized void close() {
this.flush();
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
this.requestProcessor.close();
}
}
47 changes: 47 additions & 0 deletions src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -16,13 +18,16 @@
import com.flagsmith.config.FlagsmithConfig;
import com.flagsmith.config.Retry;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import lombok.SneakyThrows;
import okhttp3.Response;
import okhttp3.mock.MockInterceptor;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

public class AnalyticsProcessorTest {

Expand Down Expand Up @@ -100,6 +105,48 @@ public void testClose_ShutsDownExecutor() {
verify(requestProcessor, times(1)).close();
}

@Test
public void testClose_FlushesPendingAnalyticsBeforeShutdown() {
analytics.trackFeature("foo");
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
Assertions.assertFalse(analytics.getAnalyticsData().isEmpty());

analytics.close();

InOrder inOrder = inOrder(requestProcessor);
inOrder.verify(requestProcessor).executeAsync(any(), any());
inOrder.verify(requestProcessor).close();
Assertions.assertTrue(analytics.getAnalyticsData().isEmpty());
}

@Test
@SneakyThrows
public void testClose_WaitsForActiveFlushBeforeShutdown() {
CountDownLatch flushStarted = new CountDownLatch(1);
CountDownLatch finishFlush = new CountDownLatch(1);
when(api.newPostRequest(any(), any())).thenAnswer(invocation -> {
flushStarted.countDown();
finishFlush.await(5, TimeUnit.SECONDS);
return null;
});
analytics.trackFeature("foo");
Thread flusher = new Thread(analytics::flush);
flusher.start();
Assertions.assertTrue(flushStarted.await(5, TimeUnit.SECONDS));

Thread closer = new Thread(analytics::close);
closer.start();
closer.join(500);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: The 500 ms join does not prove closer reached close(); if it is scheduled only after finishFlush.countDown(), an implementation that shuts down immediately can still pass the final ordering check. Wait until the closer is demonstrably blocked on the flush monitor before releasing it, so the test reliably exercises this race.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java, line 138:

<comment>The 500 ms join does not prove `closer` reached `close()`; if it is scheduled only after `finishFlush.countDown()`, an implementation that shuts down immediately can still pass the final ordering check. Wait until the closer is demonstrably blocked on the flush monitor before releasing it, so the test reliably exercises this race.</comment>

<file context>
@@ -114,6 +118,35 @@ public void testClose_FlushesPendingAnalyticsBeforeShutdown() {
+
+    Thread closer = new Thread(analytics::close);
+    closer.start();
+    closer.join(500);
+    verify(requestProcessor, never()).close();
+
</file context>
Suggested change
closer.join(500);
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
while (closer.getState() != Thread.State.BLOCKED && System.nanoTime() < deadline) {
Thread.yield();
}
Assertions.assertEquals(Thread.State.BLOCKED, closer.getState());

verify(requestProcessor, never()).close();

finishFlush.countDown();
flusher.join(5000);
closer.join(5000);

InOrder inOrder = inOrder(requestProcessor);
inOrder.verify(requestProcessor).executeAsync(any(), any());
inOrder.verify(requestProcessor).close();
}

@Test
@SneakyThrows
public void AnalyticsProcessor_longAdderGetsSerializedCorrectly() {
Expand Down