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

« back to all changes in this revision

Viewing changes to source/libs/jgdi/test/com/sun/grid/jgdi/event/JobTaskEventTestCase.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
package com.sun.grid.jgdi.event;
 
33
 
 
34
import com.sun.grid.jgdi.BaseTestCase;
 
35
import com.sun.grid.jgdi.JGDI;
 
36
import com.sun.grid.jgdi.EventClient;
 
37
import com.sun.grid.jgdi.JobSubmitter;
 
38
import java.util.HashMap;
 
39
import java.util.Map;
 
40
import junit.framework.Test;
 
41
import junit.framework.TestSuite;
 
42
 
 
43
/**
 
44
 *
 
45
 */
 
46
public class JobTaskEventTestCase extends BaseTestCase {
 
47
    
 
48
    private JGDI jgdi;
 
49
    private EventClient evc;
 
50
    
 
51
    /** Creates a new instance of SpecialEventTestCase */
 
52
    public JobTaskEventTestCase(String testName) {
 
53
        super(testName);
 
54
    }
 
55
    
 
56
    protected void setUp() throws Exception {
 
57
        
 
58
        jgdi = createJGDI();
 
59
        evc = createEventClient(0);
 
60
        super.setUp();
 
61
        logger.fine("SetUp done");
 
62
    }
 
63
    
 
64
    protected void tearDown() throws Exception {
 
65
        try {
 
66
            evc.close();
 
67
        } finally {
 
68
            jgdi.close();
 
69
            evc.close();
 
70
        }
 
71
    }
 
72
    
 
73
    public void testJobTaskEvents() throws Exception {
 
74
        
 
75
        
 
76
        jgdi.disableQueues(new String[]{"*"}, false);
 
77
        
 
78
        int numberOfTasks = 10;
 
79
        
 
80
        int jobid = JobSubmitter.submitJob(getCurrentCluster(), new String[]{"-t", "1-" + numberOfTasks, "$SGE_ROOT/examples/jobs/sleeper.sh", "1"});
 
81
        
 
82
        Map<EventTypeEnum,Integer> map = new HashMap<EventTypeEnum,Integer>();
 
83
        map.put(EventTypeEnum.JobTaskAdd, 1);
 
84
        map.put(EventTypeEnum.JobTaskDel, 1);
 
85
        map.put(EventTypeEnum.JobDel, 1);
 
86
        
 
87
        evc.subscribe(map.keySet());
 
88
        evc.setFlush(map);
 
89
        
 
90
        JobTaskEventListener lis = new JobTaskEventListener(jobid);
 
91
        evc.addEventListener(lis);
 
92
        
 
93
        evc.commit();
 
94
        
 
95
        Thread.sleep(2);
 
96
        
 
97
        jgdi.enableQueues(new String[]{"*"}, false);
 
98
        
 
99
        int timeout = 300;
 
100
        assertTrue("timeout while waiting for job finish event", lis.waitForJobFinish(timeout));
 
101
        assertEquals("too few job task add events", numberOfTasks, lis.getAddEventCount());
 
102
        assertEquals("too few job task del events", numberOfTasks - 1, lis.getDelEventCount());
 
103
    }
 
104
    
 
105
    
 
106
    class JobTaskEventListener implements EventListener {
 
107
        
 
108
        private final Object syncObj = new Object();
 
109
        private int addEventCount = 0;
 
110
        private int delEventCount = 0;
 
111
        private boolean finished = false;
 
112
        private int jobid;
 
113
        
 
114
        public JobTaskEventListener(int jobid) {
 
115
            this.jobid = jobid;
 
116
        }
 
117
        
 
118
        public void eventOccured(Event evt) {
 
119
            
 
120
            if (evt instanceof JobTaskAddEvent) {
 
121
                JobTaskAddEvent jte = (JobTaskAddEvent) evt;
 
122
                if (jte.getJobId() == jobid) {
 
123
                    addEventCount++;
 
124
                    logger.info("task " + jte.getJobId() + "." + jte.getTaskNumber() + " started");
 
125
                }
 
126
            } else if (evt instanceof JobTaskDelEvent) {
 
127
                JobTaskDelEvent tde = (JobTaskDelEvent) evt;
 
128
                if (tde.getJobId() == jobid) {
 
129
                    delEventCount++;
 
130
                    logger.info("task " + tde.getJobId() + "." + tde.getTaskNumber() + " deleted");
 
131
                }
 
132
            } else if (evt instanceof JobDelEvent) {
 
133
                JobDelEvent jde = (JobDelEvent) evt;
 
134
                if (jde.getJobNumber() == jobid) {
 
135
                    logger.info("got job delete event: " + jde);
 
136
                    synchronized (syncObj) {
 
137
                        finished = true;
 
138
                        syncObj.notifyAll();
 
139
                    }
 
140
                }
 
141
            }
 
142
        }
 
143
        
 
144
        public boolean waitForJobFinish(int timeout) throws InterruptedException {
 
145
            long end = System.currentTimeMillis() + (timeout * 1000);
 
146
            synchronized (syncObj) {
 
147
                while (!finished) {
 
148
                    long waittime = end - System.currentTimeMillis();
 
149
                    if (waittime < 0) {
 
150
                        logger.info("timeout while waiting for final usage event");
 
151
                        return false;
 
152
                    }
 
153
                    syncObj.wait(waittime);
 
154
                }
 
155
            }
 
156
            return true;
 
157
        }
 
158
        
 
159
        public int getAddEventCount() {
 
160
            return addEventCount;
 
161
        }
 
162
        
 
163
        public int getDelEventCount() {
 
164
            return delEventCount;
 
165
        }
 
166
    }
 
167
    
 
168
    public static Test suite() {
 
169
        TestSuite suite = new TestSuite(JobTaskEventTestCase.class);
 
170
        return suite;
 
171
    }
 
172
}