~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/jdrmaa/test/com/sun/grid/drmaa/SessionImplJobTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
/*
 
33
 * SessionImplJobTest.java
 
34
 * JUnit based test
 
35
 *
 
36
 * Created on November 15, 2004, 10:41 AM
 
37
 */
 
38
 
 
39
package com.sun.grid.drmaa;
 
40
 
 
41
import java.util.*;
 
42
import junit.framework.*;
 
43
import org.ggf.drmaa.*;
 
44
import com.sun.grid.Settings;
 
45
 
 
46
/**
 
47
 *
 
48
 * @author dan.templeton@sun.com
 
49
 */
 
50
public class SessionImplJobTest extends TestCase {
 
51
    private static final String SLEEPER;
 
52
    private Session session = null;
 
53
    
 
54
    static {
 
55
        SLEEPER = Settings.get(Settings.SCRIPTS_DIR) + "/suspendable_sleeper.sh";
 
56
    }
 
57
    
 
58
    public SessionImplJobTest(java.lang.String testName) {
 
59
        super(testName);
 
60
    }
 
61
    
 
62
    public static Test suite() {
 
63
        TestSuite suite = new TestSuite(SessionImplJobTest.class);
 
64
        return suite;
 
65
    }
 
66
    
 
67
    public void setUp() throws DrmaaException {
 
68
        session = SessionFactory.getFactory().getSession();
 
69
        session.init(null);
 
70
    }
 
71
    
 
72
    public void tearDown() throws DrmaaException {
 
73
        try {
 
74
            session.exit();
 
75
        } catch (NoActiveSessionException ex) {
 
76
            // Ignore
 
77
        }
 
78
        
 
79
        session = null;
 
80
    }
 
81
    
 
82
    /** Test of runJob method, of class com.sun.grid.drmaa.SessionImpl. */
 
83
    public void testBadJobTemplate() {
 
84
        System.out.println("testBadJobTemplate");
 
85
        
 
86
        JobTemplate jt = new BadJobTemplate();
 
87
        
 
88
        try {
 
89
            try {
 
90
                session.runJob(jt);
 
91
                fail("Allowed bad job template");
 
92
            } catch (InvalidJobTemplateException e) {
 
93
                /* Don't care */
 
94
            }
 
95
            
 
96
            try {
 
97
                session.runBulkJobs(jt, 1, 3, 1);
 
98
                fail("Allowed bad job template");
 
99
            } catch (InvalidJobTemplateException e) {
 
100
                /* Don't care */
 
101
            }
 
102
            
 
103
            try {
 
104
                session.deleteJobTemplate(jt);
 
105
                fail("Allowed bad job template");
 
106
            } catch (InvalidJobTemplateException e) {
 
107
                /* Don't care */
 
108
            }
 
109
        } catch (DrmaaException e) {
 
110
            fail("Exception while trying to run job: " + e.getMessage());
 
111
        }
 
112
    }
 
113
    
 
114
    /** Test of runJob method, of class com.sun.grid.drmaa.SessionImpl. */
 
115
    public void testBadRunJob() {
 
116
        System.out.println("testBadRunJob");
 
117
        
 
118
        try {
 
119
            session.runJob(null);
 
120
            fail("Allowed null job template");
 
121
        } catch (NullPointerException e) {
 
122
            /* Don't care */
 
123
        } catch (DrmaaException e) {
 
124
            fail("Exception while trying to run job: " + e.getMessage());
 
125
        }
 
126
    }
 
127
    
 
128
    public void testRunJob() {
 
129
        System.out.println("testRunJob");
 
130
        
 
131
        try {
 
132
            JobTemplate jt = this.createSleeperTemplate(5);
 
133
            
 
134
            String jobId = session.runJob(jt);
 
135
            
 
136
            assertNotNull(jobId);
 
137
            
 
138
            session.deleteJobTemplate(jt);
 
139
        } catch (DrmaaException e) {
 
140
            fail("Exception while trying to run a job: " + e.getMessage());
 
141
        }
 
142
    }
 
143
    
 
144
    /** Test of runBulkJobs method, of class com.sun.grid.drmaa.SessionImpl. */
 
145
    public void testBadRunBulkJobs() {
 
146
        System.out.println("testBadRunBulkJobs");
 
147
        
 
148
        try {
 
149
            JobTemplate jt = this.createSleeperTemplate(5);
 
150
            
 
151
            try {
 
152
                session.runBulkJobs(null, 1, 2, 1);
 
153
                fail("Allowed null job template");
 
154
            } catch (NullPointerException e) {
 
155
                /* Don't care */
 
156
            }
 
157
            
 
158
            try {
 
159
                session.runBulkJobs(jt, -1, 2, 1);
 
160
                fail("Allowed invalid start id");
 
161
            } catch (IllegalArgumentException e) {
 
162
                /* Don't care */
 
163
            }
 
164
            
 
165
            try {
 
166
                session.runBulkJobs(jt, 1, -2, 1);
 
167
                fail("Allowed invalid end id");
 
168
            } catch (IllegalArgumentException e) {
 
169
                /* Don't care */
 
170
            }
 
171
            
 
172
            try {
 
173
                session.runBulkJobs(jt, 1, 2, -1);
 
174
                fail("Allowed negative step when end > start");
 
175
            } catch (IllegalArgumentException e) {
 
176
                /* Don't care */
 
177
            }
 
178
            
 
179
            try {
 
180
                session.runBulkJobs(jt, 3, 2, 1);
 
181
                fail("Allowed end < start");
 
182
            } catch (IllegalArgumentException e) {
 
183
                /* Don't care */
 
184
            }
 
185
            
 
186
            session.deleteJobTemplate(jt);
 
187
        } catch (DrmaaException e) {
 
188
            fail("Exception while trying to run jobs: " + e.getMessage());
 
189
        }
 
190
    }
 
191
    
 
192
    public void testRunMonoBulkJobs() {
 
193
        System.out.println("testRunMonoBulkJobs");
 
194
        
 
195
        try {
 
196
            JobTemplate jt = this.createSleeperTemplate(5);
 
197
            
 
198
            List jobIds = session.runBulkJobs(jt, 1, 1, 1);
 
199
            
 
200
            assertNotNull(jobIds);
 
201
            assertEquals(1, jobIds.size());
 
202
            
 
203
            session.deleteJobTemplate(jt);
 
204
        } catch (DrmaaException e) {
 
205
            fail("Exception while trying to run jobs: " + e.getMessage());
 
206
        }
 
207
    }
 
208
    
 
209
    public void testRunDualBulkJobs() {
 
210
        System.out.println("testRunDualBulkJobs");
 
211
        
 
212
        try {
 
213
            JobTemplate jt = this.createSleeperTemplate(5);
 
214
            
 
215
            List jobIds = session.runBulkJobs(jt, 1, 2, 1);
 
216
            
 
217
            assertNotNull(jobIds);
 
218
            assertEquals(2, jobIds.size());
 
219
            
 
220
            session.deleteJobTemplate(jt);
 
221
        } catch (DrmaaException e) {
 
222
            fail("Exception while trying to run jobs: " + e.getMessage());
 
223
        }
 
224
    }
 
225
    
 
226
    public void testRunBulkJobs() {
 
227
        System.out.println("testRunBulkJobs");
 
228
        
 
229
        try {
 
230
            JobTemplate jt = this.createSleeperTemplate(5);
 
231
            
 
232
            List jobIds = session.runBulkJobs(jt, 2, 6, 2);
 
233
            
 
234
            assertNotNull(jobIds);
 
235
            assertEquals(3, jobIds.size());
 
236
            
 
237
            session.deleteJobTemplate(jt);
 
238
        } catch (DrmaaException e) {
 
239
            fail("Exception while trying to run jobs: " + e.getMessage());
 
240
        }
 
241
    }
 
242
    
 
243
    /** Test of wait method, of class com.sun.grid.drmaa.SessionImpl. */
 
244
    public void testBadWait() {
 
245
        System.out.println("testBadWait");
 
246
        
 
247
        try {
 
248
            JobTemplate jt = this.createSleeperTemplate(5);
 
249
            
 
250
            String jobId = session.runJob(jt);
 
251
            
 
252
            /* Test bad parameters */
 
253
            try {
 
254
                session.wait(null, 0L);
 
255
                fail("Allowed null job id");
 
256
            } catch (NullPointerException e) {
 
257
                /* Don't care */
 
258
            }
 
259
            
 
260
            try {
 
261
                session.wait("asdf", 0L);
 
262
                fail("Allowed invalid job id");
 
263
            } catch (IllegalArgumentException e) {
 
264
                /* Don't care */
 
265
            }
 
266
            
 
267
            try {
 
268
                session.wait(jobId, -3L);
 
269
                fail("Allowed negative timeout");
 
270
            } catch (IllegalArgumentException e) {
 
271
                /* Don't care */
 
272
            }
 
273
            
 
274
            session.deleteJobTemplate(jt);
 
275
        } catch (DrmaaException e) {
 
276
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
277
        }
 
278
    }
 
279
    
 
280
    public void testWaitForever() {
 
281
        System.out.println("testWaitForever");
 
282
        
 
283
        try {
 
284
            JobTemplate jt = this.createSleeperTemplate(5);
 
285
            
 
286
            String jobId = session.runJob(jt);
 
287
            
 
288
            /* Test wait forever */
 
289
            JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
290
            
 
291
            assertNotNull(info);
 
292
            assertEquals(jobId, info.getJobId());
 
293
            
 
294
            /* There's no reason that this job should exit prematurely. */
 
295
            assertTrue(info.hasExited());
 
296
            assertEquals(0, info.getExitStatus());
 
297
            
 
298
            /* Make sure that the previous wait worked. */
 
299
            try {
 
300
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
301
                fail("First wait didn't reap job exit info");
 
302
            } catch (InvalidJobException e) {
 
303
                /* Don't care */
 
304
            }
 
305
            
 
306
         /* Eventually I need to build a libtestjdrmaa.so with some utilities in
 
307
          * it, like sendSignal() and causeException(), so that I can test some
 
308
          * error cases. */
 
309
            
 
310
            session.deleteJobTemplate(jt);
 
311
        } catch (DrmaaException e) {
 
312
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
313
        }
 
314
    }
 
315
    
 
316
    public void testNoWait() {
 
317
        System.out.println("testNoWait");
 
318
        
 
319
        try {
 
320
            JobTemplate jt = this.createSleeperTemplate(5);
 
321
            
 
322
            /* Test no wait */
 
323
            String jobId = session.runJob(jt);
 
324
            long now = System.currentTimeMillis();
 
325
            JobInfo info = null;
 
326
            
 
327
            try {
 
328
                info = session.wait(jobId, session.TIMEOUT_NO_WAIT);
 
329
                fail("Waited for job to finish; ignored 0s timeout");
 
330
            } catch (ExitTimeoutException e) {
 
331
                /* Don't care */
 
332
            }
 
333
            
 
334
            long later = System.currentTimeMillis();
 
335
            
 
336
         /* I assume that 1 second is more than enough time to check the cache
 
337
          * and return. */
 
338
            assertTrue((later - now) < 1000L);
 
339
            
 
340
            /* Make sure that the previous wait didn't disrupt anything. */
 
341
            info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
342
            
 
343
            assertNotNull(info);
 
344
            assertEquals(jobId, info.getJobId());
 
345
            
 
346
            /* There's no reason that this job should exit prematurely. */
 
347
            assertTrue(info.hasExited());
 
348
            assertEquals(0, info.getExitStatus());
 
349
            
 
350
            /* Make sure that the previous wait worked. */
 
351
            try {
 
352
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
353
                fail("Second wait didn't reap job exit info");
 
354
            } catch (InvalidJobException e) {
 
355
                /* Don't care */
 
356
            }
 
357
            
 
358
            session.deleteJobTemplate(jt);
 
359
        } catch (DrmaaException e) {
 
360
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
361
        }
 
362
    }
 
363
    
 
364
    public void testWaitTimeout() {
 
365
        System.out.println("testWaitTimeout");
 
366
        
 
367
        try {
 
368
            JobTemplate jt = this.createSleeperTemplate(5);
 
369
            
 
370
            /* Test timed wait */
 
371
            String jobId = session.runJob(jt);
 
372
            long now = System.currentTimeMillis();
 
373
         /* I'm assuming that 3 seconds is not long enough for the job to be
 
374
          * scheduled and run. */
 
375
            JobInfo info = null;
 
376
            
 
377
            try {
 
378
                info = session.wait(jobId, 3L);
 
379
                fail("Waited for job to finish; ignored 3s timeout");
 
380
            } catch (ExitTimeoutException e) {
 
381
                /* Don't care */
 
382
            }
 
383
            
 
384
            long later = System.currentTimeMillis();
 
385
            
 
386
         /* I'm assuming that there's no more than 1 second overhead in making
 
387
          * the call. */
 
388
            assertTrue((later - now) < 4000L);
 
389
            
 
390
            /* Make sure that the previous wait didn't disrupt anything. */
 
391
            info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
392
            
 
393
            assertNotNull(info);
 
394
            assertEquals(jobId, info.getJobId());
 
395
            
 
396
            /* There's no reason that this job should exit prematurely. */
 
397
            assertTrue(info.hasExited());
 
398
            assertEquals(0, info.getExitStatus());
 
399
            
 
400
            /* Make sure that the previous wait worked. */
 
401
            try {
 
402
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
403
                fail("Second wait didn't reap job exit info");
 
404
            } catch (InvalidJobException e) {
 
405
                /* Don't care */
 
406
            }
 
407
            
 
408
            session.deleteJobTemplate(jt);
 
409
        } catch (DrmaaException e) {
 
410
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
411
        }
 
412
    }
 
413
    
 
414
    public void testWait() {
 
415
        System.out.println("testWait");
 
416
        
 
417
        try {
 
418
            JobTemplate jt = this.createSleeperTemplate(5);
 
419
            
 
420
            /* Test timed wait */
 
421
            String jobId = session.runJob(jt);
 
422
            JobInfo info = session.wait(jobId, 30L);
 
423
            
 
424
            assertNotNull(info);
 
425
            assertEquals(jobId, info.getJobId());
 
426
            
 
427
            /* There's no reason that this job should exit prematurely. */
 
428
            assertTrue(info.hasExited());
 
429
            assertEquals(0, info.getExitStatus());
 
430
            
 
431
            /* Make sure that the previous wait worked. */
 
432
            try {
 
433
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
434
                fail("First wait didn't reap job exit info");
 
435
            } catch (InvalidJobException e) {
 
436
                /* Don't care */
 
437
            }
 
438
            
 
439
            session.deleteJobTemplate(jt);
 
440
        } catch (DrmaaException e) {
 
441
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
442
        }
 
443
    }
 
444
    
 
445
    public void testWaitForeverAny() {
 
446
        System.out.println("testWaitForeverAny");
 
447
        
 
448
        try {
 
449
            JobTemplate jt = this.createSleeperTemplate(5);
 
450
            
 
451
            String jobId = session.runJob(jt);
 
452
            
 
453
            /* Test wait forever */
 
454
            JobInfo info = session.wait(session.JOB_IDS_SESSION_ANY, session.TIMEOUT_WAIT_FOREVER);
 
455
            
 
456
            assertNotNull(info);
 
457
            assertEquals(jobId, info.getJobId());
 
458
            
 
459
            /* There's no reason that this job should exit prematurely. */
 
460
            assertTrue(info.hasExited());
 
461
            assertEquals(0, info.getExitStatus());
 
462
            
 
463
            /* Make sure that the previous wait worked. */
 
464
            try {
 
465
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
466
                fail("First wait didn't reap job exit info");
 
467
            } catch (InvalidJobException e) {
 
468
                /* Don't care */
 
469
            }
 
470
            
 
471
         /* Eventually I need to build a libtestjdrmaa.so with some utilities in
 
472
          * it, like sendSignal() and causeException(), so that I can test some
 
473
          * error cases. */
 
474
            
 
475
            session.deleteJobTemplate(jt);
 
476
        } catch (DrmaaException e) {
 
477
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
478
        }
 
479
    }
 
480
    
 
481
    public void testNoWaitAny() {
 
482
        System.out.println("testNoWaitAny");
 
483
        
 
484
        try {
 
485
            JobTemplate jt = this.createSleeperTemplate(5);
 
486
            
 
487
            /* Test no wait */
 
488
            String jobId = session.runJob(jt);
 
489
            long now = System.currentTimeMillis();
 
490
            JobInfo info = null;
 
491
            
 
492
            try {
 
493
                info = session.wait(session.JOB_IDS_SESSION_ANY, session.TIMEOUT_NO_WAIT);
 
494
                fail("Waited for job to finish; ignored 0s timeout");
 
495
            } catch (ExitTimeoutException e) {
 
496
                /* Don't care */
 
497
            }
 
498
            
 
499
            long later = System.currentTimeMillis();
 
500
            
 
501
         /* I assume that 1 second is more than enough time to check the cache
 
502
          * and return. */
 
503
            assertTrue((later - now) < 1000L);
 
504
            
 
505
            /* Make sure that the previous wait didn't disrupt anything. */
 
506
            info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
507
            
 
508
            assertNotNull(info);
 
509
            assertEquals(jobId, info.getJobId());
 
510
            
 
511
            /* There's no reason that this job should exit prematurely. */
 
512
            assertTrue(info.hasExited());
 
513
            assertEquals(0, info.getExitStatus());
 
514
            
 
515
            /* Make sure that the previous wait worked. */
 
516
            try {
 
517
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
518
                fail("Second wait didn't reap job exit info");
 
519
            } catch (InvalidJobException e) {
 
520
                /* Don't care */
 
521
            }
 
522
            
 
523
            session.deleteJobTemplate(jt);
 
524
        } catch (DrmaaException e) {
 
525
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
526
        }
 
527
    }
 
528
    
 
529
    public void testWaitTimeoutAny() {
 
530
        System.out.println("testWaitTimeoutAny");
 
531
        
 
532
        try {
 
533
            JobTemplate jt = this.createSleeperTemplate(5);
 
534
            
 
535
            /* Test timed wait */
 
536
            String jobId = session.runJob(jt);
 
537
            long now = System.currentTimeMillis();
 
538
         /* I'm assuming that 3 seconds is not long enough for the job to be
 
539
          * scheduled and run. */
 
540
            JobInfo info = null;
 
541
            
 
542
            try {
 
543
                info = session.wait(session.JOB_IDS_SESSION_ANY, 3L);
 
544
                fail("Waited for job to finish; ignored 3s timeout");
 
545
            } catch (ExitTimeoutException e) {
 
546
                /* Don't care */
 
547
            }
 
548
            
 
549
            long later = System.currentTimeMillis();
 
550
            
 
551
         /* I'm assuming that there's no more than 1 second overhead in making
 
552
          * the call. */
 
553
            assertTrue((later - now) < 4000L);
 
554
            
 
555
            /* Make sure that the previous wait didn't disrupt anything. */
 
556
            info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
557
            
 
558
            assertNotNull(info);
 
559
            assertEquals(jobId, info.getJobId());
 
560
            
 
561
            /* There's no reason that this job should exit prematurely. */
 
562
            assertTrue(info.hasExited());
 
563
            assertEquals(0, info.getExitStatus());
 
564
            
 
565
            /* Make sure that the previous wait worked. */
 
566
            try {
 
567
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
568
                fail("Second wait didn't reap job exit info");
 
569
            } catch (InvalidJobException e) {
 
570
                /* Don't care */
 
571
            }
 
572
            
 
573
            session.deleteJobTemplate(jt);
 
574
        } catch (DrmaaException e) {
 
575
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
576
        }
 
577
    }
 
578
    
 
579
    public void testWaitAny() {
 
580
        System.out.println("testWaitAny");
 
581
        
 
582
        try {
 
583
            JobTemplate jt = this.createSleeperTemplate(5);
 
584
            
 
585
            /* Test timed wait */
 
586
            String jobId = session.runJob(jt);
 
587
            JobInfo info = session.wait(session.JOB_IDS_SESSION_ANY, 30L);
 
588
            
 
589
            assertNotNull(info);
 
590
            assertEquals(jobId, info.getJobId());
 
591
            
 
592
            /* There's no reason that this job should exit prematurely. */
 
593
            assertTrue(info.hasExited());
 
594
            assertEquals(0, info.getExitStatus());
 
595
            
 
596
            /* Make sure that the previous wait worked. */
 
597
            try {
 
598
                session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
599
                fail("First wait didn't reap job exit info");
 
600
            } catch (InvalidJobException e) {
 
601
                /* Don't care */
 
602
            }
 
603
            
 
604
            session.deleteJobTemplate(jt);
 
605
        } catch (DrmaaException e) {
 
606
            fail("Exception while trying to wait for a job: " + e.getMessage());
 
607
        }
 
608
    }
 
609
    
 
610
    /** Test of synchronize method, of class com.sun.grid.drmaa.SessionImpl. */
 
611
    public void testBadSynchronize() {
 
612
        System.out.println("testBadSynchronize");
 
613
        
 
614
        try {
 
615
            JobTemplate jt = this.createSleeperTemplate(5);
 
616
            
 
617
            List jobIds = session.runBulkJobs(jt, 1, 3, 1);
 
618
            
 
619
            /* Test bad parameters */
 
620
            try {
 
621
                session.synchronize(null, session.TIMEOUT_WAIT_FOREVER, true);
 
622
                fail("Allowed null job id");
 
623
            } catch (NullPointerException e) {
 
624
                /* Don't care */
 
625
            }
 
626
            
 
627
            List badJobId = Arrays.asList(new String[] {"asdf"});
 
628
            
 
629
            try {
 
630
                session.synchronize(badJobId, session.TIMEOUT_WAIT_FOREVER, true);
 
631
                fail("Allowed invalid job id");
 
632
            } catch (IllegalArgumentException e) {
 
633
                /* Don't care */
 
634
            }
 
635
            
 
636
            try {
 
637
                session.synchronize(jobIds, -3, true);
 
638
                fail("Allowed negative timeout");
 
639
            } catch (IllegalArgumentException e) {
 
640
                /* Don't care */
 
641
            }
 
642
            
 
643
            session.deleteJobTemplate(jt);
 
644
        } catch (DrmaaException e) {
 
645
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
646
        }
 
647
    }
 
648
    
 
649
    public void testSynchronizeForeverDispose() {
 
650
        System.out.println("testSynchronizeForeverDispose");
 
651
        
 
652
        try {
 
653
            JobTemplate jt = this.createSleeperTemplate(5);
 
654
            
 
655
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
656
            
 
657
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
658
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
659
            
 
660
            /* Test wait forever, dispose */
 
661
            session.synchronize(jobIds, session.TIMEOUT_WAIT_FOREVER, true);
 
662
            
 
663
            Iterator i = jobIds.iterator();
 
664
            
 
665
            while (i.hasNext()) {
 
666
                try {
 
667
                    session.wait((String)i.next(), session.TIMEOUT_NO_WAIT);
 
668
                    fail("Synchronize didn't reap exit information");
 
669
                } catch (InvalidJobException e) {
 
670
                    /* Don't care */
 
671
                }
 
672
            }
 
673
            
 
674
            session.deleteJobTemplate(jt);
 
675
        } catch (DrmaaException e) {
 
676
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
677
        }
 
678
    }
 
679
    
 
680
    public void testSynchronizeNoWaitDispose() {
 
681
        System.out.println("testSynchronizeNoWaitDispose");
 
682
        
 
683
        try {
 
684
            JobTemplate jt = this.createSleeperTemplate(5);
 
685
            
 
686
            /* Test no wait, dispose */
 
687
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
688
            
 
689
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
690
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
691
            
 
692
            long now = System.currentTimeMillis();
 
693
            
 
694
            try {
 
695
                session.synchronize(jobIds, session.TIMEOUT_NO_WAIT, true);
 
696
                fail("Waited for jobs to finish; ignored 0s timeout");
 
697
            } catch (ExitTimeoutException e) {
 
698
                /* Don't care */
 
699
            }
 
700
            
 
701
            long later = System.currentTimeMillis();
 
702
            
 
703
         /* I assume that 1 second is more than enough time to check the cache
 
704
          * and return. */
 
705
            assertTrue((later - now) < 1000L);
 
706
            
 
707
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
708
            Iterator i = jobIds.iterator();
 
709
            
 
710
            while (i.hasNext()) {
 
711
                String jobId = (String)i.next();
 
712
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
713
                
 
714
                assertNotNull(info);
 
715
                assertEquals(jobId, info.getJobId());
 
716
                
 
717
                /* There's no reason that this job should exit prematurely. */
 
718
                assertTrue(info.hasExited());
 
719
                assertEquals(0, info.getExitStatus());
 
720
            }
 
721
            
 
722
            session.deleteJobTemplate(jt);
 
723
        } catch (DrmaaException e) {
 
724
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
725
        }
 
726
    }
 
727
    
 
728
    public void testSynchronizeTimeoutDispose() {
 
729
        System.out.println("testSynchronizeTimeoutDispose");
 
730
        
 
731
        try {
 
732
            JobTemplate jt = this.createSleeperTemplate(15);
 
733
            
 
734
            /* Test timed wait (timeout), dispose */
 
735
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
736
            
 
737
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
738
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
739
            
 
740
            long now = System.currentTimeMillis();
 
741
            
 
742
         /* I'm assuming that 3 seconds won't be enough time for the job to be
 
743
          * scheduled and run. */
 
744
            try {
 
745
                session.synchronize(jobIds, 3L, true);
 
746
                fail("Waited for jobs to finish; ignored 3s timeout");
 
747
            } catch (ExitTimeoutException e) {
 
748
                /* Don't care */
 
749
            }
 
750
            
 
751
            long later = System.currentTimeMillis();
 
752
            
 
753
         /* I assume that there is less than 1 second overhead in making the
 
754
          * call. */
 
755
            assertTrue((later - now) < 4000L);
 
756
            
 
757
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
758
            Iterator i = jobIds.iterator();
 
759
            
 
760
            while (i.hasNext()) {
 
761
                String jobId = (String)i.next();
 
762
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
763
                
 
764
                assertNotNull(info);
 
765
                assertEquals(jobId, info.getJobId());
 
766
                
 
767
                /* There's no reason that this job should exit prematurely. */
 
768
                assertTrue(info.hasExited());
 
769
                assertEquals(0, info.getExitStatus());
 
770
            }
 
771
            
 
772
            session.deleteJobTemplate(jt);
 
773
        } catch (DrmaaException e) {
 
774
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
775
        }
 
776
    }
 
777
    
 
778
    public void testSynchronizeDispose() {
 
779
        System.out.println("testSynchronizeDispose");
 
780
        
 
781
        try {
 
782
            JobTemplate jt = this.createSleeperTemplate(5);
 
783
            
 
784
            /* Test timed wait, dispose */
 
785
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
786
            
 
787
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
788
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
789
            
 
790
            session.synchronize(jobIds, 600L, true);
 
791
            
 
792
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
793
            Iterator i = jobIds.iterator();
 
794
            
 
795
            while (i.hasNext()) {
 
796
                try {
 
797
                    session.wait((String)i.next(), session.TIMEOUT_NO_WAIT);
 
798
                    fail("Synchronize didn't reap exit information");
 
799
                } catch (InvalidJobException e) {
 
800
                    /* Don't care */
 
801
                }
 
802
            }
 
803
            
 
804
            session.deleteJobTemplate(jt);
 
805
        } catch (DrmaaException e) {
 
806
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
807
        }
 
808
    }
 
809
    
 
810
    public void testSynchronizeForever() {
 
811
        System.out.println("testSynchronizeForever");
 
812
        
 
813
        try {
 
814
            JobTemplate jt = this.createSleeperTemplate(5);
 
815
            
 
816
            /* Test wait forever, no dispose */
 
817
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
818
            
 
819
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
820
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
821
            
 
822
            session.synchronize(jobIds, session.TIMEOUT_WAIT_FOREVER, false);
 
823
            
 
824
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
825
            Iterator i = jobIds.iterator();
 
826
            
 
827
            while (i.hasNext()) {
 
828
                String jobId = (String)i.next();
 
829
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
830
                
 
831
                assertNotNull(info);
 
832
                assertEquals(jobId, info.getJobId());
 
833
                
 
834
                /* There's no reason that this job should exit prematurely. */
 
835
                assertTrue(info.hasExited());
 
836
                assertEquals(0, info.getExitStatus());
 
837
            }
 
838
            
 
839
            session.deleteJobTemplate(jt);
 
840
        } catch (DrmaaException e) {
 
841
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
842
        }
 
843
    }
 
844
    
 
845
    public void testSynchronizeNoWait() {
 
846
        System.out.println("testSynchronizeNoWait");
 
847
        
 
848
        try {
 
849
            JobTemplate jt = this.createSleeperTemplate(5);
 
850
            
 
851
            /* Test no wait, no dispose */
 
852
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
853
            
 
854
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
855
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
856
            
 
857
            long now = System.currentTimeMillis();
 
858
            
 
859
            try {
 
860
                session.synchronize(jobIds, session.TIMEOUT_NO_WAIT, false);
 
861
                fail("Waited for jobs to finish; ignored 0s timeout");
 
862
            } catch (ExitTimeoutException e) {
 
863
                /* Don't care */
 
864
            }
 
865
            
 
866
            long later = System.currentTimeMillis();
 
867
            
 
868
         /* I assume that there is less than 1 second overhead in making the
 
869
          * call. */
 
870
            assertTrue((later - now) < 1000L);
 
871
            
 
872
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
873
            Iterator i = jobIds.iterator();
 
874
            
 
875
            while (i.hasNext()) {
 
876
                String jobId = (String)i.next();
 
877
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
878
                
 
879
                assertNotNull(info);
 
880
                assertEquals(jobId, info.getJobId());
 
881
                
 
882
                /* There's no reason that this job should exit prematurely. */
 
883
                assertTrue(info.hasExited());
 
884
                assertEquals(0, info.getExitStatus());
 
885
            }
 
886
            
 
887
            session.deleteJobTemplate(jt);
 
888
        } catch (DrmaaException e) {
 
889
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
890
        }
 
891
    }
 
892
    
 
893
    public void testSynchronizeTimeout() {
 
894
        System.out.println("testSynchronizeTimeout");
 
895
        
 
896
        try {
 
897
            JobTemplate jt = this.createSleeperTemplate(15);
 
898
            
 
899
            /* Test timed wait (timeout), no dispose */
 
900
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
901
            
 
902
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
903
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
904
            
 
905
            long now = System.currentTimeMillis();
 
906
            
 
907
         /* I'm assuming that 3 seconds won't be enough time for the job to be
 
908
          * scheduled and run. */
 
909
            try {
 
910
                session.synchronize(jobIds, 3L, false);
 
911
                fail("Waited for jobs to finish; ignored 3s timeout");
 
912
            } catch (ExitTimeoutException e) {
 
913
                /* Don't care */
 
914
            }
 
915
            
 
916
            long later = System.currentTimeMillis();
 
917
            
 
918
         /* I assume that there is less than 1 second overhead in making the
 
919
          * call. */
 
920
            assertTrue((later - now) < 4000L);
 
921
            
 
922
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
923
            Iterator i = jobIds.iterator();
 
924
            
 
925
            while (i.hasNext()) {
 
926
                String jobId = (String)i.next();
 
927
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
928
                
 
929
                assertNotNull(info);
 
930
                assertEquals(jobId, info.getJobId());
 
931
                
 
932
                /* There's no reason that this job should exit prematurely. */
 
933
                assertTrue(info.hasExited());
 
934
                assertEquals(0, info.getExitStatus());
 
935
            }
 
936
            
 
937
            session.deleteJobTemplate(jt);
 
938
        } catch (DrmaaException e) {
 
939
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
940
        }
 
941
    }
 
942
    
 
943
    public void testSynchronize() {
 
944
        System.out.println("testSynchronize");
 
945
        
 
946
        try {
 
947
            JobTemplate jt = this.createSleeperTemplate(5);
 
948
            
 
949
            /* Test timed wait (timeout), no dispose */
 
950
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
951
            
 
952
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
953
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
954
            
 
955
            session.synchronize(jobIds, 600L, false);
 
956
            
 
957
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
958
            Iterator i = jobIds.iterator();
 
959
            
 
960
            while (i.hasNext()) {
 
961
                String jobId = (String)i.next();
 
962
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
963
                
 
964
                assertNotNull(info);
 
965
                assertEquals(jobId, info.getJobId());
 
966
                
 
967
                /* There's no reason that this job should exit prematurely. */
 
968
                assertTrue(info.hasExited());
 
969
                assertEquals(0, info.getExitStatus());
 
970
            }
 
971
            
 
972
            session.deleteJobTemplate(jt);
 
973
        } catch (DrmaaException e) {
 
974
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
975
        }
 
976
    }
 
977
    
 
978
    public void testSynchronizeForeverDisposeAll() {
 
979
        System.out.println("testSynchronizeForeverDisposeAll");
 
980
        
 
981
        try {
 
982
            JobTemplate jt = this.createSleeperTemplate(5);
 
983
            
 
984
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
985
            
 
986
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
987
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
988
            
 
989
            /* Test wait forever, dispose */
 
990
            session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), session.TIMEOUT_WAIT_FOREVER, true);
 
991
            
 
992
            Iterator i = jobIds.iterator();
 
993
            
 
994
            while (i.hasNext()) {
 
995
                try {
 
996
                    session.wait((String)i.next(), session.TIMEOUT_NO_WAIT);
 
997
                    fail("Synchronize didn't reap exit information");
 
998
                } catch (InvalidJobException e) {
 
999
                    /* Don't care */
 
1000
                }
 
1001
            }
 
1002
            
 
1003
            session.deleteJobTemplate(jt);
 
1004
        } catch (DrmaaException e) {
 
1005
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1006
        }
 
