diff --git a/README.md b/README.md index 21adb6e..2c852b1 100644 --- a/README.md +++ b/README.md @@ -189,7 +189,7 @@ switcher.isItOn(); Create chained calls to validate the switcher with a more readable and maintainable code. ```java -import static **.MyAppFeatures.*; +import static org.example.MyAppFeatures.*; getSwitcher(FEATURE01) .checkValue("My value") @@ -198,10 +198,22 @@ getSwitcher(FEATURE01) ``` 4. **Accessing the last SwitcherResult** -Switchers stores the last execution result, which can be retrieved using the following operation. +Switchers stores the last execution result, which can be retrieved using the following operation. Requires enabling executions history for each Switcher using: ```java -switcher.getLastExecutionResult(); +getSwitcher(FEATURE01) + .keepExecutions(); + .checkValue("My value") + .checkNetwork("10.0.0.1") + +switcher.isItOn(); +switcher.getLastExecutionResult(); // returns the last SwitcherResult +``` + +Executions history can also be cleared using: + +```java +switcher.flushExecutions(); ``` 5. **Throttling** diff --git a/src/main/java/com/switcherapi/client/SwitcherContextBase.java b/src/main/java/com/switcherapi/client/SwitcherContextBase.java index cecb04d..b715d2a 100644 --- a/src/main/java/com/switcherapi/client/SwitcherContextBase.java +++ b/src/main/java/com/switcherapi/client/SwitcherContextBase.java @@ -399,7 +399,7 @@ public static SwitcherRequest getSwitcher(String key, boolean keepEntries) { final SwitcherRequest switcher = switchers.get(key); if (!keepEntries) { - switcher.flush(); + switcher.resetInputs(); } return switcher; diff --git a/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java b/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java index b3b9752..2b4aaed 100644 --- a/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java +++ b/src/main/java/com/switcherapi/client/model/AsyncSwitcher.java @@ -44,7 +44,7 @@ public AsyncSwitcher(final Switcher switcher, long delay) { /** * Validate if next run is ready to be performed, otherwise it will skip and delegate the - * Switcher result for the Switcher history execution. + * Switcher result for the Switcher executions map. */ public void execute() { SwitcherUtils.debug(logger, "nextRun: {} - currentTimeMillis: {}", nextRun, System.currentTimeMillis()); @@ -60,7 +60,7 @@ public void execute() { public void run() { try { final SwitcherResult response = switcher.executeCriteria(); - switcher.updateHistoryExecution(response); + switcher.updateExecutions(response); } catch (SwitcherException e) { logger.error(e.getMessage(), e); } diff --git a/src/main/java/com/switcherapi/client/model/Switcher.java b/src/main/java/com/switcherapi/client/model/Switcher.java index 5401c30..ba789da 100644 --- a/src/main/java/com/switcherapi/client/model/Switcher.java +++ b/src/main/java/com/switcherapi/client/model/Switcher.java @@ -25,6 +25,13 @@ */ public interface Switcher { + /** + * Clear executions from SwitcherRequest + * + * @return instance of SwitcherInterface + */ + Switcher flushExecutions(); + /** * Prepare the Switcher including a list of inputs necessary to run the criteria afterward. * @@ -77,11 +84,11 @@ public interface Switcher { SwitcherResult executeCriteria(); /** - * Update the history of executions. + * Update Switcher executions. * * @param response the response to be updated */ - void updateHistoryExecution(SwitcherResult response); + void updateExecutions(SwitcherResult response); /** * Get the key of the switcher. diff --git a/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java b/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java index 46242fb..e5d2341 100644 --- a/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java +++ b/src/main/java/com/switcherapi/client/model/SwitcherBuilder.java @@ -22,6 +22,8 @@ public abstract class SwitcherBuilder implements Switcher { protected boolean bypassMetrics; + protected boolean keepExecutions; + protected Boolean restrictRelay; protected String defaultResult; @@ -35,11 +37,14 @@ protected SwitcherBuilder(final SwitcherProperties properties) { } /** - * Clear all entries previously added and history of executions. + * Clear all entries previously added * * @return {@link SwitcherBuilder} */ - public abstract SwitcherBuilder flush(); + public SwitcherBuilder resetInputs() { + this.entry.clear(); + return this; + } /** * Skip API calls given a delay time @@ -48,6 +53,7 @@ protected SwitcherBuilder(final SwitcherProperties properties) { * @return {@link SwitcherBuilder} */ public SwitcherBuilder throttle(long delay) { + this.keepExecutions(); this.delay = delay; return this; } @@ -187,6 +193,16 @@ public SwitcherBuilder bypassMetrics() { return this; } + /** + * Keep executions in memory + * + * @return {@link SwitcherBuilder} + */ + public SwitcherBuilder keepExecutions() { + this.keepExecutions = true; + return this; + } + public boolean isRemote() { return remote; } diff --git a/src/main/java/com/switcherapi/client/model/SwitcherRequest.java b/src/main/java/com/switcherapi/client/model/SwitcherRequest.java index ab8591f..1efb0a2 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 ConcurrentHashMap, SwitcherResult> historyExecution; + private final ConcurrentHashMap, SwitcherResult> executionsMap; private final SwitcherExecutor switcherExecutor; @@ -45,7 +45,13 @@ public SwitcherRequest(final String switcherKey, super(switcherProperties); this.switcherExecutor = switcherExecutor; this.switcherKey = switcherKey; - this.historyExecution = new ConcurrentHashMap<>(); + this.executionsMap = new ConcurrentHashMap<>(); + } + + @Override + public SwitcherRequest flushExecutions() { + this.executionsMap.clear(); + return this; } @Override @@ -57,7 +63,7 @@ public SwitcherRequest prepareEntry(final List entry) { @Override public SwitcherRequest prepareEntry(final Entry entry, final boolean add) { if (!add) { - this.flush(); + this.resetInputs(); } this.entry.add(entry); @@ -69,13 +75,6 @@ public SwitcherRequest prepareEntry(final Entry entry) { return this.prepareEntry(entry, false); } - @Override - public SwitcherBuilder flush() { - this.entry.clear(); - this.historyExecution.clear(); - return this; - } - @Override public boolean isItOn() throws SwitcherException { final SwitcherResult response = submit(); @@ -94,14 +93,14 @@ public SwitcherResult submit() throws SwitcherException { } asyncSwitcher.execute(); - final SwitcherResult response = getFromHistory(); + final SwitcherResult response = executionsMap.get(entry); if (Objects.nonNull(response)) { return response; } } final SwitcherResult response = this.executeCriteria(); - this.updateHistoryExecution(response); + this.updateExecutions(response); return response; } @@ -115,8 +114,10 @@ public SwitcherResult executeCriteria() { } @Override - public void updateHistoryExecution(final SwitcherResult response) { - historyExecution.put(entry, response); + public void updateExecutions(final SwitcherResult response) { + if (super.keepExecutions) { + executionsMap.put(entry, response); + } } @Override @@ -131,7 +132,7 @@ public List getEntry() { @Override public SwitcherResult getLastExecutionResult() { - return getFromHistory(); + return executionsMap.get(entry); } public boolean isBypassMetrics() { @@ -139,11 +140,7 @@ public boolean isBypassMetrics() { } private boolean canUseAsync() { - return super.delay > 0 && !this.historyExecution.isEmpty(); - } - - private SwitcherResult getFromHistory() { - return historyExecution.get(entry); + return super.delay > 0 && !this.executionsMap.isEmpty(); } @Override diff --git a/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java b/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java index b71f862..bce8261 100644 --- a/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java +++ b/src/test/java/com/switcherapi/client/SwitcherBasicCriteriaResponseTest.java @@ -96,7 +96,7 @@ void shouldFlushStrategyInputs() { //test switcherBuilder - .flush() + .resetInputs() .checkValue("anotherValue"); assertEquals(1, switcherBuilder.getEntry().size()); diff --git a/src/test/java/com/switcherapi/client/SwitcherLocal1Test.java b/src/test/java/com/switcherapi/client/SwitcherLocal1Test.java index 1e83778..526c8a7 100644 --- a/src/test/java/com/switcherapi/client/SwitcherLocal1Test.java +++ b/src/test/java/com/switcherapi/client/SwitcherLocal1Test.java @@ -7,6 +7,8 @@ import com.switcherapi.client.model.ContextKey; import com.switcherapi.client.model.Entry; import com.switcherapi.client.model.StrategyValidator; +import com.switcherapi.client.model.Switcher; +import com.switcherapi.client.model.SwitcherBuilder; import com.switcherapi.client.model.SwitcherRequest; import com.switcherapi.fixture.Product; import com.google.gson.Gson; @@ -49,36 +51,54 @@ void localShouldValidateContext() { @Test void localShouldReturnTrue() { - SwitcherRequest switcher = Switchers.getSwitcher(Switchers.USECASE11, true); + Switcher switcher = Switchers.getSwitcher(Switchers.USECASE11, true) + .keepExecutions(); assertNull(switcher.getLastExecutionResult()); assertTrue(switcher.isItOn()); - // check result history + // check result from executions assertTrue(switcher.getLastExecutionResult().isItOn()); } @Test void localShouldReturnTrueUsingFriendlyConstantName() { - SwitcherRequest switcher = Switchers.getSwitcher(Switchers.friendlyFeatureName, true); + Switcher switcher = Switchers.getSwitcher(Switchers.friendlyFeatureName, true) + .keepExecutions(); assertNull(switcher.getLastExecutionResult()); assertTrue(switcher.isItOn()); - // check result history + // check result from executions assertTrue(switcher.getLastExecutionResult().isItOn()); } @Test void localShouldReturnFalse() { - SwitcherRequest switcher = Switchers.getSwitcher(Switchers.USECASE12); + Switcher switcher = Switchers.getSwitcher(Switchers.USECASE12) + .keepExecutions(); assertNull(switcher.getLastExecutionResult()); assertFalse(switcher.isItOn()); - // check result history + // check result from executions assertFalse(switcher.getLastExecutionResult().isItOn()); } + + @Test + void localShouldCleanExecutions() { + SwitcherBuilder switcher = Switchers.getSwitcher(Switchers.USECASE11, true) + .keepExecutions(); + + assertNull(switcher.getLastExecutionResult()); + assertTrue(switcher.isItOn()); + + // check result from executions + assertTrue(switcher.getLastExecutionResult().isItOn()); + switcher.flushExecutions(); + + assertNull(switcher.getLastExecutionResult()); + } @Test void localShouldReturnFalse_groupDisabled() { diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java b/src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java index faecf2f..75eb1de 100644 --- a/src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java +++ b/src/test/java/com/switcherapi/client/SwitcherThrottle1Test.java @@ -49,7 +49,8 @@ void shouldReturnTrue_withThrottle() { //test Switcher switcher = SwitchersBase .getSwitcher(SwitchersBase.USECASE11) - .flush() + .flushExecutions() + .resetInputs() .checkValue("value") .throttle(1000); diff --git a/src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java b/src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java index 5cc29ca..33d2783 100644 --- a/src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java +++ b/src/test/java/com/switcherapi/client/SwitcherThrottle2Test.java @@ -48,7 +48,8 @@ void shouldRetrieveNewResponse_whenStrategyInputChanged() { //test SwitcherBuilder switcher = SwitchersBase .getSwitcher(SwitchersBase.USECASE11) - .flush() + .flushExecutions() + .resetInputs() .throttle(1000); for (int i = 0; i < 100; i++) {