Add testing dirs
[proteocache.git] / testsrc / compbio / engine / cluster / drmaa / DrmaaAsyncClusterEngineTester.java
diff --git a/testsrc/compbio/engine/cluster/drmaa/DrmaaAsyncClusterEngineTester.java b/testsrc/compbio/engine/cluster/drmaa/DrmaaAsyncClusterEngineTester.java
new file mode 100644 (file)
index 0000000..922aa90
--- /dev/null
@@ -0,0 +1,184 @@
+/* Copyright (c) 2009 Peter Troshin\r
+ *  \r
+ *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
+ * \r
+ *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
+ *  Apache License version 2 as published by the Apache Software Foundation\r
+ * \r
+ *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
+ *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
+ *  License for more details.\r
+ * \r
+ *  A copy of the license is in apache_license.txt. It is also available here:\r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
+ * \r
+ * Any republication or derived work distributed in source code form\r
+ * must include this copyright and license notice.\r
+ */\r
+\r
+package compbio.engine.cluster.drmaa;\r
+\r
+import static org.testng.AssertJUnit.assertEquals;\r
+import static org.testng.AssertJUnit.assertFalse;\r
+import static org.testng.AssertJUnit.assertNotNull;\r
+import static org.testng.AssertJUnit.assertNull;\r
+import static org.testng.AssertJUnit.assertTrue;\r
+import static org.testng.AssertJUnit.fail;\r
+\r
+import org.testng.annotations.Test;\r
+\r
+import compbio.engine.AsyncExecutor;\r
+import compbio.engine.Configurator;\r
+import compbio.engine.client.ConfiguredExecutable;\r
+import compbio.engine.client.Executable;\r
+import compbio.metadata.AllTestSuit;\r
+import compbio.metadata.JobStatus;\r
+import compbio.metadata.JobSubmissionException;\r
+import compbio.metadata.ResultNotAvailableException;\r
+import compbio.runner.msa.ClustalW;\r
+import compbio.util.SysPrefs;\r
+\r
+public class DrmaaAsyncClusterEngineTester {\r
+\r
+       public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
+                       + "TO1381.fasta";\r
+       public static String large_test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
+                       + "1000x3000Dna.fasta";\r
+       public static String cluster_test_outfile = "TO1381.clustal.cluster.out";\r
+\r
+       /**\r
+        * This test uses ClustalW executable as runnable to testing, thus depends\r
+        * on its correct functioning\r
+        */\r
+       @Test(groups = { AllTestSuit.test_group_cluster,\r
+                       AllTestSuit.test_group_engine })\r
+       public void testSubmit() {\r
+               ClustalW clustal = new ClustalW();\r
+               assertFalse("Cluster execution can only be in unix environment",\r
+                               SysPrefs.isWindows);\r
+               clustal.setInput(test_input).setOutput(cluster_test_outfile);\r
+               try {\r
+                       ConfiguredExecutable<ClustalW> confClustal = Configurator\r
+                                       .configureExecutable(clustal);\r
+                       AsyncExecutor runner = new AsyncClusterRunner();\r
+                       assertNotNull("Runner is NULL", runner);\r
+                       String jobId = runner.submitJob(confClustal);\r
+                       assertEquals("Input was not set!", test_input, clustal.getInput());\r
+                       assertNotNull("JobId is null", jobId);\r
+                       JobStatus status = runner.getJobStatus(jobId);\r
+                       assertTrue("Status of the process is wrong!",\r
+                                       status == JobStatus.PENDING || status == JobStatus.RUNNING);\r
+                       JobStatus info = runner.getJobStatus(jobId);\r
+                       assertNotNull("JobInfo is null", info);\r
+                       Executable<?> result = runner.getResults(jobId);\r
+\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               }\r
+       }\r
+\r
+       @Test(expectedExceptions = ResultNotAvailableException.class, groups = {\r
+                       AllTestSuit.test_group_cluster, AllTestSuit.test_group_engine })\r
+       // expectedExceptions = ResultNotAvailableException.class,\r
+       public void testCancel() throws ResultNotAvailableException {\r
+               ClustalW clustal = new ClustalW();\r
+               assertFalse("Cluster execution can only be in unix environment",\r
+                               SysPrefs.isWindows);\r
+               clustal.setInput(large_test_input).setOutput(cluster_test_outfile);\r
+\r
+               try {\r
+                       ConfiguredExecutable<ClustalW> confClustal = Configurator\r
+                                       .configureExecutable(clustal);\r
+                       AsyncClusterRunner runner = new AsyncClusterRunner();\r
+                       String jobId = runner.submitJob(confClustal);\r
+                       assertNotNull("Runner is NULL", runner);\r
+                       // assertNotNull("JobId is null", jobId1);\r
+                       Thread.sleep(200);\r
+                       JobStatus status = runner.getJobStatus(jobId);\r
+                       assertTrue("Status of the process is wrong!",\r
+                                       status == JobStatus.PENDING || status == JobStatus.RUNNING);\r
+                       assertFalse("Status of the process is wrong!",\r
+                                       status == JobStatus.FINISHED);\r
+                       runner.cancelJob(jobId);\r
+                       // This is never NULL as long as the job has started!\r
+                       ConfiguredExecutable<?> exec = runner.getResults(jobId);\r
+                       assertNull(exec.getResults());\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail("DrmaaException caught:" + e.getMessage());\r
+               } catch (InterruptedException e) {\r
+                       e.printStackTrace();\r
+                       fail("Interupted exception caught:" + e.getMessage());\r
+               }\r
+       }\r
+\r
+       @Test(groups = { AllTestSuit.test_group_cluster,\r
+                       AllTestSuit.test_group_engine })\r
+       public void testGetJobStatus() {\r
+               ClustalW clustal = new ClustalW();\r
+               assertFalse("Cluster execution can only be in unix environment",\r
+                               SysPrefs.isWindows);\r
+               clustal.setInput(test_input).setOutput(cluster_test_outfile);\r
+\r
+               try {\r
+                       AsyncClusterRunner runner = new AsyncClusterRunner();\r
+                       ConfiguredExecutable<ClustalW> confClustal = Configurator\r
+                                       .configureExecutable(clustal);\r
+                       String jobId = runner.submitJob(confClustal);\r
+                       assertNotNull("Runner is NULL", runner);\r
+                       AsyncClusterRunner runner2 = new AsyncClusterRunner();\r
+\r
+                       boolean hasRun = false;\r
+                       boolean hasPended = false;\r
+                       Thread.sleep(500); \r
+                       JobStatus status = runner2.getJobStatus(jobId);\r
+                       while (status != JobStatus.FINISHED) {\r
+                               if (status == JobStatus.CANCELLED) {\r
+                                       fail("Job is not cancelled!");\r
+                               }\r
+                               if (status == JobStatus.FAILED) {\r
+                                       fail("Job should not fail!");\r
+                               }\r
+                               if (status == JobStatus.RUNNING) {\r
+                                       hasRun = true;\r
+                               }\r
+                               if (status == JobStatus.PENDING) {\r
+                                       hasPended = true;\r
+                               }\r
+                               if (status == JobStatus.UNDEFINED) {\r
+                                       System.out.println("Wrong status (UNDEFINED) reported by cluster engine!");\r
+                                       break;\r
+                               }\r
+                               status = runner2.getJobStatus(jobId);\r
+                       }\r
+                       assertTrue(hasRun);\r
+                       assertTrue(hasPended);\r
+                       assertTrue(hasRun);\r
+                       // Bear in mind that if the task were not put in the queue\r
+                       // immediately\r
+                       // the status could be UNDEFINED!\r
+                       // assertFalse(hasUndefined);\r
+                       AsyncClusterRunner runner3 = new AsyncClusterRunner();\r
+                       Executable<?> exec = runner3.getResults(jobId);\r
+                       assertNotNull(exec);\r
+                       // Now try collecting result for the second time\r
+                       exec=null;\r
+                       Thread.sleep(1000);\r
+                       exec = runner3.getResults(jobId);\r
+                       assertNotNull(exec);\r
+               } catch (JobSubmissionException e) {\r
+                       e.printStackTrace();\r
+                       fail("DrmaaException caught:" + e.getMessage());\r
+               } catch (ResultNotAvailableException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               } catch (InterruptedException e) {\r
+                       e.printStackTrace();\r
+                       fail(e.getMessage());\r
+               }\r
+       }\r
+}\r