1007
    }
 
1008
    
 
1009
    public void testSynchronizeNoWaitDisposeAll() {
 
1010
        System.out.println("testSynchronizeNoWaitDisposeAll");
 
1011
        
 
1012
        try {
 
1013
            JobTemplate jt = this.createSleeperTemplate(5);
 
1014
            
 
1015
            /* Test no wait, dispose */
 
1016
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1017
            
 
1018
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1019
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1020
            
 
1021
            long now = System.currentTimeMillis();
 
1022
            
 
1023
            try {
 
1024
                session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), session.TIMEOUT_NO_WAIT, true);
 
1025
                fail("Waited for jobs to finish; ignored 0s timeout");
 
1026
            } catch (ExitTimeoutException e) {
 
1027
                /* Don't care */
 
1028
            }
 
1029
            
 
1030
            long later = System.currentTimeMillis();
 
1031
            
 
1032
         /* I assume that 1 second is more than enough time to check the cache
 
1033
          * and return. */
 
1034
            assertTrue((later - now) < 1000L);
 
1035
            
 
1036
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
1037
            Iterator i = jobIds.iterator();
 
1038
            
 
1039
            while (i.hasNext()) {
 
1040
                String jobId = (String)i.next();
 
1041
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
1042
                
 
1043
                assertNotNull(info);
 
1044
                assertEquals(jobId, info.getJobId());
 
1045
                
 
1046
                /* There's no reason that this job should exit prematurely. */
 
1047
                assertTrue(info.hasExited());
 
1048
                assertEquals(0, info.getExitStatus());
 
1049
            }
 
