diff options
author | liamfallon <liam.fallon@est.tech> | 2020-03-23 17:49:50 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2020-03-24 17:40:27 +0000 |
commit | 2f75e9d08d1e47e2b9b39ec21653bc3b4d65d00a (patch) | |
tree | be5374421d8b3f026175ab51d197289ea9dbbe0d /plugins/plugins-executor/plugins-executor-javascript/src/test | |
parent | f7746d758149bc68584c01dc0fe15130c7a866b1 (diff) |
Launch separate threads for Javascript task execution
When a policy is loaded, a separate thread is spawned for each
Javascript script executor. This allows us to precompile the Javascript
scripts and also to have a larger stack available for script execution.
Issue-ID: POLICY-2106
Change-Id: I97323aafb623ba537ac1889b3c9504b345b4f67e
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'plugins/plugins-executor/plugins-executor-javascript/src/test')
4 files changed, 135 insertions, 37 deletions
diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java index 8be79558f..d4a346187 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptStateFinalizerExecutorTest.java @@ -20,6 +20,7 @@ package org.onap.policy.apex.plugins.executor.javascript; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -96,36 +97,59 @@ public class JavascriptStateFinalizerExecutorTest { assertThatThrownBy(() -> { jsfe.prepare(); }).hasMessage("logic failed to compile for NULL:0.0.0:NULL:NULL " - + "with message: invalid return (NULL:0.0.0:NULL:NULL#1)"); + + "with message: invalid return (NULL:0.0.0:NULL:NULL#1)"); Map<String, Object> incomingParameters1 = new HashMap<>(); assertThatThrownBy(() -> { jsfe.execute(-1, new Properties(), incomingParameters1); - }).hasMessage("logic failed to compile for NULL:0.0.0:NULL:NULL " - + "with message: invalid return (NULL:0.0.0:NULL:NULL#1)"); + }).hasMessage("execution failed, executor NULL:0.0.0:NULL:NULL is not running"); + + assertThatThrownBy(() -> { + jsfe.prepare(); + }).hasMessage("initiation failed, executor NULL:0.0.0:NULL:NULL failed to start"); + assertThatThrownBy(() -> { + jsfe.cleanUp(); + }).hasMessage("cleanup failed, executor NULL:0.0.0:NULL:NULL is not running"); + + JavascriptStateFinalizerExecutor jsfe1 = new JavascriptStateFinalizerExecutor(); stateFinalizerLogic.setLogic("java.lang.String"); - jsfe.prepare(); + jsfe1.setContext(parentStateExcutor, stateFinalizerLogic, internalContext); + jsfe1.prepare(); AxEvent axEvent = new AxEvent(new AxArtifactKey("Event", "0.0.1")); EnEvent event = new EnEvent(axEvent); - stateFinalizerLogic.setLogic("if(executor.executionId==-1)" + "{\r\n" - + "var returnValueType = java.lang.Boolean;" + "var returnValue = new returnValueType(false); }\n" - + "else{\n" + "executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n" - + "var returnValueType = java.lang.Boolean;\n" + "\n" - + "var returnValue = new returnValueType(true);} true;"); assertThatThrownBy(() -> { - jsfe.prepare(); - jsfe.execute(-1, new Properties(), event); - }).hasMessage("execute-post: state finalizer logic \"NULL:0.0.0:NULL:NULL\" did not select an output state"); + jsfe1.execute(-1, new Properties(), event); + }).hasMessage( + "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]"); + + assertThatThrownBy(() -> { + jsfe1.execute(-1, new Properties(), event); + }).hasMessage( + "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]"); + + assertThatCode(() -> { + jsfe1.cleanUp(); + }).doesNotThrowAnyException(); + + JavascriptStateFinalizerExecutor jsfe2 = new JavascriptStateFinalizerExecutor(); + + stateFinalizerLogic.setLogic("executor.setSelectedStateOutputName(\"SelectedOutputIsMe\");\n true;"); + + jsfe2.setContext(parentStateExcutor, stateFinalizerLogic, internalContext); + assertThatCode(() -> { + jsfe2.prepare(); + }).doesNotThrowAnyException(); state.getStateOutputs().put("SelectedOutputIsMe", null); - jsfe.prepare(); - String stateOutput = jsfe.execute(0, new Properties(), event); + String stateOutput = jsfe2.execute(0, new Properties(), event); assertEquals("SelectedOutputIsMe", stateOutput); - jsfe.cleanUp(); + assertThatCode(() -> { + jsfe2.cleanUp(); + }).doesNotThrowAnyException(); } } diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java index c327ebb4d..b532d6c47 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskExecutorTest.java @@ -20,6 +20,7 @@ package org.onap.policy.apex.plugins.executor.javascript; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -64,7 +65,7 @@ public class JavascriptTaskExecutorTest { public static void prepareForTest() { final ContextParameters contextParameters = new ContextParameters(); contextParameters.getLockManagerParameters() - .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager"); + .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager"); contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME); contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME); @@ -98,32 +99,47 @@ public class JavascriptTaskExecutorTest { @Test public void testJavascriptTaskExecutor() throws Exception { - JavascriptTaskExecutor jte = new JavascriptTaskExecutor(); - assertNotNull(jte); - assertThatThrownBy(() -> { - jte.prepare(); + JavascriptTaskExecutor jteBadPrep = new JavascriptTaskExecutor(); + jteBadPrep.prepare(); }).isInstanceOf(NullPointerException.class); AxTask task = new AxTask(new AxArtifactKey("TestTask:0.0.1")); final ApexInternalContext internalContext = new ApexInternalContext(new AxPolicyModel()); - jte.setContext(null, task, internalContext); + JavascriptTaskExecutor jteBadLogic = new JavascriptTaskExecutor(); + assertNotNull(jteBadLogic); + + jteBadLogic.setContext(null, task, internalContext); task.getTaskLogic().setLogic("return boolean;"); assertThatThrownBy(() -> { - jte.prepare(); - jte.execute(-1, new Properties(), null); + jteBadLogic.prepare(); }).hasMessage("logic failed to compile for TestTask:0.0.1 with message: invalid return (TestTask:0.0.1#1)"); task.getTaskLogic().setLogic("var x = 5;"); + JavascriptTaskExecutor jte = new JavascriptTaskExecutor(); + jte.setContext(null, task, internalContext); + jte.prepare(); + + assertThatThrownBy(() -> { + jte.prepare(); + }).hasMessage("initiation failed, executor TestTask:0.0.1 is already running"); + assertThatThrownBy(() -> { jte.execute(-1, new Properties(), null); }).isInstanceOf(NullPointerException.class); - jte.cleanUp(); + + assertThatThrownBy(() -> { + jte.execute(-1, new Properties(), null); + }).isInstanceOf(NullPointerException.class); + + assertThatCode(() -> { + jte.cleanUp(); + }).doesNotThrowAnyException(); task.getTaskLogic().setLogic("var returnValue = false;\nreturnValue;"); @@ -142,6 +158,10 @@ public class JavascriptTaskExecutorTest { Map<String, Object> returnMap = jte.execute(0, new Properties(), incomingParameters); assertEquals(0, returnMap.size()); jte.cleanUp(); + + assertThatCode(() -> { + jte.cleanUp(); + }).doesNotThrowAnyException(); } @Test @@ -188,12 +208,12 @@ public class JavascriptTaskExecutorTest { private ContextAlbum createTestContextAlbum() throws ContextException { AxContextSchemas schemas = new AxContextSchemas(); AxContextSchema simpleStringSchema = - new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String"); + new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"), "JAVA", "java.lang.String"); schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema); ModelService.registerModel(AxContextSchemas.class, schemas); AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", - true, AxArtifactKey.getNullKey()); + true, AxArtifactKey.getNullKey()); axContextAlbum.setItemSchema(simpleStringSchema.getKey()); Distributor distributor = new JvmLocalDistributor(); diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java index 3acf132dd..cb577535a 100644 --- a/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/java/org/onap/policy/apex/plugins/executor/javascript/JavascriptTaskSelectExecutorTest.java @@ -20,6 +20,7 @@ package org.onap.policy.apex.plugins.executor.javascript; +import static org.assertj.core.api.Assertions.assertThatCode; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -90,7 +91,7 @@ public class JavascriptTaskSelectExecutorTest { assertThatThrownBy(() -> { jtse.execute(-1, new Properties(), event1); - }).hasMessage("no logic specified for NULL:0.0.0:NULL:NULL"); + }).hasMessage("execution failed, executor NULL:0.0.0:NULL:NULL is not running"); state.getTaskSelectionLogic().setLogic("java.lang.String"); jtse.prepare(); @@ -105,20 +106,32 @@ public class JavascriptTaskSelectExecutorTest { assertThatThrownBy(() -> { jtse.execute(-1, new Properties(), event); }).hasMessage( - "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]"); + "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]"); - state.getTaskSelectionLogic().setLogic("var x=1;\n" + "false; "); + state.getTaskSelectionLogic().setLogic("var x=1;\n" + "false;"); assertThatThrownBy(() -> { - jtse.prepare(); jtse.execute(-1, new Properties(), event); - }).hasMessage("execute-post: task selection logic failed on state \"NULL:0.0.0:NULL:NULL\""); - - state.getTaskSelectionLogic().setLogic("var x = 1\n" + "true; "); + }).hasMessage( + "execute: logic for NULL:0.0.0:NULL:NULL returned a non-boolean value [JavaClass java.lang.String]"); - jtse.prepare(); - AxArtifactKey taskKey = jtse.execute(0, new Properties(), event); - assertEquals("NULL:0.0.0", taskKey.getId()); - jtse.cleanUp(); + assertThatThrownBy(() -> { + jtse.prepare(); + }).hasMessage("initiation failed, executor NULL:0.0.0:NULL:NULL is already running"); + + assertThatCode(() -> { + jtse.cleanUp(); + }).doesNotThrowAnyException(); + + JavascriptTaskSelectExecutor jtse1 = new JavascriptTaskSelectExecutor(); + jtse1.setContext(null, state, internalContext); + state.getTaskSelectionLogic().setLogic("var x = 1\n" + "true;"); + + assertThatCode(() -> { + jtse1.prepare(); + AxArtifactKey taskKey = jtse1.execute(0, new Properties(), event); + assertEquals("NULL:0.0.0", taskKey.getId()); + jtse1.cleanUp(); + }).doesNotThrowAnyException(); } } diff --git a/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/logback-test.xml b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/logback-test.xml new file mode 100644 index 000000000..f3bc0c8ed --- /dev/null +++ b/plugins/plugins-executor/plugins-executor-javascript/src/test/resources/logback-test.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + ============LICENSE_START======================================================= + Copyright (C) 2016-2018 Ericsson. All rights reserved. + Modifications Copyright (C) 2020 Nordix Foundation. + ================================================================================ + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + SPDX-License-Identifier: Apache-2.0 + ============LICENSE_END========================================================= +--> + +<configuration> + <contextName>Apex</contextName> + <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> + + <!-- USE FOR STD OUT ONLY --> + <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <Pattern>%d %contextName [%t] %level %logger{36} - %msg%n</Pattern> + </encoder> + </appender> + + <root level="INFO"> + <appender-ref ref="STDOUT" /> + </root> + + <logger name="org.onap.policy.apex.plugins.executor.javascript" level="INFO" additivity="false"> + <appender-ref ref="STDOUT" /> + </logger> +</configuration> |