Skip to content

Commit 9d65b0c

Browse files
authored
feat: improved async performance by 2x (#377) (#378)
* feat: improved async performance by 2x (#377) * fix-test: SwitcherThrottleTest * Revert "fix-test: SwitcherThrottleTest" This reverts commit e347a68. * fix-test: set order for SwitcherThrottleTest
1 parent a2a22b5 commit 9d65b0c

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/main/java/com/switcherapi/client/model/SwitcherRequest.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class SwitcherRequest extends SwitcherBuilder {
2424

2525
private final String switcherKey;
2626

27-
private final Set<SwitcherResult> historyExecution;
27+
private final Map<List<Entry>, SwitcherResult> historyExecution;
2828

2929
private AsyncSwitcher asyncSwitcher;
3030

@@ -41,7 +41,7 @@ public SwitcherRequest(final String switcherKey,
4141
super(switcherProperties);
4242
this.switcherExecutor = switcherExecutor;
4343
this.switcherKey = switcherKey;
44-
this.historyExecution = new HashSet<>();
44+
this.historyExecution = new HashMap<>();
4545
}
4646

4747
@Override
@@ -105,10 +105,7 @@ public SwitcherResult executeCriteria() {
105105

106106
@Override
107107
public void updateHistoryExecution(final SwitcherResult response) {
108-
this.historyExecution.removeIf(item ->
109-
this.switcherKey.equals(item.getSwitcherKey()) && this.entry.equals(item.getEntry()));
110-
111-
this.historyExecution.add(response);
108+
historyExecution.put(entry, response);
112109
}
113110

114111
@Override
@@ -135,12 +132,7 @@ private boolean canUseAsync() {
135132
}
136133

137134
private Optional<SwitcherResult> getFromHistory() {
138-
for (SwitcherResult switcherResult : historyExecution) {
139-
if (switcherResult.getEntry().equals(entry)) {
140-
return Optional.of(switcherResult);
141-
}
142-
}
143-
return Optional.empty();
135+
return Optional.ofNullable(historyExecution.get(entry));
144136
}
145137

146138
@Override

src/test/java/com/switcherapi/client/SwitcherThrottleTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
import com.switcherapi.fixture.CountDownHelper;
77
import com.switcherapi.fixture.MockWebServerHelper;
88
import mockwebserver3.QueueDispatcher;
9-
import org.junit.jupiter.api.AfterAll;
10-
import org.junit.jupiter.api.BeforeAll;
11-
import org.junit.jupiter.api.BeforeEach;
12-
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.*;
1310

1411
import java.io.IOException;
1512

1613
import static org.junit.jupiter.api.Assertions.assertFalse;
1714
import static org.junit.jupiter.api.Assertions.assertTrue;
1815

16+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1917
class SwitcherThrottleTest extends MockWebServerHelper {
2018

2119
@BeforeAll
@@ -46,6 +44,7 @@ void resetSwitcherContextState() {
4644
}
4745

4846
@Test
47+
@Order(1)
4948
void shouldReturnTrue_withThrottle() {
5049
Switchers.initializeClient();
5150

@@ -72,6 +71,7 @@ void shouldReturnTrue_withThrottle() {
7271
}
7372

7473
@Test
74+
@Order(2)
7575
void shouldRetrieveNewResponse_whenStrategyInputChanged() {
7676
Switchers.initializeClient();
7777

0 commit comments

Comments
 (0)