test: unmuted slow watches test.

Removed the dependency on groovy and added a script impl. that allows to run Thread.sleep(...) in order to simulate a slow watch.

Relates to elastic/elasticsearch#724

Original commit: elastic/x-pack-elasticsearch@b18dd89b46
This commit is contained in:
Martijn van Groningen 2015-11-20 12:56:56 +01:00
parent a47adfb270
commit f2900f71c5
3 changed files with 132 additions and 5 deletions

View File

@ -0,0 +1,97 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.script;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.util.Collections;
import java.util.Map;
/**
* A dummy script engine used for testing. Scripts must be a number. Running the script
*/
public class SleepScriptEngine implements ScriptEngineService {
public static final String NAME = "sleep";
public static class TestPlugin extends Plugin {
public TestPlugin() {
}
@Override
public String name() {
return NAME;
}
@Override
public String description() {
return "Mock script engine for integration tests";
}
public void onModule(ScriptModule module) {
module.addScriptEngine(SleepScriptEngine.class);
}
}
@Override
public String[] types() {
return new String[]{ NAME };
}
@Override
public String[] extensions() {
return types();
}
@Override
public boolean sandboxed() {
return true;
}
@Override
public Object compile(String script) {
return script;
}
@Override
public ExecutableScript executable(CompiledScript compiledScript, @Nullable Map<String, Object> vars) {
return new AbstractSearchScript() {
@Override
public Object run() {
try {
Thread.sleep(((Number) vars.get("millis")).longValue());
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return true;
}
};
}
@Override
public SearchScript search(CompiledScript compiledScript, SearchLookup lookup, @Nullable Map<String, Object> vars) {
return null;
}
@Override
public void scriptRemoved(@Nullable CompiledScript script) {
}
@Override
public void close() throws IOException {
}
public static org.elasticsearch.watcher.support.Script sleepScript(long millis) {
return new org.elasticsearch.watcher.support.Script.Builder.Inline("")
.lang("sleep")
.params(Collections.singletonMap("millis", millis)).build();
}
}

View File

@ -6,13 +6,22 @@
package org.elasticsearch.watcher.transport.action.delete; package org.elasticsearch.watcher.transport.action.delete;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.SleepScriptEngine;
import org.elasticsearch.test.junit.annotations.TestLogging; import org.elasticsearch.test.junit.annotations.TestLogging;
import org.elasticsearch.watcher.condition.Condition;
import org.elasticsearch.watcher.condition.ConditionBuilders;
import org.elasticsearch.watcher.support.Script; import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase; import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.watcher.transport.actions.delete.DeleteWatchResponse; import org.elasticsearch.watcher.transport.actions.delete.DeleteWatchResponse;
import org.elasticsearch.watcher.transport.actions.put.PutWatchResponse; import org.elasticsearch.watcher.transport.actions.put.PutWatchResponse;
import org.elasticsearch.watcher.transport.actions.service.WatcherServiceResponse; import org.elasticsearch.watcher.transport.actions.service.WatcherServiceResponse;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import static org.elasticsearch.watcher.actions.ActionBuilders.loggingAction; import static org.elasticsearch.watcher.actions.ActionBuilders.loggingAction;
import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder; import static org.elasticsearch.watcher.client.WatchSourceBuilders.watchBuilder;
import static org.elasticsearch.watcher.condition.ConditionBuilders.scriptCondition; import static org.elasticsearch.watcher.condition.ConditionBuilders.scriptCondition;
@ -23,7 +32,6 @@ import static org.hamcrest.Matchers.is;
/** /**
*/ */
@AwaitsFix(bugUrl = "https://github.com/elastic/x-plugins/issues/724")
public class ForceDeleteWatchTests extends AbstractWatcherIntegrationTestCase { public class ForceDeleteWatchTests extends AbstractWatcherIntegrationTestCase {
//Disable time warping for the force delete long running watch test //Disable time warping for the force delete long running watch test
@Override @Override
@ -36,11 +44,18 @@ public class ForceDeleteWatchTests extends AbstractWatcherIntegrationTestCase {
return false; return false;
} }
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
List<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
plugins.add(SleepScriptEngine.TestPlugin.class);
return plugins;
}
@TestLogging("_root:DEBUG") @TestLogging("_root:DEBUG")
public void testForceDeleteLongRunningWatch() throws Exception { public void testForceDeleteLongRunningWatch() throws Exception {
PutWatchResponse putResponse = watcherClient().preparePutWatch("_name").setSource(watchBuilder() PutWatchResponse putResponse = watcherClient().preparePutWatch("_name").setSource(watchBuilder()
.trigger(schedule(interval("3s"))) .trigger(schedule(interval("3s")))
.condition(scriptCondition(Script.inline("sleep 5000; return true"))) .condition(scriptCondition(SleepScriptEngine.sleepScript(5000)))
.addAction("_action1", loggingAction("executed action: {{ctx.id}}"))) .addAction("_action1", loggingAction("executed action: {{ctx.id}}")))
.get(); .get();
assertThat(putResponse.getId(), equalTo("_name")); assertThat(putResponse.getId(), equalTo("_name"));

View File

@ -8,17 +8,26 @@ package org.elasticsearch.watcher.transport.action.stats;
import org.apache.lucene.util.LuceneTestCase.AwaitsFix; import org.apache.lucene.util.LuceneTestCase.AwaitsFix;
import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.common.util.concurrent.EsExecutors;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.SleepScriptEngine;
import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase;
import org.elasticsearch.watcher.WatcherState; import org.elasticsearch.watcher.WatcherState;
import org.elasticsearch.watcher.actions.ActionBuilders; import org.elasticsearch.watcher.actions.ActionBuilders;
import org.elasticsearch.watcher.condition.Condition;
import org.elasticsearch.watcher.condition.ConditionBuilders; import org.elasticsearch.watcher.condition.ConditionBuilders;
import org.elasticsearch.watcher.execution.ExecutionPhase; import org.elasticsearch.watcher.execution.ExecutionPhase;
import org.elasticsearch.watcher.execution.QueuedWatch; import org.elasticsearch.watcher.execution.QueuedWatch;
import org.elasticsearch.watcher.input.InputBuilders; import org.elasticsearch.watcher.input.InputBuilders;
import org.elasticsearch.watcher.support.Script;
import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase; import org.elasticsearch.watcher.test.AbstractWatcherIntegrationTestCase;
import org.elasticsearch.watcher.transport.actions.stats.WatcherStatsResponse; import org.elasticsearch.watcher.transport.actions.stats.WatcherStatsResponse;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST; import static org.elasticsearch.test.ESIntegTestCase.Scope.TEST;
@ -33,13 +42,19 @@ import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.Matchers.nullValue;
@ESIntegTestCase.ClusterScope(scope = TEST, numClientNodes = 0, transportClientRatio = 0, randomDynamicTemplates = false, numDataNodes = 2) @ESIntegTestCase.ClusterScope(scope = TEST, numClientNodes = 0, transportClientRatio = 0, randomDynamicTemplates = false, numDataNodes = 2)
@AwaitsFix(bugUrl = "https://github.com/elastic/x-plugins/issues/724")
public class SlowWatchStatsTests extends AbstractWatcherIntegrationTestCase { public class SlowWatchStatsTests extends AbstractWatcherIntegrationTestCase {
@Override @Override
protected boolean timeWarped() { protected boolean timeWarped() {
return false; return false;
} }
@Override
protected Collection<Class<? extends Plugin>> nodePlugins() {
List<Class<? extends Plugin>> plugins = new ArrayList<>(super.nodePlugins());
plugins.add(SleepScriptEngine.TestPlugin.class);
return plugins;
}
@Override @Override
protected Settings nodeSettings(int nodeOrdinal) { protected Settings nodeSettings(int nodeOrdinal) {
return Settings.builder() return Settings.builder()
@ -53,7 +68,7 @@ public class SlowWatchStatsTests extends AbstractWatcherIntegrationTestCase {
watcherClient().preparePutWatch("_id").setSource(watchBuilder() watcherClient().preparePutWatch("_id").setSource(watchBuilder()
.trigger(schedule(interval("1s"))) .trigger(schedule(interval("1s")))
.input(InputBuilders.simpleInput("key", "value")) .input(InputBuilders.simpleInput("key", "value"))
.condition(ConditionBuilders.scriptCondition("sleep 10000; return true")) .condition(ConditionBuilders.scriptCondition(SleepScriptEngine.sleepScript(10000)))
.addAction("_action", ActionBuilders.loggingAction("hello {{ctx.watch_id}}!")) .addAction("_action", ActionBuilders.loggingAction("hello {{ctx.watch_id}}!"))
).get(); ).get();
@ -78,7 +93,7 @@ public class SlowWatchStatsTests extends AbstractWatcherIntegrationTestCase {
watcherClient().preparePutWatch("_id" + i).setSource(watchBuilder() watcherClient().preparePutWatch("_id" + i).setSource(watchBuilder()
.trigger(schedule(interval("1s"))) .trigger(schedule(interval("1s")))
.input(InputBuilders.simpleInput("key", "value")) .input(InputBuilders.simpleInput("key", "value"))
.condition(ConditionBuilders.scriptCondition("sleep 10000; return true")) .condition(ConditionBuilders.scriptCondition(SleepScriptEngine.sleepScript(10000)))
.addAction("_action", ActionBuilders.loggingAction("hello {{ctx.watch_id}}!")) .addAction("_action", ActionBuilders.loggingAction("hello {{ctx.watch_id}}!"))
).get(); ).get();
} }