Skip to content

Commit 6a19cc5

Browse files
authored
fix: better sequencing in flakey test (#169)
* chore: update deprecated spotless rule * adjust polling test to better await underlying mechanisms and hopefully reduce flakiness
1 parent 3983f2f commit 6a19cc5

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

eppo/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ spotless {
9393
target '*.gradle', '.gitattributes', '.gitignore'
9494

9595
trimTrailingWhitespace()
96-
indentWithSpaces(2)
96+
leadingTabsToSpaces(2)
9797
endWithNewline()
9898
}
9999
java {

eppo/src/androidTest/java/cloud/eppo/android/EppoClientTest.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import static org.junit.Assert.fail;
1212
import static org.mockito.ArgumentMatchers.any;
1313
import static org.mockito.ArgumentMatchers.anyString;
14-
import static org.mockito.Mockito.atLeast;
1514
import static org.mockito.Mockito.doThrow;
1615
import static org.mockito.Mockito.mock;
1716
import static org.mockito.Mockito.spy;
@@ -54,6 +53,7 @@
5453
import java.util.Locale;
5554
import java.util.Map;
5655
import java.util.concurrent.CompletableFuture;
56+
import java.util.concurrent.CountDownLatch;
5757
import java.util.concurrent.ExecutionException;
5858
import java.util.concurrent.TimeUnit;
5959
import java.util.concurrent.TimeoutException;
@@ -354,14 +354,27 @@ private void testLoadConfigurationHelper(boolean loadAsync)
354354

355355
@Test
356356
public void testPollingClient() throws ExecutionException, InterruptedException {
357-
// Set up a changing response from the "server"
358357
EppoHttpClient mockHttpClient = mock(EppoHttpClient.class);
359358

360-
// Mock sync get
361-
when(mockHttpClient.get(anyString())).thenReturn(EMPTY_CONFIG);
359+
CountDownLatch pollLatch = new CountDownLatch(1);
360+
361+
// The poller fetches synchronously so let's return the boolean flag config
362+
when(mockHttpClient.get(anyString()))
363+
.thenAnswer(
364+
invocation -> {
365+
pollLatch.countDown(); // Signal that polling occurred
366+
Log.d("TEST", "Polling has occurred");
367+
return BOOL_FLAG_CONFIG;
368+
});
369+
370+
// Async get is used for initialization, so we'll return an empty response.
371+
CompletableFuture<byte[]> emptyResponse =
372+
CompletableFuture.supplyAsync(
373+
() -> {
374+
Log.d("TEST", "empty config supplied");
375+
return EMPTY_CONFIG;
376+
});
362377

363-
// Mock async get
364-
CompletableFuture<byte[]> emptyResponse = CompletableFuture.completedFuture(EMPTY_CONFIG);
365378
when(mockHttpClient.getAsync(anyString())).thenReturn(emptyResponse);
366379

367380
setBaseClientHttpClientOverrideField(mockHttpClient);
@@ -375,18 +388,21 @@ public void testPollingClient() throws ExecutionException, InterruptedException
375388
.pollingIntervalMs(pollingIntervalMs)
376389
.isGracefulMode(false);
377390

378-
// Initialize and no exception should be thrown.
379391
EppoClient eppoClient = clientBuilder.buildAndInitAsync().get();
380392

393+
// Empty config on initialization
381394
verify(mockHttpClient, times(1)).getAsync(anyString());
382395
assertFalse(eppoClient.getBooleanAssignment("bool_flag", "subject1", false));
383396

384-
// Now, return the boolean flag config (bool_flag = true)
385-
when(mockHttpClient.get(anyString())).thenReturn(BOOL_FLAG_CONFIG);
386-
// Wait the polling interval
387-
Thread.sleep(pollingIntervalMs * 2);
397+
// Wait for the client to send the "fetch"
398+
assertTrue("Polling did not occur within timeout", pollLatch.await(5, TimeUnit.SECONDS));
388399

389-
verify(mockHttpClient, atLeast(1)).get(anyString());
400+
// Sleep a short period to allow the polling mechanism to complete writing the config.
401+
// The above latch releases immediately after the config is "fetched", not necessarily before
402+
// the config is applied.
403+
Thread.sleep(150);
404+
405+
// Assignment is now true.
390406
assertTrue(eppoClient.getBooleanAssignment("bool_flag", "subject1", false));
391407

392408
eppoClient.stopPolling();
@@ -1048,7 +1064,7 @@ public static <T> void setBaseClientOverrideField(String fieldName, T override)
10481064
private static final byte[] BOOL_FLAG_CONFIG =
10491065
("{\n"
10501066
+ " \"createdAt\": \"2024-04-17T19:40:53.716Z\",\n"
1051-
+ " \"format\": \"SERVER\",\n"
1067+
+ " \"format\": \"CLIENT\",\n"
10521068
+ " \"environment\": {\n"
10531069
+ " \"name\": \"Test\"\n"
10541070
+ " },\n"

0 commit comments

Comments
 (0)