Skip to content

Commit 9061073

Browse files
committed
Fix deterministic trouble for custom watched services
We now only counts attacks on custom watched services instead of counting all attacks. Closes #451
1 parent 78b8c8e commit 9061073

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

chaos-monkey-spring-boot/src/main/java/de/codecentric/spring/boot/chaos/monkey/component/ChaosMonkeyRequestScope.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,11 @@ public ChaosMonkeyRequestScope(ChaosMonkeySettings chaosMonkeySettings, List<Cha
6767
}
6868

6969
public void callChaosMonkey(ChaosTarget type, String simpleName) {
70-
if (isEnabled(type, simpleName) && isTrouble()) {
71-
70+
if (isEnabled(type, simpleName) && isTrouble(simpleName)) {
7271
if (metricEventPublisher != null) {
7372
metricEventPublisher.publishMetricEvent(MetricType.APPLICATION_REQ_COUNT, "type", "total");
7473
}
75-
76-
// Custom watched services can be defined at runtime, if there are any, only
77-
// these will be attacked!
78-
AssaultProperties assaultProps = chaosMonkeySettings.getAssaultProperties();
79-
if (assaultProps.isWatchedCustomServicesActive()) {
80-
if (assaultProps.getWatchedCustomServices().stream().anyMatch(simpleName::startsWith)) {
81-
// only all listed custom methods will be attacked
82-
chooseAndRunAttack();
83-
}
84-
} else {
85-
// default attack if no custom watched service is defined
86-
chooseAndRunAttack();
87-
}
74+
chooseAndRunAttack();
8875
}
8976
}
9077

@@ -105,7 +92,14 @@ private ChaosMonkeyAssault getRandomFrom(List<ChaosMonkeyAssault> activeAssaults
10592
return activeAssaults.get(exceptionRand);
10693
}
10794

108-
private boolean isTrouble() {
95+
private boolean isTrouble(String simpleName) {
96+
// Custom watched services can be defined at runtime, if there are any, only
97+
// these will be attacked!
98+
AssaultProperties assaultProperties = chaosMonkeySettings.getAssaultProperties();
99+
if (assaultProperties.isWatchedCustomServicesActive() && assaultProperties.getWatchedCustomServices().stream().noneMatch(simpleName::startsWith)) {
100+
return false;
101+
}
102+
109103
if (chaosMonkeySettings.getAssaultProperties().isDeterministic()) {
110104
return assaultCounter.incrementAndGet() % chaosMonkeySettings.getAssaultProperties().getLevel() == 0;
111105
} else {

chaos-monkey-spring-boot/src/test/java/de/codecentric/spring/boot/chaos/monkey/component/ChaosMonkeyRequestScopeTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,23 @@ void callChaosMonkey_givenDeterministicTrouble_shouldRunAttackAndPublishRequestC
198198
verify(metricEventPublisherMock).publishMetricEvent(MetricType.APPLICATION_REQ_COUNT, "type", "assaulted");
199199
}
200200

201+
@Test
202+
void callChaosMonkey_givenDeterministicTroubleAndConfiguredWatchedCustomServices_shouldNotRunAttackIfAttackCountIsLowerThanLevel() {
203+
givenChaosMonkeyIsEnabled();
204+
givenOneActiveAttack();
205+
given(assaultProperties.isDeterministic()).willReturn(true);
206+
given(assaultProperties.getLevel()).willReturn(3);
207+
given(assaultProperties.isWatchedCustomServicesActive()).willReturn(true);
208+
given(assaultProperties.getWatchedCustomServices()).willReturn(List.of("de.test.CustomService.someMethod"));
209+
210+
// Important: We call chaos monkey three times, but the second attack is not on the watched custom service!
211+
chaosMonkeyRequestScope.callChaosMonkey(null, "de.test.CustomService.someMethod");
212+
chaosMonkeyRequestScope.callChaosMonkey(null, "de.test.CustomService.someOtherMethod");
213+
chaosMonkeyRequestScope.callChaosMonkey(null, "de.test.CustomService.someMethod");
214+
215+
verify(assaults.get(0), never()).attack();
216+
}
217+
201218
@Test
202219
void callChaosMonkey_givenConfiguredWatchedCustomServices_shouldRunAttackIfTargetPackageNameMatches() {
203220
givenChaosMonkeyIsEnabled();

0 commit comments

Comments
 (0)