diff --git a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java index cb5a61a..8025488 100644 --- a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java +++ b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java @@ -24,7 +24,7 @@ public final class SwitcherRequest extends SwitcherBuilder { private final String switcherKey; - private final Set historyExecution; + private final Map, SwitcherResult> historyExecution; private AsyncSwitcher asyncSwitcher; @@ -41,7 +41,7 @@ public SwitcherRequest(final String switcherKey, super(switcherProperties); this.switcherExecutor = switcherExecutor; this.switcherKey = switcherKey; - this.historyExecution = new HashSet<>(); + this.historyExecution = new HashMap<>(); } @Override @@ -105,10 +105,7 @@ public SwitcherResult executeCriteria() { @Override public void updateHistoryExecution(final SwitcherResult response) { - this.historyExecution.removeIf(item -> - this.switcherKey.equals(item.getSwitcherKey()) && this.entry.equals(item.getEntry())); - - this.historyExecution.add(response); + historyExecution.put(entry, response); } @Override @@ -135,12 +132,7 @@ private boolean canUseAsync() { } private Optional getFromHistory() { - for (SwitcherResult switcherResult : historyExecution) { - if (switcherResult.getEntry().equals(entry)) { - return Optional.of(switcherResult); - } - } - return Optional.empty(); + return Optional.ofNullable(historyExecution.get(entry)); } @Override diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java index 0001125..f10ce77 100644 --- a/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherThrottleTest.java @@ -6,16 +6,14 @@ 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; 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 @@ -46,6 +44,7 @@ void resetSwitcherContextState() { } @Test + @Order(1) void shouldReturnTrue_withThrottle() { Switchers.initializeClient(); @@ -72,6 +71,7 @@ void shouldReturnTrue_withThrottle() { } @Test + @Order(2) void shouldRetrieveNewResponse_whenStrategyInputChanged() { Switchers.initializeClient();