Skip to content

Commit b293e4d

Browse files
authored
feat: backup file on streaming (#323)
1 parent 7dfe36a commit b293e4d

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

.github/workflows/pull_requests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ jobs:
2121
- name: Build, test, coverage
2222
run: ./mvnw clean test jacoco:report
2323
- name: Coveralls parallel
24+
if: matrix.os != 'macos'
2425
uses: coverallsapp/github-action@v2
2526
with:
2627
github-token: ${{ secrets.GITHUB_TOKEN }}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
<dependency>
5959
<groupId>io.getunleash</groupId>
6060
<artifactId>yggdrasil-engine</artifactId>
61-
<version>0.3.2</version>
61+
<version>0.4.2</version>
6262
</dependency>
6363
<dependency>
6464
<groupId>com.google.code.gson</groupId>

src/main/java/io/getunleash/repository/FeatureRepositoryImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,12 @@ public Stream<FeatureDefinition> listKnownToggles() {
237237
return this.engine.listKnownToggles().stream().map(FeatureDefinition::new);
238238
}
239239

240-
private synchronized void handleStreamingUpdate(String data) {
240+
synchronized void handleStreamingUpdate(String data) {
241241
try {
242242
engine.takeState(data);
243-
// TODO: write backup when engine exposes current state
243+
244+
String currentState = engine.getState();
245+
featureBackupHandler.write(currentState);
244246

245247
ClientFeaturesResponse response = ClientFeaturesResponse.updated(data);
246248
eventDispatcher.dispatch(response);

src/test/java/io/getunleash/repository/FeatureRepositoryTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,50 @@ public void server_errors_should_incrementally_increase_interval()
468468
assertThat(featureRepository.getFailures()).isEqualTo(0);
469469
}
470470

471+
@Test
472+
public void should_write_backup_file_when_streaming_update_received() throws Exception {
473+
UnleashConfig streamingConfig =
474+
UnleashConfig.builder()
475+
.appName("streaming-test")
476+
.unleashAPI("http://localhost:4242/api/")
477+
.scheduledExecutor(mock(UnleashScheduledExecutor.class))
478+
.disableMetrics()
479+
.disablePolling()
480+
.experimentalStreamingMode()
481+
.build();
482+
483+
when(backupHandler.read()).thenReturn(Optional.empty());
484+
485+
FeatureRepositoryImpl repository =
486+
new FeatureRepositoryImpl(
487+
streamingConfig,
488+
backupHandler,
489+
new UnleashEngine(),
490+
fetcher,
491+
streamingFetcher,
492+
bootstrapHandler);
493+
494+
String streamingData =
495+
"{\"events\":[{\"type\":\"hydration\",\"eventId\":1,\"features\":[{\"name\":\"testFeature\",\"enabled\":true,\"strategies\":[],\"variants\":[]}],\"segments\":[]}]}";
496+
497+
repository.handleStreamingUpdate(streamingData);
498+
499+
ArgumentCaptor<String> backupContentCaptor = ArgumentCaptor.forClass(String.class);
500+
verify(backupHandler, times(1)).write(backupContentCaptor.capture());
501+
502+
String savedBackupContent = backupContentCaptor.getValue();
503+
504+
assertThat(savedBackupContent).contains("\"features\"");
505+
assertThat(savedBackupContent).contains("\"segments\"");
506+
507+
assertThat(savedBackupContent).contains("\"testFeature\"");
508+
509+
assertThat(savedBackupContent).contains("\"version\":2");
510+
assertThat(savedBackupContent).contains("\"query\":");
511+
512+
assertThat(savedBackupContent).doesNotContain("\"events\""); // store state not events
513+
}
514+
471515
private class TestRunner {
472516

473517
private final UnleashScheduledExecutor executor;

0 commit comments

Comments
 (0)