Merge pull request #15476 from ywelsch/fix/script-service-tests

[TEST] Fix ScriptServiceTests.testFineGrainedSettings that can loop indefinitely
This commit is contained in:
Yannick Welsch 2015-12-16 18:05:55 +01:00
commit 6b49d728ec
1 changed files with 42 additions and 47 deletions

View File

@ -33,6 +33,7 @@ import org.junit.Before;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -48,7 +49,7 @@ import static org.hamcrest.Matchers.sameInstance;
public class ScriptServiceTests extends ESTestCase { public class ScriptServiceTests extends ESTestCase {
private ResourceWatcherService resourceWatcherService; private ResourceWatcherService resourceWatcherService;
private Set<ScriptEngineService> scriptEngineServices; private ScriptEngineService scriptEngineService;
private Map<String, ScriptEngineService> scriptEnginesByLangMap; private Map<String, ScriptEngineService> scriptEnginesByLangMap;
private ScriptContextRegistry scriptContextRegistry; private ScriptContextRegistry scriptContextRegistry;
private ScriptContext[] scriptContexts; private ScriptContext[] scriptContexts;
@ -72,8 +73,8 @@ public class ScriptServiceTests extends ESTestCase {
.put("path.conf", genericConfigFolder) .put("path.conf", genericConfigFolder)
.build(); .build();
resourceWatcherService = new ResourceWatcherService(baseSettings, null); resourceWatcherService = new ResourceWatcherService(baseSettings, null);
scriptEngineServices = newHashSet(new TestEngineService()); scriptEngineService = new TestEngineService();
scriptEnginesByLangMap = ScriptModesTests.buildScriptEnginesByLangMap(scriptEngineServices); scriptEnginesByLangMap = ScriptModesTests.buildScriptEnginesByLangMap(Collections.singleton(scriptEngineService));
//randomly register custom script contexts //randomly register custom script contexts
int randomInt = randomIntBetween(0, 3); int randomInt = randomIntBetween(0, 3);
//prevent duplicates using map //prevent duplicates using map
@ -100,7 +101,7 @@ public class ScriptServiceTests extends ESTestCase {
private void buildScriptService(Settings additionalSettings) throws IOException { private void buildScriptService(Settings additionalSettings) throws IOException {
Settings finalSettings = Settings.builder().put(baseSettings).put(additionalSettings).build(); Settings finalSettings = Settings.builder().put(baseSettings).put(additionalSettings).build();
Environment environment = new Environment(finalSettings); Environment environment = new Environment(finalSettings);
scriptService = new ScriptService(finalSettings, environment, scriptEngineServices, resourceWatcherService, scriptContextRegistry) { scriptService = new ScriptService(finalSettings, environment, Collections.singleton(scriptEngineService), resourceWatcherService, scriptContextRegistry) {
@Override @Override
String getScriptFromIndex(String scriptLang, String id, HasContextAndHeaders headersContext) { String getScriptFromIndex(String scriptLang, String id, HasContextAndHeaders headersContext) {
//mock the script that gets retrieved from an index //mock the script that gets retrieved from an index
@ -225,13 +226,11 @@ public class ScriptServiceTests extends ESTestCase {
} while (scriptContextSettings.containsKey(scriptContext)); } while (scriptContextSettings.containsKey(scriptContext));
scriptContextSettings.put(scriptContext, randomFrom(ScriptMode.values())); scriptContextSettings.put(scriptContext, randomFrom(ScriptMode.values()));
} }
int numEngineSettings = randomIntBetween(0, 10); int numEngineSettings = randomIntBetween(0, ScriptType.values().length * scriptContexts.length);
Map<String, ScriptMode> engineSettings = new HashMap<>(); Map<String, ScriptMode> engineSettings = new HashMap<>();
for (int i = 0; i < numEngineSettings; i++) { for (int i = 0; i < numEngineSettings; i++) {
String settingKey; String settingKey;
do { do {
ScriptEngineService[] scriptEngineServices = this.scriptEngineServices.toArray(new ScriptEngineService[this.scriptEngineServices.size()]);
ScriptEngineService scriptEngineService = randomFrom(scriptEngineServices);
ScriptType scriptType = randomFrom(ScriptType.values()); ScriptType scriptType = randomFrom(ScriptType.values());
ScriptContext scriptContext = randomFrom(this.scriptContexts); ScriptContext scriptContext = randomFrom(this.scriptContexts);
settingKey = scriptEngineService.types()[0] + "." + scriptType + "." + scriptContext.getKey(); settingKey = scriptEngineService.types()[0] + "." + scriptType + "." + scriptContext.getKey();
@ -288,7 +287,6 @@ public class ScriptServiceTests extends ESTestCase {
buildScriptService(builder.build()); buildScriptService(builder.build());
createFileScripts("groovy", "expression", "mustache", "test"); createFileScripts("groovy", "expression", "mustache", "test");
for (ScriptEngineService scriptEngineService : scriptEngineServices) {
for (ScriptType scriptType : ScriptType.values()) { for (ScriptType scriptType : ScriptType.values()) {
//make sure file scripts have a different name than inline ones. //make sure file scripts have a different name than inline ones.
//Otherwise they are always considered file ones as they can be found in the static cache. //Otherwise they are always considered file ones as they can be found in the static cache.
@ -326,7 +324,6 @@ public class ScriptServiceTests extends ESTestCase {
} }
} }
} }
}
public void testCompileNonRegisteredContext() throws IOException { public void testCompileNonRegisteredContext() throws IOException {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder(); ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();
@ -338,7 +335,6 @@ public class ScriptServiceTests extends ESTestCase {
unknownContext = randomAsciiOfLength(randomIntBetween(1, 30)); unknownContext = randomAsciiOfLength(randomIntBetween(1, 30));
} while(scriptContextRegistry.isSupportedContext(new ScriptContext.Plugin(pluginName, unknownContext))); } while(scriptContextRegistry.isSupportedContext(new ScriptContext.Plugin(pluginName, unknownContext)));
for (ScriptEngineService scriptEngineService : scriptEngineServices) {
for (String type : scriptEngineService.types()) { for (String type : scriptEngineService.types()) {
try { try {
scriptService.compile(new Script("test", randomFrom(ScriptType.values()), type, null), new ScriptContext.Plugin( scriptService.compile(new Script("test", randomFrom(ScriptType.values()), type, null), new ScriptContext.Plugin(
@ -349,7 +345,6 @@ public class ScriptServiceTests extends ESTestCase {
} }
} }
} }
}
public void testCompileCountedInCompilationStats() throws IOException { public void testCompileCountedInCompilationStats() throws IOException {
ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder(); ContextAndHeaderHolder contextAndHeaders = new ContextAndHeaderHolder();