1050
            
 
1051
            session.deleteJobTemplate(jt);
 
1052
        } catch (DrmaaException e) {
 
1053
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1054
        }
 
1055
    }
 
1056
    
 
1057
    public void testSynchronizeTimeoutDisposeAll() {
 
1058
        System.out.println("testSynchronizeTimeoutDisposeAll");
 
1059
        
 
1060
        try {
 
1061
            JobTemplate jt = this.createSleeperTemplate(15);
 
1062
            
 
1063
            /* Test timed wait (timeout), dispose */
 
1064
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1065
            
 
1066
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1067
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1068
            
 
1069
            long now = System.currentTimeMillis();
 
1070
            
 
1071
         /* I'm assuming that 3 seconds won't be enough time for the job to be
 
1072
          * scheduled and run. */
 
1073
            try {
 
1074
                session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), 3L, true);
 
1075
                fail("Waited for jobs to finish; ignored 3s timeout");
 
1076
            } catch (ExitTimeoutException e) {
 
1077
                /* Don't care */
 
1078
            }
 
1079
            
 
1080
            long later = System.currentTimeMillis();
 
1081
            
 
1082
         /* I assume that there is less than 1 second overhead in making the
 
1083
          * call. */
 
1084
            assertTrue((later - now) < 4000L);
 
