Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that environment is updated on polling manager start #99

Merged
Merged
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
1 change: 1 addition & 0 deletions src/main/java/com/flagsmith/threads/PollingManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/flagsmith/FlagsmithClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Flags> 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);
}
}
}
14 changes: 8 additions & 6 deletions src/test/java/com/flagsmith/threads/PollingManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}