From 333be1d25cce2f80df53dd3576edfca955cfdd09 Mon Sep 17 00:00:00 2001 From: Roger Floriano <31597636+petruki@users.noreply.github.com> Date: Mon, 13 Oct 2025 17:03:34 -0700 Subject: [PATCH 1/3] feat: changed flush to clean execution history too (#379) --- .../client/model/SwitcherBuilder.java | 10 ++-- .../client/model/SwitcherRequest.java | 3 -- .../SwitcherBasicCriteriaResponseTest.java | 3 +- .../switcherapi/client/SwitcherBasicTest.java | 3 +- .../client/SwitcherContextBuilderTest.java | 5 +- .../SwitcherContextRemoteExecutorTest.java | 5 +- .../switcherapi/client/SwitcherFail1Test.java | 3 +- .../switcherapi/client/SwitcherFail2Test.java | 3 +- .../client/SwitcherSilentModeTest.java | 3 +- .../client/SwitcherSnapshotLookupTest.java | 3 +- .../SwitcherSnapshotValidationFailTest.java | 3 +- .../SwitcherSnapshotValidationTest.java | 7 +-- .../client/SwitcherThrottleTest.java | 46 +++++++++---------- .../client/SwitcherValidateTest.java | 3 +- .../local/SwitcherLocalServiceTest.java | 3 +- .../utils/SnapshotWatcherWorkerTest.java | 4 +- .../client/utils/SwitcherUtilsTest.java | 4 +- 17 files changed, 61 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java b/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java index 0e17344..70d325f 100644 --- a/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java +++ b/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java @@ -4,9 +4,7 @@ import com.switcherapi.client.exception.SwitcherContextException; import org.apache.commons.lang3.StringUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.util.*; /** * Builder class that simplifies how input are programmatically wrapped inside the Switcher. @@ -17,6 +15,8 @@ public abstract class SwitcherBuilder implements Switcher { protected final SwitcherProperties properties; + + protected final Map, SwitcherResult> historyExecution; protected long delay; @@ -32,16 +32,18 @@ public abstract class SwitcherBuilder implements Switcher { protected SwitcherBuilder(final SwitcherProperties properties) { this.properties = properties; + this.historyExecution = new HashMap<>(); this.entry = new ArrayList<>(); this.delay = 0; } /** - * Clear all entries previously added + * Clear all entries previously added and history of executions. * * @return {@link SwitcherBuilder} */ public SwitcherBuilder flush() { + this.historyExecution.clear(); this.entry.clear(); return this; } diff --git a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java index 8025488..b698970 100644 --- a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java +++ b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java @@ -23,8 +23,6 @@ public final class SwitcherRequest extends SwitcherBuilder { private final SwitcherExecutor switcherExecutor; private final String switcherKey; - - private final Map, SwitcherResult> historyExecution; private AsyncSwitcher asyncSwitcher; @@ -41,7 +39,6 @@ public SwitcherRequest(final String switcherKey, super(switcherProperties); this.switcherExecutor = switcherExecutor; this.switcherKey = switcherKey; - this.historyExecution = new HashMap<>(); } @Override diff --git a/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java b/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java index 8e6e600..b71f862 100644 --- a/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java @@ -15,6 +15,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherBasicCriteriaResponseTest extends MockWebServerHelper { @@ -31,7 +32,7 @@ static void setup() throws IOException { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherBasicTest.java b/src/test/java/com/switcherapi/client/SwitcherBasicTest.java index e73db94..ec6dba0 100644 --- a/src/test/java/com/switcherapi/client/SwitcherBasicTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherBasicTest.java @@ -11,6 +11,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -28,7 +29,7 @@ static void setup() throws IOException { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java b/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java index c7a46f1..dc5f2bb 100644 --- a/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherContextBuilderTest.java @@ -11,6 +11,7 @@ import static com.switcherapi.SwitchersBase.*; import static com.switcherapi.client.SwitcherContextValidator.ERR_LOCAL; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherContextBuilderTest { @@ -26,7 +27,7 @@ void shouldReturnSuccess() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client") - .environment("default") + .environment(DEFAULT_ENV) .snapshotLocation(SNAPSHOTS_LOCAL) .local(true)); @@ -46,7 +47,7 @@ void shouldReturnError_snapshotNotLoaded() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client") - .environment("default") + .environment(DEFAULT_ENV) .snapshotLocation(null) .local(true)); diff --git a/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java b/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java index 32c46e9..504b22e 100644 --- a/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherContextRemoteExecutorTest.java @@ -11,6 +11,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherContextRemoteExecutorTest extends MockWebServerHelper { @@ -40,7 +41,7 @@ void shouldConfigureRemotePoolSize() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client-pool-test") - .environment("default") + .environment(DEFAULT_ENV) .poolConnectionSize(1) .local(false)); @@ -66,7 +67,7 @@ void shouldConfigureRemoteTimeout() { .apiKey("API_KEY") .domain("switcher-domain") .component("switcher-client-timeout-test") - .environment("default") + .environment(DEFAULT_ENV) .timeoutMs(500) .local(false)); diff --git a/src/test/java/com/switcherapi/client/SwitcherFail1Test.java b/src/test/java/com/switcherapi/client/SwitcherFail1Test.java index 639b9a2..77e2a37 100644 --- a/src/test/java/com/switcherapi/client/SwitcherFail1Test.java +++ b/src/test/java/com/switcherapi/client/SwitcherFail1Test.java @@ -14,6 +14,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherFail1Test extends MockWebServerHelper { @@ -43,7 +44,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherFail2Test.java b/src/test/java/com/switcherapi/client/SwitcherFail2Test.java index e85123a..f8cbe96 100644 --- a/src/test/java/com/switcherapi/client/SwitcherFail2Test.java +++ b/src/test/java/com/switcherapi/client/SwitcherFail2Test.java @@ -13,6 +13,7 @@ import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -40,7 +41,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java b/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java index e1bd267..1b7d2a1 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSilentModeTest.java @@ -14,6 +14,7 @@ import java.io.IOException; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertTrue; class SwitcherSilentModeTest extends MockWebServerHelper { @@ -41,7 +42,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java b/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java index 45aa5aa..07241d4 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSnapshotLookupTest.java @@ -14,6 +14,7 @@ import java.nio.file.Files; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -52,7 +53,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java index 6fdd407..42e6b8c 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationFailTest.java @@ -12,6 +12,7 @@ import java.nio.file.Files; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -46,7 +47,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java index 10c9fd7..5900e94 100644 --- a/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherSnapshotValidationTest.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.nio.file.Paths; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherSnapshotValidationTest extends MockWebServerHelper { @@ -43,7 +44,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); @@ -105,7 +106,7 @@ void shouldValidateAndLoadSnapshot_whenLocal() { .local(true) .snapshotAutoLoad(false) .snapshotLocation(RESOURCES_PATH) - .environment("default")); + .environment(DEFAULT_ENV)); Switchers.initializeClient(); @@ -129,7 +130,7 @@ void shouldNotValidateAndLoadSnapshot_serviceUnavailable() { .local(false) .snapshotAutoLoad(false) .snapshotLocation(RESOURCES_PATH) - .environment("default")); + .environment(DEFAULT_ENV)); Switchers.initializeClient(); diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java index f10ce77..25c2d73 100644 --- a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java @@ -1,27 +1,35 @@ package com.switcherapi.client; -import com.switcherapi.Switchers; +import com.switcherapi.SwitchersBase; import com.switcherapi.client.model.Switcher; import com.switcherapi.client.model.SwitcherBuilder; import com.switcherapi.fixture.CountDownHelper; import com.switcherapi.fixture.MockWebServerHelper; import mockwebserver3.QueueDispatcher; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import java.io.IOException; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class SwitcherThrottleTest extends MockWebServerHelper { @BeforeAll static void setup() throws IOException { MockWebServerHelper.setupMockServer(); - - Switchers.loadProperties(); - Switchers.configure(ContextBuilder.builder().url(String.format("http://localhost:%s", mockBackEnd.getPort()))); + + SwitchersBase.configure(ContextBuilder.builder(true) + .context(SwitchersBase.class.getName()) + .url(String.format("http://localhost:%s", mockBackEnd.getPort())) + .apiKey("TEST_API_KEY") + .domain("TEST_DOMAIN") + .component("TEST_COMPONENT") + .environment(DEFAULT_ENV)); } @AfterAll @@ -32,22 +40,11 @@ static void tearDown() { @BeforeEach void resetSwitcherContextState() { ((QueueDispatcher) mockBackEnd.getDispatcher()).clear(); - - Switchers.configure(ContextBuilder.builder() - .local(false) - .snapshotLocation(null) - .snapshotSkipValidation(false) - .environment("default") - .silentMode(null) - .snapshotAutoLoad(false) - .snapshotAutoUpdateInterval(null)); + SwitchersBase.initializeClient(); } @Test - @Order(1) void shouldReturnTrue_withThrottle() { - Switchers.initializeClient(); - // Initial remote call givenResponse(generateMockAuth(10)); //auth givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached) @@ -57,8 +54,9 @@ void shouldReturnTrue_withThrottle() { givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background) //test - Switcher switcher = Switchers - .getSwitcher(Switchers.REMOTE_KEY) + Switcher switcher = SwitchersBase + .getSwitcher(SwitchersBase.USECASE11) + .flush() .checkValue("value") .throttle(1000); @@ -71,10 +69,7 @@ void shouldReturnTrue_withThrottle() { } @Test - @Order(2) void shouldRetrieveNewResponse_whenStrategyInputChanged() { - Switchers.initializeClient(); - // Initial remote call givenResponse(generateMockAuth(10)); //auth givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached) @@ -84,8 +79,9 @@ void shouldRetrieveNewResponse_whenStrategyInputChanged() { givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background) //test - SwitcherBuilder switcher = Switchers - .getSwitcher(Switchers.REMOTE_KEY) + SwitcherBuilder switcher = SwitchersBase + .getSwitcher(SwitchersBase.USECASE11) + .flush() .throttle(1000); for (int i = 0; i < 100; i++) { diff --git a/src/test/java/com/switcherapi/client/SwitcherValidateTest.java b/src/test/java/com/switcherapi/client/SwitcherValidateTest.java index f227608..eac35c3 100644 --- a/src/test/java/com/switcherapi/client/SwitcherValidateTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherValidateTest.java @@ -13,6 +13,7 @@ import java.util.HashSet; import java.util.Set; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static org.junit.jupiter.api.Assertions.*; class SwitcherValidateTest extends MockWebServerHelper { @@ -42,7 +43,7 @@ void resetSwitcherContextState() { .local(false) .snapshotLocation(null) .snapshotSkipValidation(false) - .environment("default") + .environment(DEFAULT_ENV) .silentMode(null) .snapshotAutoLoad(false) .snapshotAutoUpdateInterval(null)); diff --git a/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java b/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java index a6a55be..4fc8307 100644 --- a/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java +++ b/src/test/java/com/switcherapi/client/service/local/SwitcherLocalServiceTest.java @@ -1,5 +1,6 @@ package com.switcherapi.client.service.local; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; import static com.switcherapi.client.remote.Constants.DEFAULT_TIMEOUT; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -39,7 +40,7 @@ static void init() { SwitchersBase.configure(ContextBuilder.builder() .context(SwitchersBase.class.getName()) .snapshotLocation(SNAPSHOTS_LOCAL) - .environment("default") + .environment(DEFAULT_ENV) .local(true)); SwitcherProperties properties = SwitchersBase.getSwitcherProperties(); diff --git a/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java b/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java index c17b40a..1d605ea 100644 --- a/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java +++ b/src/test/java/com/switcherapi/client/utils/SnapshotWatcherWorkerTest.java @@ -6,6 +6,8 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; + class SnapshotWatcherWorkerTest extends SnapshotTest { @BeforeAll @@ -13,7 +15,7 @@ static void setupContext() { SwitchersBase.configure(ContextBuilder.builder(true) .context(SwitchersBase.class.getName()) .snapshotLocation(SNAPSHOTS_LOCAL) - .environment("default") + .environment(DEFAULT_ENV) .local(true)); SwitchersBase.initializeClient(); diff --git a/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java b/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java index 7dfa42f..5a5540b 100644 --- a/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java +++ b/src/test/java/com/switcherapi/client/utils/SwitcherUtilsTest.java @@ -29,6 +29,8 @@ import com.switcherapi.client.exception.SwitcherSnapshotLoadException; import com.switcherapi.client.model.ContextKey; +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; + class SwitcherUtilsTest { private static final String SNAPSHOTS_LOCAL = Paths.get(StringUtils.EMPTY).toAbsolutePath() + "/src/test/resources"; @@ -121,7 +123,7 @@ void shouldReturnMillis(String time, long expectedValue) { */ static Stream envArguments() { return Stream.of( - Arguments.of("default", "default"), + Arguments.of(DEFAULT_ENV, DEFAULT_ENV), Arguments.of("${PORT:8080}", "8080"), Arguments.of("${SNAPSHOT_LOCAL:}", ""), Arguments.of("${ENVIRONMENT}", "staging") From 0aa57d2795d656c56202fb7ddd3673cc34169e8f Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 13 Oct 2025 17:15:02 -0700 Subject: [PATCH 2/3] chore: added back TestMethodOrder (for windows agent test) --- .../java/com/switcherapi/client/SwitcherThrottleTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java index 25c2d73..fb4d8ec 100644 --- a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java @@ -6,10 +6,7 @@ import com.switcherapi.fixture.CountDownHelper; import com.switcherapi.fixture.MockWebServerHelper; import mockwebserver3.QueueDispatcher; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.*; import java.io.IOException; @@ -17,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) class SwitcherThrottleTest extends MockWebServerHelper { @BeforeAll @@ -44,6 +42,7 @@ void resetSwitcherContextState() { } @Test + @Order(1) void shouldReturnTrue_withThrottle() { // Initial remote call givenResponse(generateMockAuth(10)); //auth @@ -69,6 +68,7 @@ void shouldReturnTrue_withThrottle() { } @Test + @Order(2) void shouldRetrieveNewResponse_whenStrategyInputChanged() { // Initial remote call givenResponse(generateMockAuth(10)); //auth From 179f47034ec363aa1b7d34b7b5061d9dea7af0d9 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 13 Oct 2025 17:22:21 -0700 Subject: [PATCH 3/3] test: moved throttle tests to suite --- ...leTest.java => SwitcherThrottle1Test.java} | 42 ++----------- .../client/SwitcherThrottle2Test.java | 61 +++++++++++++++++++ 2 files changed, 67 insertions(+), 36 deletions(-) rename src/test/java/com/switcherapi/client/{SwitcherThrottleTest.java => SwitcherThrottle1Test.java} (60%) create mode 100644 src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java b/src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java similarity index 60% rename from src/test/java/com/switcherapi/client/SwitcherThrottleTest.java rename to src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java index fb4d8ec..faecf2f 100644 --- a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java @@ -2,11 +2,11 @@ import com.switcherapi.SwitchersBase; import com.switcherapi.client.model.Switcher; -import com.switcherapi.client.model.SwitcherBuilder; import com.switcherapi.fixture.CountDownHelper; import com.switcherapi.fixture.MockWebServerHelper; -import mockwebserver3.QueueDispatcher; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import java.io.IOException; @@ -14,8 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -@TestMethodOrder(MethodOrderer.OrderAnnotation.class) -class SwitcherThrottleTest extends MockWebServerHelper { +class SwitcherThrottle1Test extends MockWebServerHelper { @BeforeAll static void setup() throws IOException { @@ -28,6 +27,8 @@ static void setup() throws IOException { .domain("TEST_DOMAIN") .component("TEST_COMPONENT") .environment(DEFAULT_ENV)); + + SwitchersBase.initializeClient(); } @AfterAll @@ -35,14 +36,7 @@ static void tearDown() { MockWebServerHelper.tearDownMockServer(); } - @BeforeEach - void resetSwitcherContextState() { - ((QueueDispatcher) mockBackEnd.getDispatcher()).clear(); - SwitchersBase.initializeClient(); - } - @Test - @Order(1) void shouldReturnTrue_withThrottle() { // Initial remote call givenResponse(generateMockAuth(10)); //auth @@ -67,28 +61,4 @@ void shouldReturnTrue_withThrottle() { assertFalse(switcher.isItOn()); } - @Test - @Order(2) - void shouldRetrieveNewResponse_whenStrategyInputChanged() { - // Initial remote call - givenResponse(generateMockAuth(10)); //auth - givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached) - givenResponse(generateCriteriaResponse("false", false)); //criteria - async (cached) - - // Throttle period - should use cache - givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background) - - //test - SwitcherBuilder switcher = SwitchersBase - .getSwitcher(SwitchersBase.USECASE11) - .flush() - .throttle(1000); - - for (int i = 0; i < 100; i++) { - assertTrue(switcher.checkValue("value").isItOn()); - } - - assertFalse(switcher.checkValue("value_changed").isItOn()); - } - } diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java b/src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java new file mode 100644 index 0000000..5cc29ca --- /dev/null +++ b/src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java @@ -0,0 +1,61 @@ +package com.switcherapi.client; + +import com.switcherapi.SwitchersBase; +import com.switcherapi.client.model.SwitcherBuilder; +import com.switcherapi.fixture.MockWebServerHelper; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; + +import static com.switcherapi.client.remote.Constants.DEFAULT_ENV; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SwitcherThrottle2Test extends MockWebServerHelper { + + @BeforeAll + static void setup() throws IOException { + MockWebServerHelper.setupMockServer(); + + SwitchersBase.configure(ContextBuilder.builder(true) + .context(SwitchersBase.class.getName()) + .url(String.format("http://localhost:%s", mockBackEnd.getPort())) + .apiKey("TEST_API_KEY") + .domain("TEST_DOMAIN") + .component("TEST_COMPONENT") + .environment(DEFAULT_ENV)); + + SwitchersBase.initializeClient(); + } + + @AfterAll + static void tearDown() { + MockWebServerHelper.tearDownMockServer(); + } + + @Test + void shouldRetrieveNewResponse_whenStrategyInputChanged() { + // Initial remote call + givenResponse(generateMockAuth(10)); //auth + givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached) + givenResponse(generateCriteriaResponse("false", false)); //criteria - async (cached) + + // Throttle period - should use cache + givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background) + + //test + SwitcherBuilder switcher = SwitchersBase + .getSwitcher(SwitchersBase.USECASE11) + .flush() + .throttle(1000); + + for (int i = 0; i < 100; i++) { + assertTrue(switcher.checkValue("value").isItOn()); + } + + assertFalse(switcher.checkValue("value_changed").isItOn()); + } + +}