From 8d7e5ab778b1056c783eba5280713f11057335a0 Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Thu, 16 Jan 2025 09:08:17 +0200 Subject: [PATCH 1/4] chore: pull engine version from yggdrasil --- pom.xml | 2 +- src/main/java/io/getunleash/metric/ClientMetrics.java | 3 ++- src/main/java/io/getunleash/metric/ClientRegistration.java | 3 ++- .../io/getunleash/metric/UnleashMetricServiceImplTest.java | 4 ++-- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 7e926ec38..d2e359186 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ io.getunleash yggdrasil-engine - 0.1.0-alpha.12 + 0.1.0-alpha.14 ${os.detected.classifier} diff --git a/src/main/java/io/getunleash/metric/ClientMetrics.java b/src/main/java/io/getunleash/metric/ClientMetrics.java index 8d70070a8..8b6bfdf85 100644 --- a/src/main/java/io/getunleash/metric/ClientMetrics.java +++ b/src/main/java/io/getunleash/metric/ClientMetrics.java @@ -1,6 +1,7 @@ package io.getunleash.metric; import io.getunleash.engine.MetricsBucket; +import io.getunleash.engine.UnleashEngine; import io.getunleash.event.UnleashEvent; import io.getunleash.event.UnleashSubscriber; import io.getunleash.lang.Nullable; @@ -25,7 +26,7 @@ public class ClientMetrics implements UnleashEvent { this.specVersion = config.getClientSpecificationVersion(); this.platformName = System.getProperty("java.vm.name"); this.platformVersion = System.getProperty("java.version"); - this.yggdrasilVersion = null; + this.yggdrasilVersion = UnleashEngine.getCoreVersion(); } public String getAppName() { diff --git a/src/main/java/io/getunleash/metric/ClientRegistration.java b/src/main/java/io/getunleash/metric/ClientRegistration.java index a820ccf92..8af6f0781 100644 --- a/src/main/java/io/getunleash/metric/ClientRegistration.java +++ b/src/main/java/io/getunleash/metric/ClientRegistration.java @@ -1,5 +1,6 @@ package io.getunleash.metric; +import io.getunleash.engine.UnleashEngine; import io.getunleash.event.UnleashEvent; import io.getunleash.event.UnleashSubscriber; import io.getunleash.lang.Nullable; @@ -31,7 +32,7 @@ public class ClientRegistration implements UnleashEvent { this.specVersion = config.getClientSpecificationVersion(); this.platformName = System.getProperty("java.vm.name"); this.platformVersion = System.getProperty("java.version"); - this.yggdrasilVersion = null; + this.yggdrasilVersion = UnleashEngine.getCoreVersion(); } public String getAppName() { diff --git a/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java b/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java index ea501186e..3b839d323 100644 --- a/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java +++ b/src/test/java/io/getunleash/metric/UnleashMetricServiceImplTest.java @@ -448,7 +448,7 @@ public void should_add_new_metrics_data_to_bucket() { verify(sender, times(1)).sendMetrics(metricsSent.capture()); ClientMetrics metrics = metricsSent.getValue(); assertThat(metrics.getSpecVersion()).isNotEmpty(); - assertThat(metrics.getYggdrasilVersion()).isNull(); + assertThat(metrics.getYggdrasilVersion()).isNotEmpty(); assertThat(metrics.getPlatformName()).isNotEmpty(); assertThat(metrics.getPlatformVersion()).isNotEmpty(); } @@ -467,6 +467,6 @@ public void client_registration_also_includes_new_metrics_metadata() { assertThat(reg.getPlatformName()).isNotEmpty(); assertThat(reg.getPlatformVersion()).isNotEmpty(); assertThat(reg.getSpecVersion()).isEqualTo(config.getClientSpecificationVersion()); - assertThat(reg.getYggdrasilVersion()).isNull(); + assertThat(reg.getYggdrasilVersion()).isNotEmpty(); } } From 5de3208174c770f590cb4e40077e6ba8ce216efd Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Thu, 16 Jan 2025 12:21:35 +0200 Subject: [PATCH 2/4] fix: error handling --- pom.xml | 2 +- src/main/java/io/getunleash/DefaultUnleash.java | 2 +- .../io/getunleash/repository/UnleashEngineStateHandler.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index d2e359186..010da0e1f 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ 5.10.3 4.12.0 UTF-8 - 5.1.7 + 5.1.9 2.17.2 1.3.14 diff --git a/src/main/java/io/getunleash/DefaultUnleash.java b/src/main/java/io/getunleash/DefaultUnleash.java index abf82cc3d..59e1aaab2 100644 --- a/src/main/java/io/getunleash/DefaultUnleash.java +++ b/src/main/java/io/getunleash/DefaultUnleash.java @@ -106,7 +106,7 @@ public DefaultUnleash( try { this.unleashEngine.takeState( JsonFeatureParser.toJsonString(featureCollection)); - } catch (YggdrasilInvalidInputException e) { + } catch (YggdrasilInvalidInputException | YggdrasilError e) { LOGGER.error("Unable to update features", e); } }); diff --git a/src/test/java/io/getunleash/repository/UnleashEngineStateHandler.java b/src/test/java/io/getunleash/repository/UnleashEngineStateHandler.java index 5b7ad6b95..715e34633 100644 --- a/src/test/java/io/getunleash/repository/UnleashEngineStateHandler.java +++ b/src/test/java/io/getunleash/repository/UnleashEngineStateHandler.java @@ -55,7 +55,7 @@ public MetricsBucket captureMetrics() { public void setState(String raw) { try { this.unleashEngine.takeState(raw); - } catch (YggdrasilInvalidInputException e) { + } catch (YggdrasilInvalidInputException | YggdrasilError e) { throw new RuntimeException(e); } } From 5641bf9acb2525ba74b9c718fa0645aa10d05d56 Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Thu, 16 Jan 2025 14:28:19 +0200 Subject: [PATCH 3/4] wip: add sleep because it looks like the failing test is a concurrency thing --- src/test/java/io/getunleash/DefaultUnleashTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/io/getunleash/DefaultUnleashTest.java b/src/test/java/io/getunleash/DefaultUnleashTest.java index 5344bf540..be9f891b5 100644 --- a/src/test/java/io/getunleash/DefaultUnleashTest.java +++ b/src/test/java/io/getunleash/DefaultUnleashTest.java @@ -310,6 +310,7 @@ public void asynchronous_fetch_on_initialisation_fails_silently_and_retries() .build(); Unleash unleash = new DefaultUnleash(config); + Thread.sleep(1); verify(fetcher, times(1)).fetchFeatures(); Thread.sleep(1200); verify(fetcher, times(2)).fetchFeatures(); From 91aa0348382eafe5362fe3478ce40be1e17d05f6 Mon Sep 17 00:00:00 2001 From: Simon Hornby Date: Thu, 16 Jan 2025 15:38:37 +0200 Subject: [PATCH 4/4] fix: make client spec tests predictably ordered (#262) --- .../integration/ClientSpecificationTest.java | 61 +------------------ 1 file changed, 3 insertions(+), 58 deletions(-) diff --git a/src/test/java/io/getunleash/integration/ClientSpecificationTest.java b/src/test/java/io/getunleash/integration/ClientSpecificationTest.java index 76f1cbff8..80a30e27f 100644 --- a/src/test/java/io/getunleash/integration/ClientSpecificationTest.java +++ b/src/test/java/io/getunleash/integration/ClientSpecificationTest.java @@ -1,16 +1,8 @@ package io.getunleash.integration; -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; -import static com.github.tomakehurst.wiremock.client.WireMock.get; -import static com.github.tomakehurst.wiremock.client.WireMock.post; -import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; -import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.github.tomakehurst.wiremock.junit5.WireMockExtension; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import io.getunleash.DefaultUnleash; @@ -21,8 +13,6 @@ import io.getunleash.strategy.constraints.DateParser; import io.getunleash.util.UnleashConfig; import java.io.BufferedReader; -import java.io.File; -import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; @@ -35,17 +25,9 @@ import java.util.stream.Stream; import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; -import org.junit.jupiter.api.extension.RegisterExtension; public class ClientSpecificationTest { - @RegisterExtension - static WireMockExtension serverMock = - WireMockExtension.newInstance() - .configureStaticDsl(true) - .options(wireMockConfig().dynamicPort().dynamicHttpsPort()) - .build(); - @TestFactory public Stream clientSpecification() throws IOException, URISyntaxException { Reader content = getFileReader("/client-specification/specifications/index.json"); @@ -122,20 +104,14 @@ private List createVariantTests(String fileName) } private Unleash setupUnleash(TestDefinition testDefinition) throws URISyntaxException { - mockUnleashAPI(testDefinition); - - // Required because the client is available before it may have had the chance to - // talk with - // the API - String backupFile = writeUnleashBackup(testDefinition); // Set-up a unleash instance, using mocked API and backup-file UnleashConfig config = UnleashConfig.builder() .appName(testDefinition.getName()) - .unleashAPI(new URI("http://localhost:" + serverMock.getPort() + "/api/")) - .synchronousFetchOnInitialisation(true) - .backupFile(backupFile) + .disableMetrics() + .disablePolling() + .unleashAPI(new URI("http://notusedbutrequired:9999/api/")) .build(); DefaultUnleash defaultUnleash = new DefaultUnleash(config); @@ -144,18 +120,6 @@ private Unleash setupUnleash(TestDefinition testDefinition) throws URISyntaxExce return defaultUnleash; } - private void mockUnleashAPI(TestDefinition definition) { - stubFor( - get(urlEqualTo("/api/client/features")) - .withHeader("Accept", equalTo("application/json")) - .willReturn( - aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withBody(definition.getState().toString()))); - stubFor(post(urlEqualTo("/api/client/register")).willReturn(aResponse().withStatus(200))); - } - private TestDefinition getTestDefinition(String fileName) throws IOException { Reader content = getFileReader("/client-specification/specifications/" + fileName); return new Gson().fromJson(content, TestDefinition.class); @@ -190,23 +154,4 @@ private Reader getFileReader(String filename) throws IOException { InputStreamReader reader = new InputStreamReader(in); return new BufferedReader(reader); } - - private String writeUnleashBackup(TestDefinition definition) { - String backupFile = - System.getProperty("java.io.tmpdir") - + File.separatorChar - + "unleash-test-" - + definition.getName() - + ".json"; - - // TODO: we can probably drop this after introduction of - // `synchronousFetchOnInitialisation`. - try (FileWriter writer = new FileWriter(backupFile)) { - writer.write(definition.getState().toString()); - } catch (IOException e) { - System.out.println("Unable to write toggles to file"); - } - - return backupFile; - } }