+/*\r
+ * Copyright (c) 2010 Peter Troshin JAva Bioinformatics Analysis Web Services\r
+ * (JABAWS) @version: 2.0 \r
+ * \r
+ * This library is free software; you can redistribute it and/or modify it under \r
+ * the terms of the Apache License version 2 as published\r
+ * by the Apache Software Foundation This library is distributed in the hope\r
+ * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
+ * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+ * Apache License for more details. A copy of the license is in\r
+ * apache_license.txt. It is also available here:\r
+ * \r
+ * @see: http://www.apache.org/licenses/LICENSE-2.0.txt \r
+ * \r
+ * Any republication or derived work distributed in source code form must include \r
+ * this copyright and license notice.\r
+ */\r
+package compbio.runner.conservation;\r
+\r
+import static org.testng.Assert.assertEquals;\r
+import static org.testng.Assert.assertFalse;\r
+import static org.testng.Assert.assertNotNull;\r
+import static org.testng.Assert.assertNull;\r
+import static org.testng.Assert.assertTrue;\r
+import static org.testng.Assert.fail;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.IOException;\r
+import java.text.ParseException;\r
+\r
+import javax.xml.bind.ValidationException;\r
+\r
+import org.ggf.drmaa.DrmaaException;\r
+import org.ggf.drmaa.JobInfo;\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.Test;\r
+\r
+import compbio.conservation.Method;\r
+import compbio.data.sequence.MultiAnnotatedSequence;\r
+import compbio.engine.AsyncExecutor;\r
+import compbio.engine.Configurator;\r
+import compbio.engine.FilePuller;\r
+import compbio.engine.SyncExecutor;\r
+import compbio.engine.client.ConfExecutable;\r
+import compbio.engine.client.ConfiguredExecutable;\r
+import compbio.engine.client.Executable;\r
+import compbio.engine.client.RunConfiguration;\r
+import compbio.engine.cluster.drmaa.ClusterUtil;\r
+import compbio.engine.cluster.drmaa.JobRunner;\r
+import compbio.engine.cluster.drmaa.StatisticManager;\r
+import compbio.engine.local.LocalRunner;\r
+import compbio.metadata.ChunkHolder;\r
+import compbio.metadata.JobExecutionException;\r
+import compbio.metadata.JobStatus;\r
+import compbio.metadata.JobSubmissionException;\r
+import compbio.metadata.LimitsManager;\r
+import compbio.metadata.PresetManager;\r
+import compbio.metadata.ResultNotAvailableException;\r
+import compbio.metadata.RunnerConfig;\r
+import compbio.util.FileWatcher;\r
+import compbio.util.SysPrefs;\r
+\r
+public class AAConTester {\r
+\r
+ public static final String CURRENT_DIRECTORY = SysPrefs\r
+ .getCurrentDirectory() + File.separator;\r
+\r
+ public static String test_outfile = "TO1381.aacon.out"; // "/homes/pvtroshin/TO1381.clustal.cluster.out\r
+ public static String test_alignment_input = CURRENT_DIRECTORY + "testsrc"\r
+ + File.separator + "testdata" + File.separator + "TO1381.fasta.aln";\r
+ private AACon aacon;\r
+\r
+ @BeforeMethod(alwaysRun = true)\r
+ void init() {\r
+ aacon = new AACon();\r
+ aacon.setInput(test_alignment_input).setOutput(test_outfile);\r
+ }\r
+\r
+ @Test()\r
+ public void testRunOnCluster() {\r
+ assertFalse(SysPrefs.isWindows,\r
+ "Cluster execution can only be in unix environment");\r
+ try {\r
+ ConfiguredExecutable<AACon> confAAcon = Configurator\r
+ .configureExecutable(aacon, Executable.ExecProvider.Cluster);\r
+ JobRunner runner = JobRunner.getInstance(confAAcon);\r
+\r
+ assertNotNull(runner, "Runner is NULL");\r
+ runner.executeJob();\r
+ // assertNotNull("JobId is null", jobId1);\r
+ JobStatus status = runner.getJobStatus();\r
+ assertTrue(status == JobStatus.PENDING\r
+ || status == JobStatus.RUNNING,\r
+ "Status of the process is wrong!");\r
+ JobInfo info = runner.getJobInfo();\r
+ assertNotNull(info, "JobInfo is null");\r
+ StatisticManager sm = new StatisticManager(info);\r
+ assertNotNull(sm, "Statictic manager is null");\r
+ try {\r
+\r
+ String exits = sm.getExitStatus();\r
+ assertNotNull("Exit status is null", exits);\r
+ // cut 4 trailing zeros from the number\r
+ int exitsInt = ClusterUtil.CLUSTER_STAT_IN_SEC.parse(exits)\r
+ .intValue();\r
+ assertEquals(0, exitsInt);\r
+ System.out.println(sm.getAllStats());\r
+\r
+ } catch (ParseException e) {\r
+ e.printStackTrace();\r
+ fail("Parse Exception: " + e.getMessage());\r
+ }\r
+ // assertFalse(runner.cleanup());\r
+ assertTrue(sm.hasExited());\r
+ assertFalse(sm.wasAborted());\r
+ assertFalse(sm.hasDump());\r
+ assertFalse(sm.hasSignaled());\r
+\r
+ } catch (JobSubmissionException e) {\r
+ e.printStackTrace();\r
+ fail("DrmaaException caught:" + e.getMessage());\r
+ } catch (JobExecutionException e) {\r
+ e.printStackTrace();\r
+ fail("DrmaaException caught:" + e.getMessage());\r
+ } catch (DrmaaException e) {\r
+ e.printStackTrace();\r
+ fail("DrmaaException caught:" + e.getMessage());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * This tests fails from time to time depending on the cluster load or some\r
+ * other factors. Any client code has to adjust for this issue\r
+ */\r
+ @Test()\r
+ public void testRunOnClusterAsync() {\r
+ assertFalse(SysPrefs.isWindows,\r
+ "Cluster execution can only be in unix environment");\r
+ try {\r
+ ConfiguredExecutable<AACon> confAAcon = Configurator\r
+ .configureExecutable(aacon, Executable.ExecProvider.Cluster);\r
+ AsyncExecutor aengine = Configurator.getAsyncEngine(confAAcon);\r
+ String jobId = aengine.submitJob(confAAcon);\r
+ assertNotNull(jobId, "Runner is NULL");\r
+ // let drmaa to start\r
+ Thread.sleep(500);\r
+ JobStatus status = aengine.getJobStatus(jobId);\r
+ while (status != JobStatus.FINISHED) {\r
+ System.out.println("Job Status: " + status);\r
+ Thread.sleep(1000);\r
+ status = aengine.getJobStatus(jobId);\r
+ ConfiguredExecutable<AACon> result = (ConfiguredExecutable<AACon>) aengine\r
+ .getResults(jobId);\r
+ assertNotNull(result);\r
+ System.out.println("RES:" + result);\r
+ // Some times the job could be removed from the cluster\r
+ // accounting\r
+ // before it has been reported to finish. Make sure\r
+ // to stop waiting in such case\r
+ if (status == JobStatus.UNDEFINED) {\r
+ break;\r
+ }\r
+ }\r
+ } catch (JobSubmissionException e) {\r
+ e.printStackTrace();\r
+ fail("DrmaaException caught:" + e.getMessage());\r
+ } catch (InterruptedException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (ResultNotAvailableException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ }\r
+ }\r
+\r
+ @Test()\r
+ public void testRunLocally() {\r
+ try {\r
+ ConfiguredExecutable<AACon> confAAcon = Configurator\r
+ .configureExecutable(aacon, Executable.ExecProvider.Local);\r
+\r
+ // For local execution use relative\r
+ LocalRunner lr = new LocalRunner(confAAcon);\r
+ lr.executeJob();\r
+ ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+ assertNotNull(al1.getResults());\r
+ MultiAnnotatedSequence<Method> annotations = confAAcon.getResults();\r
+ assertNotNull(annotations);\r
+ assertEquals(annotations.getAnnotations().size(), 18);\r
+ assertEquals(al1.getResults(), annotations);\r
+ } catch (JobSubmissionException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (ResultNotAvailableException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (JobExecutionException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+ }\r
+\r
+ @Test()\r
+ public void testRunLocallyOnTwoCpu() {\r
+ try {\r
+ aacon.setNCore(2);\r
+ ConfiguredExecutable<AACon> confAAcon = Configurator\r
+ .configureExecutable(aacon, Executable.ExecProvider.Local);\r
+\r
+ // For local execution use relative\r
+ LocalRunner lr = new LocalRunner(confAAcon);\r
+ lr.executeJob();\r
+ ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+ assertNotNull(al1.getResults());\r
+ MultiAnnotatedSequence<Method> annotations = confAAcon.getResults();\r
+ assertNotNull(annotations);\r
+ assertEquals(annotations.getAnnotations().size(), 18);\r
+ assertEquals(al1.getResults(), annotations);\r
+ } catch (JobSubmissionException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (ResultNotAvailableException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (JobExecutionException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+ }\r
+\r
+ @Test()\r
+ public void readStatistics() {\r
+ try {\r
+ ConfiguredExecutable<AACon> confAAcon = Configurator\r
+ .configureExecutable(aacon, Executable.ExecProvider.Local);\r
+ // For local execution use relative\r
+\r
+ AsyncExecutor sexec = Configurator.getAsyncEngine(confAAcon);\r
+ String jobId = sexec.submitJob(confAAcon);\r
+ FilePuller fw = FilePuller.newFilePuller(\r
+ confAAcon.getWorkDirectory() + File.separator\r
+ + AACon.getStatFile(),\r
+ FileWatcher.MIN_CHUNK_SIZE_BYTES);\r
+ int count = 0;\r
+ long position = 0;\r
+ fw.waitForFile(2);\r
+ JobStatus status = sexec.getJobStatus(jobId);\r
+ do {\r
+ ChunkHolder ch = fw.pull(position);\r
+ String chunk = ch.getChunk();\r
+ position = ch.getNextPosition();\r
+ // System.out.println(chunk);\r
+ count++;\r
+ // Make sure the loop is terminated if the job fails\r
+ if ((status == JobStatus.UNDEFINED || status == JobStatus.FAILED)) {\r
+ fail("job failed!");\r
+ break;\r
+ }\r
+ Thread.sleep(300);\r
+ status = sexec.getJobStatus(jobId);\r
+ } while (status != JobStatus.FINISHED || fw.hasMoreData());\r
+\r
+ assertTrue(count >= 1);\r
+ ConfiguredExecutable<?> al = sexec.getResults(jobId);\r
+ assertNotNull(al.getResults());\r
+ } catch (JobSubmissionException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (ResultNotAvailableException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (InterruptedException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ }\r
+ }\r
+\r
+ @Test()\r
+ public void testPersistance() {\r
+ try {\r
+ AACon aacon = new AACon();\r
+ aacon.setError("errrr.txt").setInput(test_alignment_input)\r
+ .setOutput("outtt.txt");\r
+ assertEquals(aacon.getInput(), test_alignment_input);\r
+ assertEquals(aacon.getError(), "errrr.txt");\r
+ assertEquals(aacon.getOutput(), "outtt.txt");\r
+ ConfiguredExecutable<AACon> cAAcon = Configurator\r
+ .configureExecutable(aacon, Executable.ExecProvider.Local);\r
+\r
+ SyncExecutor sexec = Configurator.getSyncEngine(cAAcon);\r
+ sexec.executeJob();\r
+ ConfiguredExecutable<?> al = sexec.waitForResult();\r
+ assertNotNull(al.getResults());\r
+ // Save run configuration\r
+ assertTrue(cAAcon.saveRunConfiguration());\r
+\r
+ // See if loaded configuration is the same as saved\r
+ RunConfiguration loadedRun = RunConfiguration\r
+ .load(new FileInputStream(new File(cAAcon\r
+ .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+ assertEquals(\r
+ ((ConfExecutable<AACon>) cAAcon).getRunConfiguration(),\r
+ loadedRun);\r
+ // Load run configuration as ConfExecutable\r
+ ConfiguredExecutable<AACon> resurrectedCAAcon = (ConfiguredExecutable<AACon>) cAAcon\r
+ .loadRunConfiguration(new FileInputStream(new File(cAAcon\r
+ .getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+ assertNotNull(resurrectedCAAcon);\r
+ assertEquals(resurrectedCAAcon.getExecutable().getInput(),\r
+ test_alignment_input);\r
+ assertEquals(resurrectedCAAcon.getExecutable().getError(),\r
+ "errrr.txt");\r
+ assertEquals(resurrectedCAAcon.getExecutable().getOutput(),\r
+ "outtt.txt");\r
+ // See in details whether executables are the same\r
+ assertEquals(resurrectedCAAcon.getExecutable(), aacon);\r
+\r
+ ConfiguredExecutable<AACon> resAAcon = Configurator\r
+ .configureExecutable(resurrectedCAAcon.getExecutable(),\r
+ Executable.ExecProvider.Local);\r
+\r
+ sexec = Configurator.getSyncEngine(resAAcon,\r
+ Executable.ExecProvider.Local);\r
+ sexec.executeJob();\r
+ al = sexec.waitForResult();\r
+ assertNotNull(al);\r
+\r
+ } catch (JobSubmissionException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (JobExecutionException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ } catch (ResultNotAvailableException e) {\r
+ e.printStackTrace();\r
+ fail(e.getMessage());\r
+ }\r
+ }\r
+\r
+ @Test()\r
+ public void testConfigurationLoading() {\r
+ try {\r
+ RunnerConfig<AACon> aaconConfig = ConfExecutable\r
+ .getRunnerOptions(AACon.class);\r
+ assertNotNull(aaconConfig);\r
+ assertTrue(aaconConfig.getArguments().size() > 0);\r
+\r
+ PresetManager<AACon> aaconPresets = ConfExecutable\r
+ .getRunnerPresets(AACon.class);\r
+ assertNull(aaconPresets); // there is no presets\r
+\r
+ LimitsManager<AACon> jronnLimits = ConfExecutable\r
+ .getRunnerLimits(AACon.class);\r
+ assertNotNull(jronnLimits);\r
+ assertTrue(jronnLimits.getLimits().size() > 0);\r
+ jronnLimits.validate(aaconPresets);\r
+\r
+ } catch (FileNotFoundException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (IOException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ } catch (ValidationException e) {\r
+ e.printStackTrace();\r
+ fail(e.getLocalizedMessage());\r
+ }\r
+ }\r
+\r
+}\r