View Javadoc

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  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
19  * ***             http://cc.in2p3.fr/             ***
20  * ***************************************************
21  * File:   OptionalTest
22  * Author: Lionel Schwarz (lionel.schwarz@in2p3.fr)
23  * Date:   5 NOV 2013
24  * ***************************************************
25  * Description: 
26  * This test suite is made to be test advanced functions
27  */
28  public abstract class OptionalTest extends JobBaseTest {
29      
30      protected OptionalTest(String jobprotocol) throws Exception {
31          super(jobprotocol);
32      }
33  
34      /**
35       * Runs a long job, waits for RUNNING_QUEUED state and suspends it
36       */
37      @Test
38      public void test_suspend_queued() throws Exception {
39          
40          // prepare
41          JobDescription desc = createLongJob();
42          
43          // submit
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          // resume job
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       * Runs a long job, waits for running state and suspends it
68       */
69      @Test
70      public void test_suspend_running() throws Exception {
71          
72          // prepare
73          JobDescription desc = createLongJob();
74          
75          // submit
76          Job job = runJob(desc);
77          
78          // wait for RUNNING state (deviation from SAGA specification)
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          // suspend job
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          // resume job
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      * Runs a long job, waits for running state and suspends it
102      */
103     @Test(expected=IncorrectStateException.class)
104     public void test_suspend_done() throws Exception {
105         
106         // prepare
107         JobDescription desc = createSimpleJob();
108         
109         // submit
110         Job job = runJob(desc);
111         
112         // wait for the END
113         job.waitFor();
114         
115         // check job for DONE status
116         checkStatus(job.getState(), State.DONE);
117         
118         // suspend job
119         job.suspend();
120     }
121     
122     /**
123      * Runs a long job, waits for done state and resumes it
124      */
125     @Test(expected=IncorrectStateException.class)
126     public void test_resume_done() throws Exception {
127         
128         // prepare
129         JobDescription desc = createSimpleJob();
130         
131         // submit
132         Job job = runJob(desc);
133         
134         // wait the end
135         job.waitFor();
136         
137         // check job for DONE status
138         checkStatus(job.getState(), State.DONE);
139         
140         // resume job
141         job.resume();
142     }
143     
144     /**
145      * Runs a long job, list available jobs and check if the running job is in the resource manager list
146      */
147     @Test
148     public void test_listJob() throws Exception {
149         
150         // prepare
151         JobDescription desc = createLongJob();
152         
153         // submit
154         Job job = runJob(desc);
155 
156         // get jobs
157         try {
158             JobService service = JobFactory.createJobService(m_session, m_jobservice);
159             List<String> jobList = service.list();
160             // test if the running job is in the job list
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      * Runs long jobs on the same time with one job service
176      */
177     @Test
178     public void test_simultaneousLongJob() throws Exception {
179         
180         int numberOfJobs = Integer.parseInt(SIMULTANEOUS_JOB_NUMBER);
181 
182         // jobs
183         StartJob[] newJob = new StartJob[numberOfJobs];
184         
185         // create and start jobs
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         // get job exception
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      * Runs short jobs on the same time with one job service
212      */
213     @Test
214     public void test_simultaneousShortJob() throws Exception {
215         
216         int numberOfJobs = Integer.parseInt(SIMULTANEOUS_JOB_NUMBER);
217 
218         // jobs
219         StartJob[] newJob = new StartJob[numberOfJobs];
220         
221         // create and start jobs
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         // get job exception
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      * Runs short jobs on the same time with one task container
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         // create and start jobs
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             // create description
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             // add job to task
270             jobs[index] = m_service.createJob(desc);
271             taskContainer.add(jobs[index]);
272         }
273     
274         // run
275         taskContainer.run();
276         
277         // wait the end 
278         taskContainer.waitFor(WaitMode.ALL);
279         
280         // get failed jobs
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 }