1 package org.ogf.saga.job.run;
2
3 import org.junit.Assert;
4 import org.junit.Ignore;
5 import org.junit.Test;
6 import org.ogf.saga.error.*;
7 import org.ogf.saga.job.Job;
8 import org.ogf.saga.job.JobDescription;
9 import org.ogf.saga.job.JobFactory;
10 import org.ogf.saga.job.JobService;
11 import org.ogf.saga.job.StartJob;
12 import org.ogf.saga.job.base.JobBaseTest;
13 import org.ogf.saga.task.*;
14
15 import java.util.List;
16
17
18
19
20
21
22
23
24
25
26
27
28 public abstract class OptionalTest extends JobBaseTest {
29
30 protected OptionalTest(String jobprotocol) throws Exception {
31 super(jobprotocol);
32 }
33
34
35
36
37 @Test
38 public void test_suspend_queued() throws Exception {
39
40
41 JobDescription desc = createLongJob();
42
43
44 Job job = runJob(desc);
45
46 if (! super.waitForSubState(job, MODEL+":RUNNING_QUEUED")) {
47 fail("Job did not enter RUNNING_QUEUED state within "+MAX_QUEUING_TIME+" seconds");
48 }
49
50 job.suspend();
51
52 if (! super.waitForSubState(job, MODEL+":SUSPENDED_ACTIVE")) {
53 fail("Job did not enter SUSPENDED_ACTIVE state within "+MAX_QUEUING_TIME+" seconds");
54 }
55 checkStatus(job.getState(), State.SUSPENDED);
56
57
58 job.resume();
59
60 if (! super.waitForSubState(job, MODEL+":RUNNING_ACTIVE")) {
61 fail("Job did not enter RUNNING_ACTIVE state within "+MAX_QUEUING_TIME+" seconds");
62 }
63 checkStatus(job.getState(), State.RUNNING);
64 }
65
66
67
68
69 @Test
70 public void test_suspend_running() throws Exception {
71
72
73 JobDescription desc = createLongJob();
74
75
76 Job job = runJob(desc);
77
78
79 if (! super.waitForSubState(job, MODEL+":RUNNING_ACTIVE")) {
80 Assert.fail("Job did not enter RUNNING_ACTIVE state within "+MAX_QUEUING_TIME+" seconds");
81 }
82
83
84 job.suspend();
85
86 if (! super.waitForSubState(job, MODEL+":SUSPENDED_ACTIVE")) {
87 fail("Job did not enter SUSPENDED_ACTIVE state within "+MAX_QUEUING_TIME+" seconds");
88 }
89 checkStatus(job.getState(), State.SUSPENDED);
90
91
92 job.resume();
93
94 if (! super.waitForSubState(job, MODEL+":RUNNING_ACTIVE")) {
95 fail("Job did not enter RUNNING_ACTIVE state within "+MAX_QUEUING_TIME+" seconds");
96 }
97 checkStatus(job.getState(), State.RUNNING);
98 }
99
100
101
102
103 @Test(expected=IncorrectStateException.class)
104 public void test_suspend_done() throws Exception {
105
106
107 JobDescription desc = createSimpleJob();
108
109
110 Job job = runJob(desc);
111
112
113 job.waitFor();
114
115
116 checkStatus(job.getState(), State.DONE);
117
118
119 job.suspend();
120 }
121
122
123
124
125 @Test(expected=IncorrectStateException.class)
126 public void test_resume_done() throws Exception {
127
128
129 JobDescription desc = createSimpleJob();
130
131
132 Job job = runJob(desc);
133
134
135 job.waitFor();
136
137
138 checkStatus(job.getState(), State.DONE);
139
140
141 job.resume();
142 }
143
144
145
146
147 @Test
148 public void test_listJob() throws Exception {
149
150
151 JobDescription desc = createLongJob();
152
153
154 Job job = runJob(desc);
155
156
157 try {
158 JobService service = JobFactory.createJobService(m_session, m_jobservice);
159 List<String> jobList = service.list();
160
161 boolean jobIsInList = false;
162 if(jobList.contains(job.getAttribute(Job.JOBID)))
163 jobIsInList = true;
164
165 Assert.assertEquals(
166 true,
167 jobIsInList);
168 }
169 finally {
170 job.waitFor(Float.valueOf(FINALY_TIMEOUT));
171 }
172 }
173
174
175
176
177 @Test
178 public void test_simultaneousLongJob() throws Exception {
179
180 int numberOfJobs = Integer.parseInt(SIMULTANEOUS_JOB_NUMBER);
181
182
183 StartJob[] newJob = new StartJob[numberOfJobs];
184
185
186 JobService m_service = JobFactory.createJobService(m_session, m_jobservice);
187 for (int i = 0; i < numberOfJobs; i++) {
188 newJob[i] = new StartJob(m_service, i, true);
189 newJob[i].start();
190 }
191
192 for (int i = 0; i < numberOfJobs; i++) {
193 newJob[i].join();
194 }
195
196
197 int numberOfFailed = 0;
198 for (int i = 0; i < numberOfJobs; i++) {
199 if(newJob[i].getException() != null) {
200 numberOfFailed ++;
201 }
202 }
203 if(numberOfFailed > 1)
204 throw new NoSuccessException(numberOfFailed + " jobs of "+numberOfJobs+" are failed.");
205 if(numberOfFailed > 0)
206 throw new NoSuccessException(numberOfFailed + " job of "+numberOfJobs+" is failed.");
207
208 }
209
210
211
212
213 @Test
214 public void test_simultaneousShortJob() throws Exception {
215
216 int numberOfJobs = Integer.parseInt(SIMULTANEOUS_JOB_NUMBER);
217
218
219 StartJob[] newJob = new StartJob[numberOfJobs];
220
221
222 JobService m_service = JobFactory.createJobService(m_session, m_jobservice);
223 for (int i = 0; i < numberOfJobs; i++) {
224 newJob[i] = new StartJob(m_service, i, false);
225 newJob[i].start();
226 }
227
228 for (int i = 0; i < numberOfJobs; i++) {
229 newJob[i].join();
230 }
231
232
233 int numberOfFailed = 0;
234 SagaException lastException = null;
235 for (int i = 0; i < numberOfJobs; i++) {
236 if(newJob[i].getException() != null) {
237 numberOfFailed ++;
238 lastException = newJob[i].getException();
239 }
240 }
241 if(numberOfFailed > 0) {
242 throw new NoSuccessException("Failed job(s): "+numberOfFailed+"/"+numberOfJobs, lastException);
243 }
244
245 Assert.assertEquals(
246 0,
247 numberOfFailed);
248 }
249
250
251
252
253 @Test
254 public void test_TaskContainer_ShortJob() throws Exception {
255
256 int numberOfJobs = Integer.parseInt(SIMULTANEOUS_JOB_NUMBER);
257
258 TaskContainer taskContainer = TaskFactory.createTaskContainer();
259
260
261 JobService m_service = JobFactory.createJobService(m_session, m_jobservice);
262 Job[] jobs = new Job[numberOfJobs];
263 for (int index = 0; index < numberOfJobs; index++) {
264
265 JobDescription desc = JobFactory.createJobDescription();
266 desc.setAttribute(JobDescription.EXECUTABLE, "/bin/date");
267 desc.setAttribute(JobDescription.OUTPUT, index+"-stdout.txt");
268 desc.setAttribute(JobDescription.ERROR, index+"-stderr.txt");
269
270 jobs[index] = m_service.createJob(desc);
271 taskContainer.add(jobs[index]);
272 }
273
274
275 taskContainer.run();
276
277
278 taskContainer.waitFor(WaitMode.ALL);
279
280
281 int numberOfFailed = 0;
282 for (int index = 0; index < numberOfJobs; index++) {
283 if(jobs[index].getState().getValue() == State.FAILED.getValue()) {
284 numberOfFailed ++;
285 }
286 }
287
288 if(numberOfFailed > 1)
289 throw new NoSuccessException(numberOfFailed + " jobs of "+numberOfJobs+" are failed.");
290 if(numberOfFailed > 0)
291 throw new NoSuccessException(numberOfFailed + " job of "+numberOfJobs+" is failed.");
292
293 Assert.assertEquals(
294 0,
295 numberOfFailed);
296 }
297 }