1085
            
 
1086
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
1087
            Iterator i = jobIds.iterator();
 
1088
            
 
1089
            while (i.hasNext()) {
 
1090
                String jobId = (String)i.next();
 
1091
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
1092
                
 
1093
                assertNotNull(info);
 
1094
                assertEquals(jobId, info.getJobId());
 
1095
                
 
1096
                /* There's no reason that this job should exit prematurely. */
 
1097
                assertTrue(info.hasExited());
 
1098
                assertEquals(0, info.getExitStatus());
 
1099
            }
 
1100
            
 
1101
            session.deleteJobTemplate(jt);
 
1102
        } catch (DrmaaException e) {
 
1103
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1104
        }
 
1105
    }
 
1106
    
 
1107
    public void testSynchronizeDisposeAll() {
 
1108
        System.out.println("testSynchronizeDisposeAll");
 
1109
        
 
1110
        try {
 
1111
            JobTemplate jt = this.createSleeperTemplate(5);
 
1112
            
 
1113
            /* Test timed wait, dispose */
 
1114
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1115
            
 
1116
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1117
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1118
            
 
1119
            session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), 600L, true);
 
1120
            
 
1121
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
1122
            Iterator i = jobIds.iterator();
 
1123
            
 
1124
            while (i.hasNext()) {
 
1125
                try {
 
1126
                    JobInfo info = session.wait((String)i.next(), session.TIMEOUT_NO_WAIT);
 
1127
                    fail("Synchronize didn't reap exit information for " + info.getJobId());
 
1128
                } catch (InvalidJobException e) {
 
1129
                    /* Don't care */
 
1130
                }
 
1131
            }
 
