Next version of JABA
[jabaws.git] / testsrc / compbio / engine / FilePullerTester.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;\r
20 \r
21 import static org.testng.Assert.assertEquals;\r
22 import static org.testng.Assert.assertNotNull;\r
23 import static org.testng.Assert.fail;\r
24 \r
25 import java.io.File;\r
26 import java.io.IOException;\r
27 import java.util.concurrent.TimeUnit;\r
28 \r
29 import org.testng.annotations.BeforeTest;\r
30 import org.testng.annotations.Test;\r
31 \r
32 import compbio.engine.client.ConfiguredExecutable;\r
33 import compbio.engine.client.Executable;\r
34 import compbio.engine.local.AsyncLocalRunner;\r
35 import compbio.metadata.AllTestSuit;\r
36 import compbio.metadata.ChunkHolder;\r
37 import compbio.metadata.JobSubmissionException;\r
38 import compbio.metadata.ResultNotAvailableException;\r
39 import compbio.runner.msa.ClustalW;\r
40 import compbio.runner.msa.Muscle;\r
41 \r
42 public class FilePullerTester {\r
43 \r
44         public static String test_input = AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
45                         + "TO1381.fasta";\r
46 \r
47         String jobId;\r
48         String jobId2;\r
49         String jobId3;\r
50 \r
51         @BeforeTest(alwaysRun = true)\r
52         public void init() {\r
53                 ClustalW clustal = new ClustalW();\r
54                 clustal.setInput(test_input);\r
55                 Muscle ms = new Muscle().setInput(test_input);\r
56                 Muscle ms2 = new Muscle().setInput(test_input);\r
57 \r
58                 try {\r
59                         // For local execution use relavive\r
60                         ConfiguredExecutable<ClustalW> confClustal = Configurator\r
61                                         .configureExecutable(clustal, Executable.ExecProvider.Local);\r
62                         ConfiguredExecutable<Muscle> confms = Configurator\r
63                                         .configureExecutable(ms, Executable.ExecProvider.Local);\r
64 \r
65                         ConfiguredExecutable<Muscle> confms2 = Configurator\r
66                                         .configureExecutable(ms2, Executable.ExecProvider.Local);\r
67 \r
68                         AsyncLocalRunner as = new AsyncLocalRunner();\r
69                         jobId = as.submitJob(confClustal);\r
70                         jobId2 = as.submitJob(confms);\r
71                         jobId3 = as.submitJob(confms2);\r
72 \r
73                         ConfiguredExecutable<?> al = as.getResults(jobId);\r
74                         ConfiguredExecutable<?> al2 = as.getResults(jobId2);\r
75                         ConfiguredExecutable<?> al3 = as.getResults(jobId3);\r
76                         assertNotNull(al);\r
77                         assertNotNull(al2);\r
78                         assertNotNull(al3);\r
79                 } catch (JobSubmissionException e) {\r
80                         e.printStackTrace();\r
81                         fail(e.getLocalizedMessage());\r
82                 } catch (ResultNotAvailableException e) {\r
83                         e.printStackTrace();\r
84                         fail(e.getLocalizedMessage());\r
85                 }\r
86         }\r
87 \r
88         @Test(groups = { AllTestSuit.test_group_engine })\r
89         public void testPull() {\r
90                 assertNotNull(jobId, "init() method failed!");\r
91                 String workDir = Configurator.getWorkDirectory(jobId);\r
92                 String statFile = workDir + File.separator + ClustalW.getStatFile();\r
93 \r
94                 ChunkHolder ch = ProgressGetter.pull(statFile, 0);\r
95                 while (ch == null) {\r
96                         ch = ProgressGetter.pull(statFile, 0);\r
97                 }\r
98                 String chunk = "";\r
99                 long pos = 0;\r
100                 do {\r
101                         chunk = ch.getChunk();\r
102                         assertNotNull(chunk);\r
103                         pos = ch.getNextPosition();\r
104                         ch = ProgressGetter.pull(statFile, pos);\r
105                 } while (chunk.length() > 0);\r
106 \r
107                 // All consequent pulls just return empty chunk and same position =\r
108                 // file.length\r
109                 ch = ProgressGetter.pull(statFile, pos);\r
110                 assertNotNull(ch);\r
111                 assertEquals(ch.getChunk().length(), 0);\r
112                 // Output file size depends on the operation system fs!\r
113                 // assertEquals(ch.getNextPosition(), 668);\r
114 \r
115                 ch = ProgressGetter.pull(statFile, pos);\r
116                 assertNotNull(ch);\r
117                 assertEquals(ch.getChunk().length(), 0);\r
118                 // Output file size depends on the operation system and fs!\r
119                 // assertEquals(ch.getNextPosition(), 668);\r
120 \r
121         }\r
122 \r
123         @Test(groups = { AllTestSuit.test_group_engine })\r
124         public void testGetDelay() {\r
125                 FilePuller fp = FilePuller.newFilePuller(Configurator\r
126                                 .getWorkDirectory(jobId)\r
127                                 + File.separator + "stat.log", 256);\r
128                 // default delay is 5 minutes\r
129                 assertEquals(fp.getDelayValue(TimeUnit.SECONDS), 5 * 60);\r
130                 long d = 1000 * 1000 * 1000L * 60; // 1m in nanoseconds\r
131                 fp.setDelay(d, TimeUnit.NANOSECONDS);\r
132                 assertEquals(fp.getDelayValue(TimeUnit.NANOSECONDS), d);\r
133                 assertEquals(fp.getDelayValue(TimeUnit.SECONDS), 60);\r
134                 assertEquals(fp.getDelayValue(TimeUnit.MINUTES), 1);\r
135         }\r
136 \r
137         @Test(groups = { AllTestSuit.test_group_engine }, dependsOnMethods = { "testPull" })\r
138         public void testCache() {\r
139                 assertNotNull(jobId, "init() method failed!");\r
140                 assertNotNull(jobId2, "init() method failed!");\r
141                 assertEquals(PulledFileCache.getSize(), 1); // One is from previous test\r
142 \r
143                 String statFile = Configurator.getWorkDirectory(jobId) + File.separator\r
144                                 + ClustalW.getStatFile();\r
145                 String statFile2 = Configurator.getWorkDirectory(jobId2)\r
146                                 + File.separator + Muscle.getStatFile();\r
147                 String statFile3 = Configurator.getWorkDirectory(jobId3)\r
148                                 + File.separator + Muscle.getStatFile();\r
149 \r
150                 ChunkHolder ch = ProgressGetter.pull(statFile, 0);\r
151                 assertEquals(PulledFileCache.getSize(), 1); // Still one as job has been\r
152                 // retrieved from cache\r
153                 ChunkHolder ch2 = ProgressGetter\r
154                                 .pull(statFile2, 0, 5, TimeUnit.SECONDS); // 5\r
155                 // second delay\r
156                 assertEquals(PulledFileCache.getSize(), 2); // One is from previous test\r
157 \r
158                 // Pull the first job completely\r
159                 while (ch == null) {\r
160                         ch = ProgressGetter.pull(statFile, 0);\r
161                 }\r
162                 String chunk = "";\r
163                 long pos = 0;\r
164                 do {\r
165                         chunk = ch.getChunk();\r
166                         assertNotNull(chunk);\r
167                         pos = ch.getNextPosition();\r
168                         ch = ProgressGetter.pull(statFile, pos);\r
169                 } while (chunk.length() > 0);\r
170 \r
171                 try {\r
172                         Thread.sleep(1000 * 6);\r
173                 } catch (InterruptedException e) {\r
174                         e.printStackTrace();\r
175                         fail(e.getMessage());\r
176                 }\r
177                 // Elements are removed on put operation only\r
178                 assertEquals(PulledFileCache.getSize(), 2); // One is from previous test\r
179                 ChunkHolder ch3 = ProgressGetter.pull(statFile3, 0);\r
180                 // Now old element was removed, but new added, thus size remains\r
181                 // constant\r
182                 assertEquals(PulledFileCache.getSize(), 2); // One is from previous test\r
183 \r
184         }\r
185 \r
186         @Test\r
187         public void testGet() {\r
188 \r
189                 FilePuller pp = FilePuller\r
190                                 .newProgressPuller(AllTestSuit.TEST_DATA_PATH_ABSOLUTE\r
191                                                 + "percentProgress.txt");\r
192                 try {\r
193                         assertEquals(pp.getProgress(), 12);\r
194                         pp.disconnect();\r
195                 } catch (NumberFormatException e) {\r
196                         e.printStackTrace();\r
197                         fail(e.getMessage());\r
198                 } catch (IOException e) {\r
199                         e.printStackTrace();\r
200                         fail(e.getMessage());\r
201                 }\r
202         }\r
203 }\r