infrastructure-tests/src/test/java/org/gcube/vremanagement/executor/client/DefaultExecutorTest.java

200 lines
7.4 KiB
Java

/**
*
*/
package org.gcube.vremanagement.executor.client;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.acme.HWPluginStateNotification;
import org.acme.HelloWorldPlugin;
import org.acme.HelloWorldPluginDeclaration;
import org.gcube.testutility.ScopedTest;
import org.gcube.vremanagement.executor.api.types.LaunchParameter;
import org.gcube.vremanagement.executor.api.types.Scheduling;
import org.gcube.vremanagement.executor.client.plugins.ExecutorPlugin;
import org.gcube.vremanagement.executor.client.proxies.SmartExecutorProxy;
import org.gcube.vremanagement.executor.exception.PluginInstanceNotFoundException;
import org.gcube.vremanagement.executor.exception.PluginNotFoundException;
import org.gcube.vremanagement.executor.plugin.PluginState;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*
*/
public class DefaultExecutorTest extends ScopedTest {
private static Logger logger = LoggerFactory.getLogger(DefaultExecutorTest.class);
private SmartExecutorProxy proxy;
@Before
public void before() throws Exception{
proxy = ExecutorPlugin.getExecutorProxy("HelloWorld").build();
Assert.assertNotNull(proxy);
}
@Test
public void launch() {
Map<String, Object> inputs = new HashMap<String, Object>();
LaunchParameter launchParameter = new LaunchParameter("Test", inputs);
try {
proxy.launch(launchParameter);
} catch (Exception e) {
Assert.assertEquals(PluginNotFoundException.class, e.getCause().getClass());
}
}
@Test
public void getState() {
String executionIdentifier = UUID.randomUUID().toString();
try {
proxy.getStateEvolution(executionIdentifier);
} catch (Exception e) {
Assert.assertEquals(PluginInstanceNotFoundException.class, e.getCause().getClass());
}
}
@Test
public void testOk() throws Exception {
Map<String, Object> inputs = new HashMap<String, Object>();
inputs.put("Hello", "World");
long sleepTime = 10000; // 1000 millisec * 10 = 10 sec
inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime);
LaunchParameter launchParameter = new LaunchParameter(HelloWorldPluginDeclaration.NAME, inputs);
Map<String, String> notificationInputs = new HashMap<String, String>();
notificationInputs.put("Hello", "Hello World Notification:) :)");
launchParameter.addPluginStateNotifications(HWPluginStateNotification.class, notificationInputs);
try {
String executionIdentifier = proxy.launch(launchParameter);
Thread.sleep(1000); // 1 sec
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(4000); // 4 sec (total : 5 sec)
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(6000); // 6 sec (total : 11 sec)
Assert.assertEquals(PluginState.DONE, proxy.getStateEvolution(executionIdentifier).getPluginState());
} catch (Exception e) {
logger.error("testOk Exception", e);
throw e;
}
}
@Test
public void testScheduledTaskNotPersisted() throws Exception {
proxy = ExecutorPlugin.getExecutorProxy((new HelloWorldPluginDeclaration()).getName()).build();
Assert.assertNotNull(proxy);
Map<String, Object> inputs = new HashMap<String, Object>();
inputs.put("Hello", "World");
long sleepTime = 20000; // 1000 millisec * 20 = 20 sec
inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime);
int minuteinterval = 2;
// Every 5 minutes, for 12 times (one hour totally).
Scheduling scheduling = new Scheduling(60*minuteinterval,12,true);
scheduling.setGlobal(false);
LaunchParameter launchParameter = new LaunchParameter(HelloWorldPluginDeclaration.NAME, inputs, scheduling);
Map<String, String> notificationInputs = new HashMap<String, String>();
notificationInputs.put("Hello", "Hello World Notification:) :)");
launchParameter.addPluginStateNotifications(HWPluginStateNotification.class, notificationInputs);
try {
String executionIdentifier = proxy.launch(launchParameter);
Thread.sleep(1000); // 2 sec
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(4000); // 8 sec (total : 10 sec)
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(6000); // 12 sec (total : 22 sec)
Assert.assertEquals(PluginState.DONE, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(1000*60*minuteinterval); // After 5 minutes the thread should be active again
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Assert.assertEquals(PluginState.RUNNING, proxy.getIterationStateEvolution(executionIdentifier, 1).getPluginState());
//logger.debug("Trying to stop scheduled task");
//proxy.unSchedule(executionIdentifier, true);
} catch (Exception e) {
logger.error("testOk Exception", e);
throw e;
}
}
@Test
public void testScheduledTaskPersisted() throws Exception {
proxy = ExecutorPlugin.getExecutorProxy((new HelloWorldPluginDeclaration()).getName()).build();
Assert.assertNotNull(proxy);
Map<String, Object> inputs = new HashMap<String, Object>();
inputs.put("Hello", "World");
long sleepTime = 20000; // 1000 millisec * 20 = 20 sec
inputs.put(HelloWorldPlugin.SLEEP_TIME, sleepTime);
// Every 5 minutes, for 12 times (one hour totally).
Scheduling scheduling = new Scheduling(60*5,12,true);
scheduling.setGlobal(true);
LaunchParameter launchParameter = new LaunchParameter(HelloWorldPluginDeclaration.NAME, inputs, scheduling);
Map<String, String> notificationInputs = new HashMap<String, String>();
notificationInputs.put("Hello", "Hello World Notification:) :)");
launchParameter.addPluginStateNotifications(HWPluginStateNotification.class, notificationInputs);
try {
String executionIdentifier = proxy.launch(launchParameter);
logger.debug("Task Lauched with ID : {}", executionIdentifier);
Thread.sleep(1000); // 2 sec
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(4000); // 8 sec (total : 10 sec)
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(6000); // 12 sec (total : 22 sec)
Assert.assertEquals(PluginState.DONE, proxy.getStateEvolution(executionIdentifier).getPluginState());
Thread.sleep(1000*60*5); // After 5 minutes the thread should be active again
Assert.assertEquals(PluginState.RUNNING, proxy.getStateEvolution(executionIdentifier).getPluginState());
Assert.assertEquals(PluginState.RUNNING, proxy.getIterationStateEvolution(executionIdentifier, 1).getPluginState());
proxy.stop(executionIdentifier);
Thread.sleep(1000*60*5); // After 5 minutes the thread should be active again. Goign to unschedule globally
logger.debug("Trying to stop scheduled task");
Assert.assertTrue(proxy.unSchedule(executionIdentifier, false));
Thread.sleep(1000*60);
Assert.assertTrue(proxy.unSchedule(executionIdentifier, true));
} catch (Exception e) {
logger.error("testOk Exception", e);
throw e;
}
}
@Test
public void testUnschedule() throws Exception {
//proxy.unSchedule("542ddb03-d8d7-4913-8700-2acfa74c7485", true);
//proxy.unSchedule("37d8f020-cdeb-4b2c-b245-4deabc1fe149", false);
}
}