13dfed084e819bf9d2c1d8d1796ec6b8ec2f488e
[proteocache.git] / testsrc / compbio / engine / local / LocalRunnerTester.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.engine.local;\r
20 \r
21 import static org.testng.Assert.assertFalse;\r
22 import static org.testng.Assert.assertTrue;\r
23 import static org.testng.AssertJUnit.assertNotSame;\r
24 import static org.testng.AssertJUnit.assertNull;\r
25 import static org.testng.AssertJUnit.fail;\r
26 \r
27 import java.util.concurrent.CancellationException;\r
28 \r
29 import org.testng.annotations.Test;\r
30 \r
31 import compbio.engine.Configurator;\r
32 import compbio.engine.client.ConfiguredExecutable;\r
33 import compbio.engine.client.Executable;\r
34 import compbio.engine.client.EngineUtil;\r
35 import compbio.metadata.AllTestSuit;\r
36 import compbio.metadata.JobExecutionException;\r
37 import compbio.metadata.JobStatus;\r
38 import compbio.metadata.JobSubmissionException;\r
39 import compbio.metadata.ResultNotAvailableException;\r
40 import compbio.runner.msa.ClustalW;\r
41 \r
42 public class LocalRunnerTester {\r
43 \r
44         public static String cluster_test_outfile = "TO1381.clustal.cluster.out"; // "/homes/pvtroshin/TO1381.clustal.cluster.out\r
45         // go up 2 directories from workspace: workspace/clustengine\r
46         public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
47                         + "TO1381.fasta";\r
48         public static String test_outfile = "TO1381.clustal.out";\r
49 \r
50         @Test(expectedExceptions = CancellationException.class, groups = { AllTestSuit.test_group_engine })\r
51         public void testCancelLocally() {\r
52                 ClustalW clustal = new ClustalW();\r
53                 clustal.setInput(test_input).setOutput(test_outfile);\r
54 \r
55                 try {\r
56                         ConfiguredExecutable<ClustalW> confClust = Configurator\r
57                                         .configureExecutable(clustal);\r
58                         LocalRunner lr = new LocalRunner(confClust);\r
59 \r
60                         lr.executeJob();\r
61                         // Thread.sleep(10); //wait for 100ms\r
62                         assertNotSame("Job has finished already. Too late to test cancel!",\r
63                                         JobStatus.FINISHED, lr.getJobStatus());\r
64                         lr.cancelJob();\r
65                         // This call causes CancellationException to be thrown\r
66                         Executable<?> clustalr = lr.waitForResult();\r
67                         assertTrue(EngineUtil.isMarked(confClust.getWorkDirectory(),\r
68                                         JobStatus.CANCELLED));\r
69                         assertTrue(EngineUtil.isMarked(confClust.getWorkDirectory(),\r
70                                         JobStatus.STARTED));\r
71                         assertTrue(EngineUtil.isMarked(confClust.getWorkDirectory(),\r
72                                         JobStatus.COLLECTED));\r
73                         assertTrue(EngineUtil.isMarked(confClust.getWorkDirectory(),\r
74                                         JobStatus.FINISHED));\r
75                         assertFalse(EngineUtil.isMarked(confClust.getWorkDirectory(),\r
76                                         JobStatus.SUBMITTED));\r
77                 } catch (JobSubmissionException e) {\r
78                         e.printStackTrace();\r
79                         fail(e.getLocalizedMessage());\r
80                 } catch (JobExecutionException e) {\r
81                         e.printStackTrace();\r
82                         fail(e.getLocalizedMessage());\r
83                 }\r
84         }\r
85 \r
86         @Test(expectedExceptions = { CancellationException.class,\r
87                         JobExecutionException.class }, groups = { AllTestSuit.test_group_engine })\r
88         public void testMultipleCancelLocally() throws JobExecutionException {\r
89                 ClustalW clustal = new ClustalW();\r
90                 ClustalW clustal2 = new ClustalW();\r
91                 clustal.setInput(test_input).setOutput(test_outfile);\r
92                 clustal2.setInput(test_input).setOutput(test_outfile);\r
93                 try {\r
94                         ConfiguredExecutable<ClustalW> confClust = Configurator\r
95                                         .configureExecutable(clustal);\r
96                         ConfiguredExecutable<ClustalW> confClust2 = Configurator\r
97                                         .configureExecutable(clustal2);\r
98 \r
99                         LocalRunner lr = new LocalRunner(confClust);\r
100                         LocalRunner lr2 = new LocalRunner(confClust2);\r
101 \r
102                         lr.executeJob();\r
103                         lr2.executeJob();\r
104 \r
105                         // Thread.sleep(10); //wait for 100ms\r
106                         assertNotSame("Job has finished already. Too late to test cancel!",\r
107                                         JobStatus.FINISHED, lr.getJobStatus());\r
108                         lr.cancelJob();\r
109                         // Thread.sleep(10);\r
110                         assertNotSame("Job has finished already. Too late to test cancel!",\r
111                                         JobStatus.FINISHED, lr2.getJobStatus());\r
112                         lr2.cancelJob();\r
113                         // This call causes CancellationException to be thrown\r
114                         Executable<?> clustalr = lr.waitForResult();\r
115                         Executable<?> clustalr2 = lr2.waitForResult();\r
116 \r
117                 } catch (JobSubmissionException e) {\r
118                         e.printStackTrace();\r
119                         fail(e.getLocalizedMessage());\r
120                 }\r
121         }\r
122 \r
123         @Test(expectedExceptions = { CancellationException.class }, groups = { AllTestSuit.test_group_engine })\r
124         public void testCancelCompletedTaskLocally() throws JobExecutionException {\r
125                 ClustalW clustal = new ClustalW();\r
126                 clustal.setInput(test_input).setOutput(test_outfile);\r
127 \r
128                 try {\r
129                         ConfiguredExecutable<ClustalW> confClust = Configurator\r
130                                         .configureExecutable(clustal, Executable.ExecProvider.Local);\r
131                         LocalRunner lr = new LocalRunner(confClust);\r
132                         lr.executeJob();\r
133                         Thread.currentThread();\r
134                         Thread.sleep(30); // wait for 100ms\r
135                         assertNotSame("Job has not finished!", JobStatus.FINISHED, lr\r
136                                         .getJobStatus());\r
137                         lr.cancelJob();\r
138                         // This call causes ResultNotAvailableException to be thrown\r
139                         ConfiguredExecutable<?> clustalr = lr.waitForResult();\r
140                         assertNull(clustalr.getResults());\r
141                 } catch (JobSubmissionException e) {\r
142                         e.printStackTrace();\r
143                         fail(e.getLocalizedMessage());\r
144                 } catch (InterruptedException e) {\r
145                         e.printStackTrace();\r
146                         fail(e.getLocalizedMessage());\r
147                 } catch (ResultNotAvailableException e) {\r
148                         e.printStackTrace();\r
149                         fail(e.getLocalizedMessage());\r
150                 }\r
151         }\r
152 \r
153 }\r