--- /dev/null
+/*\r
+ * Copyright (c) 2010 Alexander Sherstnev, Java Bioinformatics Analysis Web Services\r
+ * (JABAWS) @version: 2.5\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.predictors;\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.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
+import java.util.Arrays;\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.data.sequence.Alignment;\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.AllTestSuit;\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.Preset;\r
+import compbio.metadata.PresetManager;\r
+import compbio.metadata.ResultNotAvailableException;\r
+import compbio.metadata.RunnerConfig;\r
+import compbio.runner.Util;\r
+import compbio.util.FileWatcher;\r
+import compbio.util.SysPrefs;\r
+\r
+public class JpredTester {\r
+\r
+ public static final String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() + File.separator;\r
+\r
+ public static String test_output = "Jpred.test1.out";\r
+ public static String test_input = CURRENT_DIRECTORY + "testsrc" + File.separator + "testdata" + File.separator + "Jpred.test1.fasta";\r
+ private Jpred pred;\r
+\r
+ @BeforeMethod(alwaysRun = true)\r
+ void init() {\r
+ pred = new Jpred();\r
+ pred.setInput(test_input);\r
+ pred.setOutput(test_output);\r
+ }\r
+\r
+ @Test(groups = {AllTestSuit.test_group_runner})\r
+ public void testRunOnCluster() {\r
+ assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");\r
+ try {\r
+ PresetManager<Jpred> jpredPreset = Util.getPresets(Jpred.class);\r
+ assert jpredPreset != null;\r
+ ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Cluster);\r
+ Preset<Jpred> conf = jpredPreset.getPresetByName("cluster configuration");\r
+ confpred.addParameters(conf.getOptions());\r
+ JobRunner runner = JobRunner.getInstance(confpred);\r
+ assertNotNull(runner, "Runner is NULL");\r
+\r
+ runner.executeJob();\r
+ JobStatus status = runner.getJobStatus();\r
+ assertTrue(status == JobStatus.PENDING || status == JobStatus.RUNNING, "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, "Statistic manager is null");\r
+ try {\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).intValue();\r
+ assertEquals(0, exitsInt);\r
+ System.out.println(sm.getAllStats());\r
+ } catch (ParseException e) {\r
+ e.printStackTrace();\r
+ fail("Parse Exception: " + e.getMessage());\r
+ }\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(groups = {AllTestSuit.test_group_runner, AllTestSuit.test_group_cluster})\r
+ public void testRunOnClusterAsync() {\r
+ assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");\r
+ try {\r
+ pred.addParameters(Arrays.asList("-dbname uniref90", "-dbpath /homes/www-jpred/databases/"));\r
+ ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Cluster);\r
+ AsyncExecutor aengine = Configurator.getAsyncEngine(confpred);\r
+ String jobId = aengine.submitJob(confpred);\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
+ Thread.sleep(1000);\r
+ status = aengine.getJobStatus(jobId);\r
+ ConfiguredExecutable<Jpred> result = (ConfiguredExecutable<Jpred>) aengine.getResults(jobId);\r
+ assertNotNull(result);\r
+ if (status == JobStatus.UNDEFINED || status == JobStatus.FAILED) {\r
+ fail("job " + jobId +" failed!");\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(groups = {AllTestSuit.test_group_runner})\r
+ public void testRunLocally() {\r
+ try {\r
+ ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Local);\r
+ confpred.addParameters(Arrays.asList("-dbname ported_db", "-dbpath /data/UNIREFdb/"));\r
+\r
+ // For local execution use relative\r
+ LocalRunner lr = new LocalRunner(confpred);\r
+ lr.executeJob();\r
+ ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+ assertNotNull(al1.getResults());\r
+ Alignment annotations = confpred.getResults();\r
+ assertNotNull(annotations);\r
+ assertEquals(annotations.getSize(), 19);\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(groups = {AllTestSuit.test_group_runner})\r
+ public void testRunLocallyWithPreset() {\r
+ try {\r
+ ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(pred, Executable.ExecProvider.Local);\r
+ PresetManager<Jpred> preset = Util.getPresets(Jpred.class);\r
+ assert preset != null;\r
+ Preset<Jpred> conf = preset.getPresetByName("laptop configuration");\r
+ confpred.addParameters(conf.getOptions());\r
+\r
+ // For local execution use relative\r
+ LocalRunner lr = new LocalRunner(confpred);\r
+ lr.executeJob();\r
+ ConfiguredExecutable<?> al1 = lr.waitForResult();\r
+ assertNotNull(al1.getResults());\r
+ Alignment annotations = confpred.getResults();\r
+ assertNotNull(annotations);\r
+ assertEquals(annotations.getSize(), 19);\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(groups = {AllTestSuit.test_group_runner})\r
+ public void readStatistics() {\r
+ try {\r
+ Jpred jpred = new Jpred();\r
+ jpred.setInput(test_input);\r
+ jpred.setOutput(test_output);\r
+ ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(jpred, Executable.ExecProvider.Local);\r
+ PresetManager<Jpred> preset = Util.getPresets(Jpred.class);\r
+ assert preset != null;\r
+ Preset<Jpred> conf = preset.getPresetByName("laptop configuration");\r
+ confpred.addParameters(conf.getOptions());\r
+\r
+ // For local execution use relative\r
+ AsyncExecutor sexec = Configurator.getAsyncEngine(confpred);\r
+ String jobId = sexec.submitJob(confpred);\r
+ String file = confpred.getWorkDirectory() + File.separator + Jpred.getStatFile();\r
+ FilePuller fw = FilePuller.newFilePuller(file, 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
+ if (fw.hasMoreData()) {\r
+ ChunkHolder ch = fw.pull(position);\r
+ String chunk = ch.getChunk();\r
+ position = ch.getNextPosition();\r
+ }\r
+ count++;\r
+ // Make sure the loop is terminated if the job fails\r
+ if ((status == JobStatus.UNDEFINED || status == JobStatus.FAILED)) {\r
+ fail("job " + jobId +" failed!");\r
+ break;\r
+ }\r
+ Thread.sleep(200);\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(groups = {AllTestSuit.test_group_runner})\r
+ public void testPersistance() {\r
+ try {\r
+ Jpred jpred = new Jpred();\r
+ jpred.setError("errrr.txt");\r
+ jpred.setInput(test_input);\r
+ jpred.setOutput("outtt.txt");\r
+ assertEquals(jpred.getInput(), test_input);\r
+ assertEquals(jpred.getError(), "errrr.txt");\r
+ assertEquals(jpred.getOutput(), "outtt.txt");\r
+ ConfiguredExecutable<Jpred> confpred = Configurator.configureExecutable(jpred, Executable.ExecProvider.Local);\r
+ confpred.addParameters(Arrays.asList("-dbname ported_db", "-dbpath /data/UNIREFdb/"));\r
+\r
+ SyncExecutor sexec = Configurator.getSyncEngine(confpred);\r
+ sexec.executeJob();\r
+ ConfiguredExecutable<?> al = sexec.waitForResult();\r
+ assertNotNull(al.getResults());\r
+\r
+ // Save run configuration\r
+ assertTrue(confpred.saveRunConfiguration());\r
+\r
+ // See if loaded configuration is the same as saved\r
+ RunConfiguration loadedRun = RunConfiguration.load(new FileInputStream(new File(confpred.getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+ assertEquals(((ConfExecutable<Jpred>) confpred).getRunConfiguration(),loadedRun);\r
+\r
+ // Load run configuration as ConfExecutable\r
+ ConfiguredExecutable<Jpred> jpred2 = (ConfiguredExecutable<Jpred>) confpred.loadRunConfiguration(new FileInputStream(new File(confpred.getWorkDirectory(), RunConfiguration.rconfigFile)));\r
+ assertNotNull(jpred2);\r
+ assertEquals(jpred2.getExecutable().getInput(), test_input);\r
+ assertEquals(jpred2.getExecutable().getError(), "errrr.txt");\r
+ assertEquals(jpred2.getExecutable().getOutput(), "outtt.txt");\r
+\r
+ // See in details whether executables are the same\r
+ assertEquals(jpred2.getExecutable(), jpred);\r
+ ConfiguredExecutable<Jpred> resjpred2 = Configurator.configureExecutable(jpred2.getExecutable(), Executable.ExecProvider.Local);\r
+\r
+ sexec = Configurator.getSyncEngine(resjpred2, Executable.ExecProvider.Local);\r
+ sexec.executeJob();\r
+ al = sexec.waitForResult();\r
+ assertNotNull(al);\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(groups = {AllTestSuit.test_group_runner})\r
+ public void testConfigurationLoading() {\r
+ try {\r
+ RunnerConfig<Jpred> jpredConfig = ConfExecutable.getRunnerOptions(Jpred.class);\r
+ assertNotNull(jpredConfig);\r
+ assertTrue(jpredConfig.getArguments().size() > 0);\r
+\r
+ PresetManager<Jpred> jpredPreset = ConfExecutable.getRunnerPresets(Jpred.class);\r
+ assertNotNull(jpredPreset);\r
+\r
+ LimitsManager<Jpred> jpredLimits = ConfExecutable.getRunnerLimits(Jpred.class);\r
+ assertNotNull(jpredLimits);\r
+ assertTrue(jpredLimits.getLimits().size() > 0);\r
+ jpredLimits.validate(jpredPreset);\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