Skip to content
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
10 changes: 6 additions & 4 deletions src/main/java/com/switcherapi/client/model/SwitcherBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -17,6 +15,8 @@
public abstract class SwitcherBuilder implements Switcher {

protected final SwitcherProperties properties;

protected final Map<List<Entry>, SwitcherResult> historyExecution;

protected long delay;

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public final class SwitcherRequest extends SwitcherBuilder {
private final SwitcherExecutor switcherExecutor;

private final String switcherKey;

private final Map<List<Entry>, SwitcherResult> historyExecution;

private AsyncSwitcher asyncSwitcher;

Expand All @@ -41,7 +39,6 @@ public SwitcherRequest(final String switcherKey,
super(switcherProperties);
this.switcherExecutor = switcherExecutor;
this.switcherKey = switcherKey;
this.historyExecution = new HashMap<>();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/switcherapi/client/SwitcherBasicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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));

Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));

Expand All @@ -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));

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/switcherapi/client/SwitcherFail1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,7 +44,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/switcherapi/client/SwitcherFail2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,7 +41,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -41,7 +42,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -52,7 +53,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,7 +47,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -43,7 +44,7 @@ void resetSwitcherContextState() {
.local(false)
.snapshotLocation(null)
.snapshotSkipValidation(false)
.environment("default")
.environment(DEFAULT_ENV)
.silentMode(null)
.snapshotAutoLoad(false)
.snapshotAutoUpdateInterval(null));
Expand Down Expand Up @@ -105,7 +106,7 @@ void shouldValidateAndLoadSnapshot_whenLocal() {
.local(true)
.snapshotAutoLoad(false)
.snapshotLocation(RESOURCES_PATH)
.environment("default"));
.environment(DEFAULT_ENV));

Switchers.initializeClient();

Expand All @@ -129,7 +130,7 @@ void shouldNotValidateAndLoadSnapshot_serviceUnavailable() {
.local(false)
.snapshotAutoLoad(false)
.snapshotLocation(RESOURCES_PATH)
.environment("default"));
.environment(DEFAULT_ENV));

Switchers.initializeClient();

Expand Down
64 changes: 64 additions & 0 deletions src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.switcherapi.client;

import com.switcherapi.SwitchersBase;
import com.switcherapi.client.model.Switcher;
import com.switcherapi.fixture.CountDownHelper;
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 SwitcherThrottle1Test 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 shouldReturnTrue_withThrottle() {
// Initial remote call
givenResponse(generateMockAuth(10)); //auth
givenResponse(generateCriteriaResponse("true", false)); //criteria - sync (cached)

// Throttle period - should use cache
givenResponse(generateCriteriaResponse("false", false)); //criteria - async (background)
givenResponse(generateCriteriaResponse("false", false)); //criteria - async after 1 sec (background)

//test
Switcher switcher = SwitchersBase
.getSwitcher(SwitchersBase.USECASE11)
.flush()
.checkValue("value")
.throttle(1000);

for (int i = 0; i < 100; i++) {
assertTrue(switcher.isItOn());
}

CountDownHelper.wait(1);
assertFalse(switcher.isItOn());
}

}
61 changes: 61 additions & 0 deletions src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
Loading