diff --git a/src/main/java/com/flagsmith/threads/PollingManager.java b/src/main/java/com/flagsmith/threads/PollingManager.java index 503791d5..1b5107a2 100644 --- a/src/main/java/com/flagsmith/threads/PollingManager.java +++ b/src/main/java/com/flagsmith/threads/PollingManager.java @@ -24,6 +24,7 @@ public PollingManager(FlagsmithClient client, Integer interval) { this.client = client; this.interval = interval * 1000; // converting to ms this.internalThread = initializeThread(); + client.updateEnvironment(); } /** diff --git a/src/test/java/com/flagsmith/FlagsmithClientTest.java b/src/test/java/com/flagsmith/FlagsmithClientTest.java index 76d7cb62..8183bfc3 100644 --- a/src/test/java/com/flagsmith/FlagsmithClientTest.java +++ b/src/test/java/com/flagsmith/FlagsmithClientTest.java @@ -549,4 +549,44 @@ public void testClose_ClosesFlagsmithSdk() { // Then verify(mockedApiWrapper, times(1)).close(); } + + @Test(groups = "unit") + public void testLocalEvaluation_ReturnsConsistentResults() throws FlagsmithClientError { + // Specific test to ensure that results are consistent when making multiple calls to + // evaluate flags soon after the client is instantiated. + + // Given + EnvironmentModel environmentModel = FlagsmithTestHelper.environmentModel(); + + FlagsmithConfig config = FlagsmithConfig.newBuilder().withLocalEvaluation(true).build(); + + FlagsmithApiWrapper mockedApiWrapper = mock(FlagsmithApiWrapper.class); + when(mockedApiWrapper.getEnvironment()) + .thenReturn(environmentModel) + .thenReturn(null); + when(mockedApiWrapper.getConfig()).thenReturn(config); + + FlagsmithClient client = FlagsmithClient.newBuilder() + .withFlagsmithApiWrapper(mockedApiWrapper) + .withConfiguration(config) + .setApiKey("ser.dummy-key") + .build(); + + // When + // make 3 calls to get identity flags + List results = new ArrayList<>(); + for (int i = 0; i < 3; ++i) { + results.add(client.getIdentityFlags("some-identity")); + } + + // Then + // iterate over the results list and verify that the results are all the same + Boolean expectedState = true; + String expectedValue = "some-value"; + + for (Flags flags : results) { + Assert.assertEquals(flags.isFeatureEnabled("some_feature"), expectedState); + Assert.assertEquals(flags.getFeatureValue("some_feature"), expectedValue); + } + } } diff --git a/src/test/java/com/flagsmith/threads/PollingManagerTest.java b/src/test/java/com/flagsmith/threads/PollingManagerTest.java index 32bf8d74..0d4846cd 100644 --- a/src/test/java/com/flagsmith/threads/PollingManagerTest.java +++ b/src/test/java/com/flagsmith/threads/PollingManagerTest.java @@ -21,18 +21,20 @@ public void init() { manager.startPolling(); } - public void PollingManagerTest_checkPollingMethodInvoked() throws InterruptedException { - Thread.sleep(50); + public void testPollingManager_checkPollingMethodInvoked() throws InterruptedException { verify(client, times(1)).updateEnvironment(); - Thread.sleep(1500); + Thread.sleep(50); verify(client, times(2)).updateEnvironment(); + Thread.sleep(1500); + verify(client, times(3)).updateEnvironment(); } - public void PollingManagerTest_checkPollingMethodInvokedAndStopped() throws InterruptedException { - Thread.sleep(50); + public void testPollingManager_checkPollingMethodInvokedAndStopped() throws InterruptedException { verify(client, times(1)).updateEnvironment(); + Thread.sleep(50); + verify(client, times(2)).updateEnvironment(); manager.stopPolling(); Thread.sleep(1500); - verify(client, times(1)).updateEnvironment(); + verify(client, times(2)).updateEnvironment(); } }