diff --git a/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java b/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java index da631f5..6ce4268 100644 --- a/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java +++ b/src/main/java/com/flagsmith/threads/AnalyticsProcessor.java @@ -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()) { @@ -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(); this.requestProcessor.close(); } } diff --git a/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java b/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java index 6895f4b..660b26f 100644 --- a/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java +++ b/src/test/java/com/flagsmith/threads/AnalyticsProcessorTest.java @@ -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; @@ -16,6 +18,8 @@ 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; @@ -23,6 +27,7 @@ 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 { @@ -100,6 +105,48 @@ public void testClose_ShutsDownExecutor() { verify(requestProcessor, times(1)).close(); } + @Test + public void testClose_FlushesPendingAnalyticsBeforeShutdown() { + analytics.trackFeature("foo"); + 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); + 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() {