1132
            
 
1133
            session.deleteJobTemplate(jt);
 
1134
        } catch (DrmaaException e) {
 
1135
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1136
        }
 
1137
    }
 
1138
    
 
1139
    public void testSynchronizeForeverAll() {
 
1140
        System.out.println("testSynchronizeForeverAll");
 
1141
        
 
1142
        try {
 
1143
            JobTemplate jt = this.createSleeperTemplate(5);
 
1144
            
 
1145
            /* Test wait forever, no dispose */
 
1146
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1147
            
 
1148
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1149
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1150
            
 
1151
            session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), session.TIMEOUT_WAIT_FOREVER, false);
 
1152
            
 
1153
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
1154
            Iterator i = jobIds.iterator();
 
1155
            
 
1156
            while (i.hasNext()) {
 
1157
                String jobId = (String)i.next();
 
1158
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
1159
                
 
1160
                assertNotNull(info);
 
1161
                assertEquals(jobId, info.getJobId());
 
1162
                
 
1163
                /* There's no reason that this job should exit prematurely. */
 
1164
                assertTrue(info.hasExited());
 
1165
                assertEquals(0, info.getExitStatus());
 
1166
            }
 
1167
            
 
1168
            session.deleteJobTemplate(jt);
 
1169
        } catch (DrmaaException e) {
 
1170
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1171
        }
 
