1 /* Copyright (c) 2010 Peter Troshin
\r
2 * Copyright (c) 2013 Alexander Sherstnev
\r
4 * Java Bioinformatics Analysis Web Services (JABAWS)
\r
5 * (JABAWS) @version: 2.5
\r
7 * This library is free software; you can redistribute it and/or modify it under
\r
8 * the terms of the Apache License version 2 as published
\r
9 * by the Apache Software Foundation This library is distributed in the hope
\r
10 * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
\r
11 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
12 * Apache License for more details. A copy of the license is in
\r
13 * apache_license.txt. It is also available here:
\r
15 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
17 * Any republication or derived work distributed in source code form must include
\r
18 * this copyright and license notice.
\r
20 package compbio.runner.conservation;
\r
22 import static org.testng.Assert.assertEquals;
\r
23 import static org.testng.Assert.assertFalse;
\r
24 import static org.testng.Assert.assertNotNull;
\r
25 import static org.testng.Assert.assertTrue;
\r
26 import static org.testng.Assert.fail;
\r
28 import java.io.File;
\r
29 import java.io.FileInputStream;
\r
30 import java.io.FileNotFoundException;
\r
31 import java.io.IOException;
\r
32 import java.text.ParseException;
\r
33 import java.util.Arrays;
\r
35 import javax.xml.bind.ValidationException;
\r
37 import org.ggf.drmaa.DrmaaException;
\r
38 import org.ggf.drmaa.JobInfo;
\r
39 import org.testng.annotations.BeforeMethod;
\r
40 import org.testng.annotations.Test;
\r
42 import compbio.data.sequence.ScoreManager;
\r
43 import compbio.engine.AsyncExecutor;
\r
44 import compbio.engine.Configurator;
\r
45 import compbio.engine.FilePuller;
\r
46 import compbio.engine.SyncExecutor;
\r
47 import compbio.engine.client.ConfExecutable;
\r
48 import compbio.engine.client.ConfiguredExecutable;
\r
49 import compbio.engine.client.Executable;
\r
50 import compbio.engine.client.RunConfiguration;
\r
51 import compbio.engine.cluster.drmaa.ClusterEngineUtil;
\r
52 import compbio.engine.cluster.drmaa.JobRunner;
\r
53 import compbio.engine.cluster.drmaa.StatisticManager;
\r
54 import compbio.engine.local.LocalRunner;
\r
55 import compbio.metadata.AllTestSuit;
\r
56 import compbio.metadata.ChunkHolder;
\r
57 import compbio.metadata.JobExecutionException;
\r
58 import compbio.metadata.JobStatus;
\r
59 import compbio.metadata.JobSubmissionException;
\r
60 import compbio.metadata.LimitsManager;
\r
61 import compbio.metadata.Preset;
\r
62 import compbio.metadata.PresetManager;
\r
63 import compbio.metadata.ResultNotAvailableException;
\r
64 import compbio.metadata.RunnerConfig;
\r
65 import compbio.runner.RunnerUtil;
\r
66 import compbio.util.FileWatcher;
\r
67 import compbio.util.SysPrefs;
\r
69 public class AAConTester {
\r
71 public static final String CURRENT_DIRECTORY = SysPrefs.getCurrentDirectory() + File.separator;
\r
72 public static String test_outfile = "TO1381.aacon.out";
\r
73 public static String test_alignment_input = CURRENT_DIRECTORY + "testsrc" + File.separator + "testdata" + File.separator + "TO1381.fasta.aln";
\r
74 private AACon aacon;
\r
76 @BeforeMethod(alwaysRun = true)
\r
78 aacon = new AACon();
\r
79 aacon.setInput(test_alignment_input).setOutput(test_outfile);
\r
82 @Test(groups = {AllTestSuit.test_group_runner})
\r
83 public void RunOnCluster() {
\r
84 assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");
\r
86 ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Cluster);
\r
87 JobRunner runner = JobRunner.getInstance(confAAcon);
\r
89 assertNotNull(runner, "Runner is NULL");
\r
90 runner.executeJob();
\r
91 JobStatus status = runner.getJobStatus();
\r
92 assertTrue(status == JobStatus.PENDING || status == JobStatus.RUNNING, "Status of the process is wrong!");
\r
93 JobInfo info = runner.getJobInfo();
\r
94 assertNotNull(info, "JobInfo is null");
\r
95 StatisticManager sm = new StatisticManager(info);
\r
96 assertNotNull(sm, "Statictic manager is null");
\r
98 String exits = sm.getExitStatus();
\r
99 assertNotNull("Exit status is null", exits);
\r
100 // cut 4 trailing zeros from the number
\r
101 int exitsInt = ClusterEngineUtil.CLUSTER_STAT_IN_SEC.parse(exits).intValue();
\r
102 assertEquals(0, exitsInt);
\r
103 System.out.println(sm.getAllStats());
\r
105 } catch (ParseException e) {
\r
106 e.printStackTrace();
\r
107 fail("Parse Exception: " + e.getMessage());
\r
109 assertTrue(sm.hasExited());
\r
110 assertFalse(sm.wasAborted());
\r
111 assertFalse(sm.hasDump());
\r
112 assertFalse(sm.hasSignaled());
\r
113 } catch (JobSubmissionException e) {
\r
114 e.printStackTrace();
\r
115 fail("DrmaaException caught:" + e.getMessage());
\r
116 } catch (JobExecutionException e) {
\r
117 e.printStackTrace();
\r
118 fail("DrmaaException caught:" + e.getMessage());
\r
119 } catch (DrmaaException e) {
\r
120 e.printStackTrace();
\r
121 fail("DrmaaException caught:" + e.getMessage());
\r
126 * This test fails from time to time depending on the cluster load or some
\r
127 * other factors. Any client code has to adjust for this issue
\r
129 @Test(groups = {AllTestSuit.test_group_runner, AllTestSuit.test_group_cluster})
\r
130 public void RunOnClusterAsync() {
\r
131 assertFalse(SysPrefs.isWindows, "Cluster execution can only be in unix environment");
\r
133 ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Cluster);
\r
134 AsyncExecutor aengine = Configurator.getAsyncEngine(confAAcon);
\r
135 String jobId = aengine.submitJob(confAAcon);
\r
136 assertNotNull(jobId, "Runner is NULL");
\r
137 // let drmaa to start
\r
139 JobStatus status = aengine.getJobStatus(jobId);
\r
140 while (status != JobStatus.FINISHED) {
\r
141 System.out.println("Job Status: " + status);
\r
142 Thread.sleep(1000);
\r
143 status = aengine.getJobStatus(jobId);
\r
144 ConfiguredExecutable<AACon> result = (ConfiguredExecutable<AACon>) aengine.getResults(jobId);
\r
145 assertNotNull(result);
\r
146 System.out.println("RES:" + result);
\r
147 // Some times the job could be removed from the cluster accounting
\r
148 // before it has been reported to finish. Make sure to stop waiting in such case
\r
149 if (status == JobStatus.UNDEFINED) {
\r
153 } catch (JobSubmissionException e) {
\r
154 e.printStackTrace();
\r
155 fail("DrmaaException caught:" + e.getMessage());
\r
156 } catch (InterruptedException e) {
\r
157 e.printStackTrace();
\r
158 fail(e.getMessage());
\r
159 } catch (ResultNotAvailableException e) {
\r
160 e.printStackTrace();
\r
161 fail(e.getMessage());
\r
165 @Test(groups = {AllTestSuit.test_group_runner})
\r
166 public void RunLocally() {
\r
168 ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);
\r
170 // For local execution use relative
\r
171 LocalRunner lr = new LocalRunner(confAAcon);
\r
173 ConfiguredExecutable<?> al1 = lr.waitForResult();
\r
174 assertNotNull(al1.getResults());
\r
175 ScoreManager annotations = confAAcon.getResults();
\r
176 assertNotNull(annotations);
\r
177 assertEquals(annotations.asSet().size(), 18);
\r
178 assertEquals(al1.getResults(), annotations);
\r
179 } catch (JobSubmissionException e) {
\r
180 e.printStackTrace();
\r
181 fail(e.getLocalizedMessage());
\r
182 } catch (ResultNotAvailableException e) {
\r
183 e.printStackTrace();
\r
184 fail(e.getLocalizedMessage());
\r
185 } catch (JobExecutionException e) {
\r
186 e.printStackTrace();
\r
187 fail(e.getLocalizedMessage());
\r
191 @Test(groups = {AllTestSuit.test_group_runner})
\r
192 public void RunLocallyWithPreset() {
\r
194 PresetManager<AACon> aaconPresets = RunnerUtil.getPresets(AACon.class);
\r
195 assert aaconPresets != null;
\r
196 ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);
\r
197 Preset<AACon> quick = aaconPresets.getPresetByName("Quick conservation");
\r
198 confAAcon.addParameters(quick.getOptions());
\r
199 // For local execution use relative
\r
200 LocalRunner lr = new LocalRunner(confAAcon);
\r
202 ConfiguredExecutable<?> al1 = lr.waitForResult();
\r
203 assertNotNull(al1.getResults());
\r
204 ScoreManager annotations = confAAcon.getResults();
\r
205 assertNotNull(annotations);
\r
206 assertEquals(annotations.asSet().size(), 13);
\r
207 assertEquals(al1.getResults(), annotations);
\r
208 } catch (JobSubmissionException e) {
\r
209 e.printStackTrace();
\r
210 fail(e.getLocalizedMessage());
\r
211 } catch (ResultNotAvailableException e) {
\r
212 e.printStackTrace();
\r
213 fail(e.getLocalizedMessage());
\r
214 } catch (JobExecutionException e) {
\r
215 e.printStackTrace();
\r
216 fail(e.getLocalizedMessage());
\r
220 @Test(groups = {AllTestSuit.test_group_runner})
\r
221 public void RunLocallyOnTwoCpu() {
\r
223 aacon = new AACon();
\r
224 aacon.setInput(test_alignment_input).setOutput(test_outfile);
\r
227 ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);
\r
228 confAAcon.addParameters(Arrays.asList("-m=KABAT,JORES"));
\r
229 confAAcon.addParameters(Arrays.asList("-m=TAYLOR_GAPS"));
\r
230 // For local execution use relative
\r
231 LocalRunner lr = new LocalRunner(confAAcon);
\r
234 ConfiguredExecutable<?> al1 = lr.waitForResult();
\r
235 assertNotNull(al1.getResults());
\r
236 ScoreManager annotations = confAAcon.getResults();
\r
237 assertNotNull(annotations);
\r
238 assertEquals(annotations.asSet().size(), 3);
\r
239 assertEquals(al1.getResults(), annotations);
\r
240 } catch (JobSubmissionException e) {
\r
241 e.printStackTrace();
\r
242 fail(e.getLocalizedMessage());
\r
243 } catch (ResultNotAvailableException e) {
\r
244 e.printStackTrace();
\r
245 fail(e.getLocalizedMessage());
\r
246 } catch (JobExecutionException e) {
\r
247 e.printStackTrace();
\r
248 fail(e.getLocalizedMessage());
\r
252 @Test(groups = {AllTestSuit.test_group_runner})
\r
253 public void readStatistics() {
\r
255 ConfiguredExecutable<AACon> confAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);
\r
256 // For local execution use relative
\r
258 AsyncExecutor sexec = Configurator.getAsyncEngine(confAAcon);
\r
259 String jobId = sexec.submitJob(confAAcon);
\r
260 String file = confAAcon.getWorkDirectory() + File.separator + AACon.getStatFile();
\r
261 FilePuller fw = FilePuller.newFilePuller(file, FileWatcher.MIN_CHUNK_SIZE_BYTES);
\r
265 JobStatus status = sexec.getJobStatus(jobId);
\r
267 if (fw.hasMoreData()) {
\r
268 ChunkHolder ch = fw.pull(position);
\r
269 String chunk = ch.getChunk();
\r
270 position = ch.getNextPosition();
\r
273 // Make sure the loop is terminated if the job fails
\r
274 if ((status == JobStatus.UNDEFINED || status == JobStatus.FAILED)) {
\r
275 fail("job failed!");
\r
279 status = sexec.getJobStatus(jobId);
\r
280 } while (status != JobStatus.FINISHED || fw.hasMoreData());
\r
281 assertTrue(count >= 1);
\r
282 ConfiguredExecutable<?> al = sexec.getResults(jobId);
\r
283 assertNotNull(al.getResults());
\r
284 } catch (JobSubmissionException e) {
\r
285 e.printStackTrace();
\r
286 fail(e.getMessage());
\r
287 } catch (ResultNotAvailableException e) {
\r
288 e.printStackTrace();
\r
289 fail(e.getMessage());
\r
290 } catch (IOException e) {
\r
291 e.printStackTrace();
\r
292 fail(e.getMessage());
\r
293 } catch (InterruptedException e) {
\r
294 e.printStackTrace();
\r
295 fail(e.getMessage());
\r
299 @Test(groups = {AllTestSuit.test_group_runner})
\r
300 public void Persistance() {
\r
302 AACon aacon = new AACon();
\r
303 aacon.setError("errrr.txt");
\r
304 aacon.setInput(test_alignment_input);
\r
305 aacon.setOutput("outtt.txt");
\r
306 assertEquals(aacon.getInput(), test_alignment_input);
\r
307 assertEquals(aacon.getError(), "errrr.txt");
\r
308 assertEquals(aacon.getOutput(), "outtt.txt");
\r
309 ConfiguredExecutable<AACon> cAAcon = Configurator.configureExecutable(aacon, Executable.ExecProvider.Local);
\r
311 SyncExecutor sexec = Configurator.getSyncEngine(cAAcon);
\r
312 sexec.executeJob();
\r
313 ConfiguredExecutable<?> al = sexec.waitForResult();
\r
314 assertNotNull(al.getResults());
\r
316 // Save run configuration
\r
317 assertTrue(cAAcon.saveRunConfiguration());
\r
319 // See if loaded configuration is the same as saved
\r
320 RunConfiguration loadedRun = RunConfiguration
\r
321 .load(new FileInputStream(new File(cAAcon
\r
322 .getWorkDirectory(), RunConfiguration.rconfigFile)));
\r
323 assertEquals(((ConfExecutable<AACon>) cAAcon).getRunConfiguration(),loadedRun);
\r
325 // Load run configuration as ConfExecutable
\r
326 ConfiguredExecutable<AACon> resurrectedCAAcon = (ConfiguredExecutable<AACon>) cAAcon
\r
327 .loadRunConfiguration(new FileInputStream(new File(cAAcon
\r
328 .getWorkDirectory(), RunConfiguration.rconfigFile)));
\r
329 assertNotNull(resurrectedCAAcon);
\r
330 assertEquals(resurrectedCAAcon.getExecutable().getInput(), test_alignment_input);
\r
331 assertEquals(resurrectedCAAcon.getExecutable().getError(), "errrr.txt");
\r
332 assertEquals(resurrectedCAAcon.getExecutable().getOutput(), "outtt.txt");
\r
334 // See in details whether executables are the same
\r
335 assertEquals(resurrectedCAAcon.getExecutable(), aacon);
\r
337 ConfiguredExecutable<AACon> resAAcon = Configurator.configureExecutable(resurrectedCAAcon.getExecutable(),
\r
338 Executable.ExecProvider.Local);
\r
340 sexec = Configurator.getSyncEngine(resAAcon, Executable.ExecProvider.Local);
\r
341 sexec.executeJob();
\r
342 al = sexec.waitForResult();
\r
344 } catch (JobSubmissionException e) {
\r
345 e.printStackTrace();
\r
346 fail(e.getMessage());
\r
347 } catch (JobExecutionException e) {
\r
348 e.printStackTrace();
\r
349 fail(e.getMessage());
\r
350 } catch (FileNotFoundException e) {
\r
351 e.printStackTrace();
\r
352 fail(e.getMessage());
\r
353 } catch (IOException e) {
\r
354 e.printStackTrace();
\r
355 fail(e.getMessage());
\r
356 } catch (ResultNotAvailableException e) {
\r
357 e.printStackTrace();
\r
358 fail(e.getMessage());
\r
362 @Test(groups = {AllTestSuit.test_group_runner})
\r
363 public void ConfigurationLoading() {
\r
365 RunnerConfig<AACon> aaconConfig = ConfExecutable.getRunnerOptions(AACon.class);
\r
366 assertNotNull(aaconConfig);
\r
367 assertTrue(aaconConfig.getArguments().size() > 0);
\r
369 PresetManager<AACon> aaconPresets = ConfExecutable.getRunnerPresets(AACon.class);
\r
370 assertNotNull(aaconPresets);
\r
372 LimitsManager<AACon> aaconLimits = ConfExecutable.getRunnerLimits(AACon.class);
\r
373 assertNotNull(aaconLimits);
\r
374 assertTrue(aaconLimits.getLimits().size() > 0);
\r
375 aaconLimits.validate(aaconPresets);
\r
376 } catch (FileNotFoundException e) {
\r
377 e.printStackTrace();
\r
378 fail(e.getLocalizedMessage());
\r
379 } catch (IOException e) {
\r
380 e.printStackTrace();
\r
381 fail(e.getLocalizedMessage());
\r
382 } catch (ValidationException e) {
\r
383 e.printStackTrace();
\r
384 fail(e.getLocalizedMessage());
\r