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
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/switcherapi/client/model/AsyncSwitcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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);
}
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/switcherapi/client/model/Switcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/com/switcherapi/client/model/SwitcherBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public abstract class SwitcherBuilder implements Switcher {

protected boolean bypassMetrics;

protected boolean keepExecutions;

protected Boolean restrictRelay;

protected String defaultResult;
Expand All @@ -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
Expand All @@ -48,6 +53,7 @@ protected SwitcherBuilder(final SwitcherProperties properties) {
* @return {@link SwitcherBuilder}
*/
public SwitcherBuilder throttle(long delay) {
this.keepExecutions();
this.delay = delay;
return this;
}
Expand Down Expand Up @@ -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;
}
Expand Down
37 changes: 17 additions & 20 deletions src/main/java/com/switcherapi/client/model/SwitcherRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public final class SwitcherRequest extends SwitcherBuilder {

private final ConcurrentHashMap<List<Entry>, SwitcherResult> historyExecution;
private final ConcurrentHashMap<List<Entry>, SwitcherResult> executionsMap;

private final SwitcherExecutor switcherExecutor;

Expand All @@ -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
Expand All @@ -57,7 +63,7 @@ public SwitcherRequest prepareEntry(final List<Entry> entry) {
@Override
public SwitcherRequest prepareEntry(final Entry entry, final boolean add) {
if (!add) {
this.flush();
this.resetInputs();
}

this.entry.add(entry);
Expand All @@ -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();
Expand All @@ -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;
}

Expand All @@ -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
Expand All @@ -131,19 +132,15 @@ public List<Entry> getEntry() {

@Override
public SwitcherResult getLastExecutionResult() {
return getFromHistory();
return executionsMap.get(entry);
}

public boolean isBypassMetrics() {
return bypassMetrics;
}

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void shouldFlushStrategyInputs() {

//test
switcherBuilder
.flush()
.resetInputs()
.checkValue("anotherValue");

assertEquals(1, switcherBuilder.getEntry().size());
Expand Down
32 changes: 26 additions & 6 deletions src/test/java/com/switcherapi/client/SwitcherLocal1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ void shouldReturnTrue_withThrottle() {
//test
Switcher switcher = SwitchersBase
.getSwitcher(SwitchersBase.USECASE11)
.flush()
.flushExecutions()
.resetInputs()
.checkValue("value")
.throttle(1000);

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