1172
    }
 
1173
    
 
1174
    public void testSynchronizeNoWaitAll() {
 
1175
        System.out.println("testSynchronizeNoWaitAll");
 
1176
        
 
1177
        try {
 
1178
            JobTemplate jt = this.createSleeperTemplate(5);
 
1179
            
 
1180
            /* Test no wait, no dispose */
 
1181
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1182
            
 
1183
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1184
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1185
            
 
1186
            long now = System.currentTimeMillis();
 
1187
            
 
1188
            try {
 
1189
                session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), session.TIMEOUT_NO_WAIT, false);
 
1190
                fail("Waited for jobs to finish; ignored 0s timeout");
 
1191
            } catch (ExitTimeoutException e) {
 
1192
                /* Don't care */
 
1193
            }
 
1194
            
 
1195
            long later = System.currentTimeMillis();
 
1196
            
 
1197
         /* I assume that there is less than 1 second overhead in making the
 
1198
          * call. */
 
1199
            assertTrue((later - now) < 1000L);
 
1200
            
 
1201
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
1202
            Iterator i = jobIds.iterator();
 
1203
            
 
1204
            while (i.hasNext()) {
 
1205
                String jobId = (String)i.next();
 
1206
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
1207
                
 
1208
                assertNotNull(info);
 
1209
                assertEquals(jobId, info.getJobId());
 
1210
                
 
1211
                /* There's no reason that this job should exit prematurely. */
 
1212
                assertTrue(info.hasExited());
 
1213
                assertEquals(0, info.getExitStatus());
 
1214
            }
 
1215
            
 
1216
            session.deleteJobTemplate(jt);
 
1217
        } catch (DrmaaException e) {
 
1218
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1219
        }
 
1220
    }
 
1221
    
 
1222
    public void testSynchronizeTimeoutAll() {
 
1223
        System.out.println("testSynchronizeTimeoutAll");
 
1224
        
 
1225
        try {
 
1226
            JobTemplate jt = this.createSleeperTemplate(15);
 
1227
            
 
1228
            /* Test timed wait (timeout), no dispose */
 
1229
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1230
            
 
1231
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1232
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1233
            
 
1234
            long now = System.currentTimeMillis();
 
1235
            
 
1236
         /* I'm assuming that 3 seconds won't be enough time for the job to be
 
1237
          * scheduled and run. */
 
1238
            try {
 
1239
                session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), 3L, false);
 
1240
                fail("Waited for jobs to finish; ignored 3s timeout");
 
1241
            } catch (ExitTimeoutException e) {
 
1242
                /* Don't care */
 
1243
            }
 
1244
            
 
1245
            long later = System.currentTimeMillis();
 
1246
            
 
1247
         /* I assume that there is less than 1 second overhead in making the
 
1248
          * call. */
 
1249
            assertTrue((later - now) < 4000L);
 
1250
            
 
1251
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
1252
            Iterator i = jobIds.iterator();
 
1253
            
 
1254
            while (i.hasNext()) {
 
1255
                String jobId = (String)i.next();
 
1256
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
1257
                
 
1258
                assertNotNull(info);
 
1259
                assertEquals(jobId, info.getJobId());
 
1260
                
 
1261
                /* There's no reason that this job should exit prematurely. */
 
1262
                assertTrue(info.hasExited());
 
1263
                assertEquals(0, info.getExitStatus());
 
1264
            }
 
1265
            
 
1266
            session.deleteJobTemplate(jt);
 
1267
        } catch (DrmaaException e) {
 
1268
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1269
        }
 
1270
    }
 
1271
    
 
1272
    public void testSynchronizeAll() {
 
1273
        System.out.println("testSynchronizeAll");
 
1274
        
 
1275
        try {
 
1276
            JobTemplate jt = this.createSleeperTemplate(5);
 
1277
            
 
1278
            /* Test timed wait (timeout), no dispose */
 
1279
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1280
            
 
1281
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1282
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1283
            
 
1284
            session.synchronize(Collections.singletonList(session.JOB_IDS_SESSION_ALL), 600L, false);
 
1285
            
 
1286
            /* Make sure that the previous synchronize didn't disrupt anything. */
 
1287
            Iterator i = jobIds.iterator();
 
1288
            
 
1289
            while (i.hasNext()) {
 
1290
                String jobId = (String)i.next();
 
1291
                JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
1292
                
 
1293
                assertNotNull(info);
 
1294
                assertEquals(jobId, info.getJobId());
 
1295
                
 
1296
                /* There's no reason that this job should exit prematurely. */
 
1297
                assertTrue(info.hasExited());
 
1298
                assertEquals(0, info.getExitStatus());
 
1299
            }
 
1300
            
 
1301
            session.deleteJobTemplate(jt);
 
1302
        } catch (DrmaaException e) {
 
1303
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1304
        }
 
1305
    }
 
1306
    
 
1307
    public void testSynchronizeForeverDisposeMoreThanAll() {
 
1308
        System.out.println("testSynchronizeDisposeMoreThanAll");
 
1309
        
 
1310
        try {
 
1311
            JobTemplate jt = this.createSleeperTemplate(5);
 
1312
            
 
1313
            /* Test timed wait, dispose */
 
1314
            List jobIds = new LinkedList(session.runBulkJobs(jt, 1, 3, 1));
 
1315
            
 
1316
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1317
            jobIds.addAll(session.runBulkJobs(jt, 1, 3, 1));
 
1318
            
 
1319
            List myJobIds = new ArrayList(2);
 
1320
            
 
1321
            myJobIds.add(jobIds.get(0));
 
1322
            myJobIds.add(session.JOB_IDS_SESSION_ALL);
 
1323
            session.synchronize(myJobIds, session.TIMEOUT_WAIT_FOREVER, true);
 
1324
            
 
1325
            /* Make sure that the previous synchronize reaped exit info. */
 
1326
            Iterator i = jobIds.iterator();
 
1327
            
 
1328
            while (i.hasNext()) {
 
1329
                try {
 
1330
                    session.wait((String)i.next(), session.TIMEOUT_NO_WAIT);
 
1331
                    fail("Synchronize didn't reap exit information");
 
1332
                } catch (InvalidJobException e) {
 
1333
                    /* Don't care */
 
1334
                }
 
1335
            }
 
1336
            
 
1337
            session.deleteJobTemplate(jt);
 
1338
        } catch (DrmaaException e) {
 
1339
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1340
        }
 
1341
    }
 
