View Javadoc

1   package org.ogf.saga.job.run;
2   
3   import org.junit.Assert;
4   import org.junit.Test;
5   import org.ogf.saga.error.IncorrectStateException;
6   import org.ogf.saga.error.NotImplementedException;
7   import org.ogf.saga.job.Job;
8   import org.ogf.saga.job.JobDescription;
9   import org.ogf.saga.job.base.JobBaseTest;
10  import org.ogf.saga.task.State;
11  
12  /* ***************************************************
13  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
14  * ***             http://cc.in2p3.fr/             ***
15  * ***************************************************
16  * File:   JobRunRequiredTest
17  * Author: Nicolas Demesy (nicolas.demesy@bt.com)
18  * Date:   30 janv. 2008
19  * ***************************************************
20  * Description: 
21  * This test suite is made to be sure that the plug-in 
22  * has the minimum functions                         */
23  /**
24   *
25   */
26  public abstract class RequiredTest extends JobBaseTest {
27      
28      protected RequiredTest(String jobprotocol) throws Exception {
29          super(jobprotocol);
30      }
31  
32      /*
33       * Runs long job and expects done status
34       */
35      @Test
36      public void test_run_long() throws Exception {
37          
38      	// prepare
39      	JobDescription desc = createLongJob();
40      	
41          // submit
42          Job job = runJob(desc);
43  
44          // wait for the END
45          job.waitFor();
46  
47          // check job status
48          Assert.assertEquals(
49                  State.DONE,
50                  job.getState());
51      }
52  
53  	/*
54       * Runs simple job and expects failed status
55       */
56      @Test
57      public void test_run_error() throws Exception {
58          
59      	// prepare
60      	JobDescription desc = createErrorJob();
61      	
62          // submit
63          Job job = runJob(desc);
64          
65          // wait for the end
66          job.waitFor();
67          
68          // check job status
69          Assert.assertEquals(
70                  State.FAILED,
71                  job.getState());
72      }
73  	
74      /*
75       * Runs a long job, waits for running state and cancels it
76       */
77      @Test
78      public void test_cancel_running() throws Exception {
79          
80      	// prepare
81      	JobDescription desc = createLongJob();
82      	
83          // submit
84          Job job = runJob(desc);
85          
86          // wait for RUNNING Jsaga substate
87          if (! super.waitForSubState(job, MODEL+":RUNNING_ACTIVE")) {
88          	job.waitFor(Float.valueOf(MAX_QUEUING_TIME));
89              Assert.fail("Job did not enter RUNNING_ACTIVE state within "+MAX_QUEUING_TIME+" seconds");
90          }
91          
92          try {
93  	        // cancel job
94  	        job.cancel();
95  	        
96  	        // wait
97  	        job.waitFor(Float.valueOf(FINALY_TIMEOUT));
98  	        
99  	        // check job status
100 	        Assert.assertEquals(
101 	                State.CANCELED,
102 	                job.getState());
103         }
104         catch (NotImplementedException notImplemented) {
105         	Assert.fail("WARNING : CANCEL not implemented in plugin");
106         }
107         finally {
108         	job.waitFor(Float.valueOf(FINALY_TIMEOUT));
109         }
110     }
111     
112     /*
113      * Create a new job cancels it and expects exception
114      */
115     @Test(expected=IncorrectStateException.class)
116     public void test_cancel_new() throws Exception {
117         
118     	// prepare
119     	JobDescription desc = createSimpleJob();
120     	
121         // submit
122         Job job = createJob(desc);
123 
124         // check job for NEW status
125         checkStatus(job.getState(), State.NEW);
126 
127         // cancel job
128         job.cancel();
129         
130         // wait for 2 seconds because cancel is an asynchronous method
131         Thread.sleep(2000);
132         
133     }
134     
135     /*
136      * Runs a simple job, waits for done state, cancels it
137      * expect nothing happens and job stays DONE
138      */
139     @Test
140     public void test_cancel_done() throws Exception {
141         
142     	// prepare
143     	JobDescription desc = createSimpleJob();
144     	
145         // submit
146         Job job = runJob(desc);
147 
148         // wait the end
149         job.waitFor();
150 
151         // check job for DONE status
152         checkStatus(job.getState(), State.DONE);
153 
154         // cancel job
155         job.cancel();
156         
157         // wait 2 seconds because cancel is an asynchronous method
158         Thread.sleep(2000);
159         
160         // check job for DONE status
161         checkStatus(job.getState(), State.DONE);
162     }
163 }