From 02a1c84c7246c2b2c3cd4f870c210b9d69a7c522 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Wed, 29 Apr 2015 20:10:51 +0200 Subject: [PATCH] Fixed NPE when adding same job several times. Closes elastic/elasticsearch#277 Original commit: elastic/x-pack-elasticsearch@42831113da99f9079f5d8aaba950d766dbc78b9c --- .../engine/SchedulerScheduleTriggerEngine.java | 2 +- .../schedule/engine/BaseTriggerEngineTests.java | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java b/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java index ad70f728158..64e50b3e2f6 100644 --- a/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java +++ b/src/main/java/org/elasticsearch/watcher/trigger/schedule/engine/SchedulerScheduleTriggerEngine.java @@ -168,7 +168,7 @@ public class SchedulerScheduleTriggerEngine extends ScheduleTriggerEngine { ImmutableMap.Builder builder = ImmutableMap.builder(); for (int i = 0; i < schedules.length; i++) { ActiveSchedule sched = schedules[i].name.equals(schedule.name) ? schedule : schedules[i]; - schedules[i] = sched; + newSchedules[i] = sched; builder.put(sched.name, sched); } return new Schedules(newSchedules, builder.build()); diff --git a/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTests.java b/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTests.java index a5a3e269e9e..83a50832cff 100644 --- a/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTests.java +++ b/src/test/java/org/elasticsearch/watcher/trigger/schedule/engine/BaseTriggerEngineTests.java @@ -193,6 +193,23 @@ public abstract class BaseTriggerEngineTests extends ElasticsearchTestCase { } } + @Test + public void testAddSameJobSeveralTimes() { + engine.start(Collections.emptySet()); + engine.register(new TriggerEngine.Listener() { + + @Override + public void triggered(Iterable events) { + logger.info("triggered job on [{}]", new DateTime()); + } + }); + + int times = scaledRandomIntBetween(3, 30); + for (int i = 0; i < times; i++) { + engine.add(new SimpleJob("_id", interval("10s"))); + } + } + static class SimpleJob implements TriggerEngine.Job { private final String name;