1342
    
 
1343
    public void testSynchronizeNonexistant() {
 
1344
        System.out.println("testSynchronizeNonexistant");
 
1345
        
 
1346
        try {
 
1347
            JobTemplate jt = this.createSleeperTemplate(5);
 
1348
            
 
1349
            /* Test timed wait (timeout), no dispose */
 
1350
            String jobId = session.runJob(jt);
 
1351
            /* Create a valid, unknown id. */
 
1352
            String nextId = Integer.toString(Integer.parseInt(jobId) + 1);
 
1353
            List jobIds = Collections.singletonList(nextId);
 
1354
            
 
1355
            try {
 
1356
                session.synchronize(jobIds, session.TIMEOUT_WAIT_FOREVER, false);
 
1357
                // Success!
 
1358
            } catch (InvalidJobException e) {
 
1359
                fail("Synchronize on non-existant job id failed");
 
1360
            }
 
1361
            
 
1362
            /* Wait for the real job to end. */
 
1363
            JobInfo info = session.wait(jobId, session.TIMEOUT_WAIT_FOREVER);
 
1364
            
 
1365
            assertNotNull(info);
 
1366
            assertEquals(jobId, info.getJobId());
 
1367
            
 
1368
            /* There's no reason that this job should exit prematurely. */
 
1369
            assertTrue(info.hasExited());
 
1370
            assertEquals(0, info.getExitStatus());
 
1371
            
 
1372
            session.deleteJobTemplate(jt);
 
1373
        } catch (DrmaaException e) {
 
1374
            fail("Exception while trying to synchronize jobs: " + e.getMessage());
 
1375
        }
 
1376
    }
 
1377
    
 
1378
    /** Test of getJobProgramStatus method, of class com.sun.grid.drmaa.SessionImpl. */
 
1379
    public void testBadGetJobProgramStatus() {
 
1380
        System.out.println("testBadGetJobProgramStatus");
 
1381
        
 
1382
        try {
 
1383
            try {
 
1384
                session.getJobProgramStatus(null);
 
1385
                fail("Allowed null job id");
 
1386
            } catch (NullPointerException e) {
 
1387
                /* Don't care */
 
1388
            }
 
1389
            
 
1390
            try {
 
1391
                session.getJobProgramStatus("asdf");
 
1392
                fail("Allowed invalid job id");
 
1393
            } catch (IllegalArgumentException e) {
 
1394
                /* Don't care */
 
1395
            }
 
1396
        } catch (DrmaaException e) {
 
1397
            fail("Exception while trying to get job status: " + e.getMessage());
 
1398
        }
 
1399
    }
 
1400
    
 
1401
    public void testGetJobProgramStatus() {
 
1402
        System.out.println("testGetJobProgramStatus");
 
1403
        
 
1404
        try {
 
1405
            JobTemplate jt = this.createSleeperTemplate(5);
 
1406
            
 
1407
            String jobId = session.runJob(jt);
 
1408
         /* Make sure it doesn't throw an exception.  We can't really be sure
 
1409
          * what the state will be at this point. */
 
1410
            session.getJobProgramStatus(jobId);
 
1411
            /* We use synchronize so that we don't reap the job info. */
 
1412
            session.synchronize(Collections.singletonList(jobId), session.TIMEOUT_WAIT_FOREVER, false);
 
1413
            
 
1414
            int status = session.getJobProgramStatus(jobId);
 
1415
            
 
1416
            /* No reason why this job should fail. */
 
1417
            assertEquals(session.DONE, status);
 
1418
            
 
1419
            /* How do we test the other states??? */
 
1420
            
 
1421
            session.deleteJobTemplate(jt);
 
1422
        } catch (DrmaaException e) {
 
1423
            fail("Exception while trying to get job status: " + e.getMessage());
 
1424
        }
 
1425
    }
 
1426
    
 
1427
    /** Test of control method, of class com.sun.grid.drmaa.SessionImpl. */
 
1428
    public void testBadControl() {
 
1429
        System.out.println("testBadControl");
 
1430
        
 
1431
        try {
 
1432
            JobTemplate jt = this.createSleeperTemplate(5);
 
1433
            
 
1434
            String jobId = session.runJob(jt);
 
1435
            
 
1436
            try {
 
1437
                session.control(null, session.HOLD);
 
1438
                fail("Allowed null job id");
 
1439
            } catch (NullPointerException e) {
 
1440
                /* Don't care */
 
1441
            }
 
1442
            
 
1443
            try {
 
1444
                session.control("asdf", session.HOLD);
 
1445
                fail("Allowed invalid job id");
 
1446
            } catch (IllegalArgumentException e) {
 
1447
                /* Don't care */
 
1448
            }
 
1449
            
 
1450
            try {
 
1451
                session.control(jobId, -10);
 
1452
                fail("Allowed invalid action");
 
1453
            } catch (IllegalArgumentException e) {
 
1454
                /* Don't care */
 
1455
            }
 
1456
            
 
1457
            session.deleteJobTemplate(jt);
 
1458
        } catch (DrmaaException e) {
 
1459
            fail("Exception while trying to get job status: " + e.getMessage());
 
1460
        }
 
1461
    }
 
1462
    
 
1463
    public void testControl() {
 
1464
        System.out.println("testControl");
 
1465
        
 
1466
        try {
 
1467
            JobTemplate jt = this.createSleeperTemplate(60000);
 
1468
            
 
1469
            String jobId = session.runJob(jt);
 
1470
            
 
1471
            session.deleteJobTemplate(jt);
 
1472
            
 
1473
            /* Take a nap so that we give the job time to get scheduled. */
 
1474
            try {
 
1475
                Thread.sleep(30000);
 
1476
            } catch (InterruptedException e) {
 
1477
                fail("Sleep was interrupted");
 
1478
            }
 
1479
            
 
1480
            int status = session.getJobProgramStatus(jobId);
 
1481
            
 
1482
            /* Make sure the job is running. */
 
1483
            assertEquals(session.RUNNING, status);
 
1484
            
 
1485
            session.control(jobId, session.HOLD);
 
1486
            
 
1487
            /* Take a nap so that we give the job time to held. */
 
1488
            try {
 
1489
                Thread.sleep(30000);
 
1490
            } catch (InterruptedException e) {
 
1491
                fail("Sleep was interrupted");
 
1492
            }
 
1493
            
 
1494
            status = session.getJobProgramStatus(jobId);
 
1495
            assertEquals(session.USER_ON_HOLD, status);
 
1496
            
 
1497
            session.control(jobId, session.RELEASE);
 
1498
            
 
1499
            /* Take a nap so that we give the job time to released. */
 
1500
            try {
 
1501
                Thread.sleep(30000);
 
1502
            } catch (InterruptedException e) {
 
1503
                fail("Sleep was interrupted");
 
1504
            }
 
1505
            
 
1506
            status = session.getJobProgramStatus(jobId);
 
1507
            assertEquals(session.RUNNING, status);
 
1508
            
 
1509
            session.control(jobId, session.SUSPEND);
 
1510
            
 
1511
            /* Take a nap so that we give the job time to suspended. */
 
1512
            try {
 
1513
                Thread.sleep(30000);
 
1514
            } catch (InterruptedException e) {
 
1515
                fail("Sleep was interrupted");
 
1516
            }
 
1517
            
 
1518
            status = session.getJobProgramStatus(jobId);
 
1519
            assertEquals(session.USER_SUSPENDED, status);
 
1520
            
 
1521
            session.control(jobId, session.RESUME);
 
1522
            
 
1523
            /* Take a nap so that we give the job time to resumed. */
 
1524
            try {
 
1525
                Thread.sleep(30000);
 
1526
            } catch (InterruptedException e) {
 
1527
                fail("Sleep was interrupted");
 
1528
            }
 
1529
            
 
1530
            status = session.getJobProgramStatus(jobId);
 
1531
            assertEquals(session.RUNNING, status);
 
1532
            
 
1533
            session.control(jobId, session.TERMINATE);
 
1534
            
 
1535
            /* Take a nap so that we give the job time to killed. */
 
1536
            try {
 
1537
                Thread.sleep(60000);
 
1538
            } catch (InterruptedException e) {
 
1539
                fail("Sleep was interrupted");
 
1540
            }
 
1541
            
 
1542
            status = session.getJobProgramStatus(jobId);
 
1543
            assertEquals(session.FAILED, status);
 
1544
        } catch (DrmaaException e) {
 
1545
            fail("Exception while trying to get job status: " + e.getMessage());
 
1546
        }
 
1547
    }
 
