Skip to content

Commit b5a793b

Browse files
authored
Tests: Fail if test watches could not be triggered (elastic#30392)
Watcher tests now always fail hard when watches that were tried to be triggered in a test using the trigger() method, but could not because they were not found on any of the nodes in the cluster.
1 parent d893041 commit b5a793b

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java

+15-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.watcher.test;
77

8+
import org.apache.logging.log4j.Logger;
89
import org.elasticsearch.action.admin.indices.alias.Alias;
910
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
1011
import org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse;
@@ -70,10 +71,12 @@
7071
import java.util.Collections;
7172
import java.util.HashSet;
7273
import java.util.List;
74+
import java.util.Locale;
7375
import java.util.Set;
7476
import java.util.concurrent.atomic.AtomicReference;
7577
import java.util.function.Consumer;
7678
import java.util.stream.Collectors;
79+
import java.util.stream.StreamSupport;
7780

7881
import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
7982
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
@@ -177,7 +180,7 @@ protected boolean timeWarped() {
177180
public void _setup() throws Exception {
178181
if (timeWarped()) {
179182
timeWarp = new TimeWarp(internalCluster().getInstances(ScheduleTriggerEngineMock.class),
180-
(ClockMock)getInstanceFromMaster(Clock.class));
183+
(ClockMock)getInstanceFromMaster(Clock.class), logger);
181184
}
182185

183186
if (internalCluster().size() > 0) {
@@ -536,24 +539,28 @@ public EmailSent send(Email email, Authentication auth, Profile profile, String
536539

537540
protected static class TimeWarp {
538541

539-
protected final Iterable<ScheduleTriggerEngineMock> schedulers;
540-
protected final ClockMock clock;
542+
private final List<ScheduleTriggerEngineMock> schedulers;
543+
private final ClockMock clock;
544+
private final Logger logger;
541545

542-
public TimeWarp(Iterable<ScheduleTriggerEngineMock> schedulers, ClockMock clock) {
543-
this.schedulers = schedulers;
546+
TimeWarp(Iterable<ScheduleTriggerEngineMock> schedulers, ClockMock clock, Logger logger) {
547+
this.schedulers = StreamSupport.stream(schedulers.spliterator(), false).collect(Collectors.toList());
544548
this.clock = clock;
549+
this.logger = logger;
545550
}
546551

547552
public void trigger(String jobName) {
548-
schedulers.forEach(scheduler -> scheduler.trigger(jobName));
553+
trigger(jobName, 1, null);
549554
}
550555

551556
public ClockMock clock() {
552557
return clock;
553558
}
554559

555-
public void trigger(String id, int times, TimeValue timeValue) {
556-
schedulers.forEach(scheduler -> scheduler.trigger(id, times, timeValue));
560+
public void trigger(String watchId, int times, TimeValue timeValue) {
561+
boolean isTriggered = schedulers.stream().anyMatch(scheduler -> scheduler.trigger(watchId, times, timeValue));
562+
String msg = String.format(Locale.ROOT, "could not find watch [%s] to trigger", watchId);
563+
assertThat(msg, isTriggered, is(true));
557564
}
558565
}
559566

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/trigger/ScheduleTriggerEngineMock.java

+6-9
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,13 @@ public boolean remove(String jobId) {
7777
return watches.remove(jobId) != null;
7878
}
7979

80-
public void trigger(String jobName) {
81-
trigger(jobName, 1, null);
80+
public boolean trigger(String jobName) {
81+
return trigger(jobName, 1, null);
8282
}
8383

84-
public void trigger(String jobName, int times) {
85-
trigger(jobName, times, null);
86-
}
87-
88-
public void trigger(String jobName, int times, TimeValue interval) {
84+
public boolean trigger(String jobName, int times, TimeValue interval) {
8985
if (watches.containsKey(jobName) == false) {
90-
logger.trace("not executing job [{}], not found", jobName);
91-
return;
86+
return false;
9287
}
9388

9489
for (int i = 0; i < times; i++) {
@@ -108,5 +103,7 @@ public void trigger(String jobName, int times, TimeValue interval) {
108103
}
109104
}
110105
}
106+
107+
return true;
111108
}
112109
}

0 commit comments

Comments
 (0)