1548
    
 
1549
    public void testControlAll() {
 
1550
        System.out.println("testControlAll");
 
1551
        
 
1552
        try {
 
1553
            JobTemplate jt = this.createSleeperTemplate(60000);
 
1554
            
 
1555
            String jobId1 = session.runJob(jt);
 
1556
            String jobId2 = session.runJob(jt);
 
1557
            String jobId3 = session.runJob(jt);
 
1558
            
 
1559
            session.deleteJobTemplate(jt);
 
1560
            
 
1561
            /* Take a nap so that we give the jobs time to get scheduled. */
 
1562
            try {
 
1563
                Thread.sleep(30000);
 
1564
            } catch (InterruptedException e) {
 
1565
                fail("Sleep was interrupted");
 
1566
            }
 
1567
            
 
1568
            /* Make sure the job is running. */
 
1569
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId1));
 
1570
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId2));
 
1571
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId3));
 
1572
            
 
1573
            session.control(Session.JOB_IDS_SESSION_ALL, session.HOLD);
 
1574
            
 
1575
            /* Take a nap so that we give the jobs time to held. */
 
1576
            try {
 
1577
                Thread.sleep(30000);
 
1578
            } catch (InterruptedException e) {
 
1579
                fail("Sleep was interrupted");
 
1580
            }
 
1581
            
 
1582
            assertEquals(session.USER_ON_HOLD, session.getJobProgramStatus(jobId1));
 
1583
            assertEquals(session.USER_ON_HOLD, session.getJobProgramStatus(jobId2));
 
1584
            assertEquals(session.USER_ON_HOLD, session.getJobProgramStatus(jobId3));
 
1585
            
 
1586
            session.control(Session.JOB_IDS_SESSION_ALL, session.RELEASE);
 
1587
            
 
1588
            /* Take a nap so that we give the jobs time to released. */
 
1589
            try {
 
1590
                Thread.sleep(30000);
 
1591
            } catch (InterruptedException e) {
 
1592
                fail("Sleep was interrupted");
 
1593
            }
 
1594
            
 
1595
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId1));
 
1596
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId2));
 
1597
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId3));
 
1598
            
 
1599
            session.control(Session.JOB_IDS_SESSION_ALL, session.SUSPEND);
 
1600
            
 
1601
            /* Take a nap so that we give the jobs time to suspended. */
 
1602
            try {
 
1603
                Thread.sleep(30000);
 
1604
            } catch (InterruptedException e) {
 
1605
                fail("Sleep was interrupted");
 
1606
            }
 
1607
            
 
1608
            assertEquals(session.USER_SUSPENDED, session.getJobProgramStatus(jobId1));
 
1609
            assertEquals(session.USER_SUSPENDED, session.getJobProgramStatus(jobId2));
 
1610
            assertEquals(session.USER_SUSPENDED, session.getJobProgramStatus(jobId3));
 
1611
            
 
1612
            session.control(Session.JOB_IDS_SESSION_ALL, session.RESUME);
 
1613
            
 
1614
            /* Take a nap so that we give the jobs time to resumed. */
 
1615
            try {
 
1616
                Thread.sleep(30000);
 
1617
            } catch (InterruptedException e) {
 
1618
                fail("Sleep was interrupted");
 
1619
            }
 
1620
            
 
1621
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId1));
 
1622
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId2));
 
1623
            assertEquals(session.RUNNING, session.getJobProgramStatus(jobId3));
 
1624
            
 
1625
            session.control(Session.JOB_IDS_SESSION_ALL, session.TERMINATE);
 
1626
            
 
1627
            /* Take a nap so that we give the jobs time to killed. */
 
1628
            try {
 
1629
                Thread.sleep(60000);
 
1630
            } catch (InterruptedException e) {
 
1631
                fail("Sleep was interrupted");
 
1632
            }
 
1633
            
 
1634
            assertEquals(session.FAILED, session.getJobProgramStatus(jobId1));
 
1635
            assertEquals(session.FAILED, session.getJobProgramStatus(jobId2));
 
1636
            assertEquals(session.FAILED, session.getJobProgramStatus(jobId3));
 
1637
        } catch (DrmaaException e) {
 
1638
            fail("Exception while trying to get job status: " + e.getMessage());
 
1639
        }
 
1640
    }
 
1641
    
 
1642
    public void testRecoverableSession() throws DrmaaException {
 
1643
        System.out.println("testRecoverableSession");
 
1644
        
 
1645
        String contact = session.getContact();
 
1646
        
 
1647
        JobTemplate jt = this.createSleeperTemplate(120);
 
1648
        
 
1649
        String jobId1 = session.runJob(jt);
 
1650
        
 
1651
        session.deleteJobTemplate(jt);
 
1652
        
 
1653
        jt = this.createSleeperTemplate(10);
 
1654
        
 
1655
        String jobId2 = session.runJob(jt);
 
1656
        
 
1657
        session.deleteJobTemplate(jt);
 
1658
        
 
1659
        jt = this.createSleeperTemplate(10);
 
1660
        jt.setJobSubmissionState(jt.HOLD_STATE);
 
1661
        
 
1662
        List jobIds34 = session.runBulkJobs(jt, 1, 2, 1);
 
1663
        
 
1664
        session.deleteJobTemplate(jt);
 
1665
        session.control((String)jobIds34.get(0), session.RELEASE);
 
1666
        
 
1667
        session.exit();
 
1668
        
 
1669
        /* Take a nap so that we give the job time to get scheduled. */
 
1670
        try {
 
1671
            Thread.sleep(60000);
 
1672
        } catch (InterruptedException e) {
 
1673
            fail("Sleep was interrupted");
 
1674
        }
 
1675
        
 
1676
        session.init(contact);
 
1677
        
 
1678
        JobInfo ji = session.wait(jobId1, session.TIMEOUT_WAIT_FOREVER);
 
1679
        
 
1680
        assertEquals(true, ji.hasExited());
 
1681
        assertEquals(0, ji.getExitStatus());
 
1682
        assertEquals(jobId1, ji.getJobId());
 
1683
        
 
1684
        try {
 
1685
            ji = session.wait(jobId2, session.TIMEOUT_WAIT_FOREVER);
 
1686
 
 
1687
            assertNull(ji.getResourceUsage());
 
1688
        } catch (InvalidJobException e) {
 
1689
            // Supposed to happen
 
1690
        }
 
1691
        
 
1692
        ji = session.wait((String)jobIds34.get(0), session.TIMEOUT_WAIT_FOREVER);
 
1693
        assertNull(ji.getResourceUsage());
 
1694
        
 
1695
        try {
 
1696
            session.wait((String)jobIds34.get(1), session.TIMEOUT_NO_WAIT);
 
1697
            fail("Call to wait() did not time out as expect");
 
1698
        } catch (ExitTimeoutException ex) {
 
1699
            // Supposed to happen
 
1700
        }
 
1701
        
 
1702
        session.control((String)jobIds34.get(1), session.TERMINATE);
 
1703
    }
 
1704
    
 
1705
    private JobTemplate createSleeperTemplate(int sleep) throws DrmaaException {
 
1706
        JobTemplate jt = session.createJobTemplate();
 
1707
        
 
1708
        jt.setRemoteCommand(SLEEPER);
 
1709
        jt.setArgs(Collections.singletonList(Integer.toString(sleep)));
 
1710
        jt.setOutputPath(":/dev/null");
 
1711
        jt.setJoinFiles(true);
 
1712
        
 
1713
        return jt;
 
1714
    }
 
1715
    
 
1716
    private class BadJobTemplate extends SimpleJobTemplate {
 
1717
        public BadJobTemplate() {
 
1718
        }
 
1719
    }
 
1720
}