~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/PETaskEventTestCase.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.JGDIException;
 
38
import com.sun.grid.jgdi.JobSubmitter;
 
39
import com.sun.grid.jgdi.configuration.ClusterQueue;
 
40
import com.sun.grid.jgdi.configuration.ParallelEnvironment;
 
41
import com.sun.grid.jgdi.configuration.ParallelEnvironmentImpl;
 
42
import java.io.File;
 
43
import java.util.HashMap;
 
44
import java.util.Map;
 
45
import java.util.logging.Level;
 
46
import junit.framework.Test;
 
47
import junit.framework.TestSuite;
 
48
 
 
49
/**
 
50
 *
 
51
 */
 
52
public class PETaskEventTestCase extends BaseTestCase {
 
53
    
 
54
    private JGDI jgdi;
 
55
    private EventClient evc;
 
56
    
 
57
    /** Creates a new instance of PETaskEventTestCase */
 
58
    public PETaskEventTestCase(String testName) {
 
59
        super(testName);
 
60
    }
 
61
    
 
62
    private ParallelEnvironment pe;
 
63
    private ClusterQueue queue;
 
64
    
 
65
    protected void setUp() throws Exception {
 
66
        
 
67
        jgdi = createJGDI();
 
68
        evc = createEventClient(0);
 
69
        super.setUp();
 
70
        
 
71
        String peName = "pe" + System.currentTimeMillis();
 
72
        
 
73
        pe = new ParallelEnvironmentImpl();
 
74
        pe.setName(peName);
 
75
        pe.setSlots(999);
 
76
        pe.setStartProcArgs("/bin/true");
 
77
        pe.setStopProcArgs("/bin/true");
 
78
        pe.setAllocationRule("$pe_slots");
 
79
        pe.setControlSlaves(true);
 
80
        pe.setJobIsFirstTask(true);
 
81
        pe.setUrgencySlots("min");
 
82
        
 
83
        jgdi.addParallelEnvironment(pe);
 
84
        
 
85
        queue = jgdi.getClusterQueue("all.q");
 
86
        
 
87
        String queueName = peName + ".q";
 
88
        queue.setName(queueName);
 
89
        queue.putJobSlots("@/", 2);
 
90
        queue.removeAllPe();
 
91
        queue.addDefaultPe(peName);
 
92
        jgdi.addClusterQueue(queue);
 
93
        
 
94
        logger.fine("SetUp done");
 
95
    }
 
96
    
 
97
    protected void tearDown() throws Exception {
 
98
        if (queue != null) {
 
99
            try {
 
100
                jgdi.deleteClusterQueue(queue);
 
101
            } catch (JGDIException ex) {
 
102
                logger.log(Level.WARNING, ex.getLocalizedMessage(), ex);
 
103
            }
 
104
        }
 
105
        if (pe != null) {
 
106
            try {
 
107
                jgdi.deleteParallelEnvironment(pe);
 
108
            } catch (JGDIException ex) {
 
109
                logger.log(Level.WARNING, ex.getLocalizedMessage(), ex);
 
110
            }
 
111
        }
 
112
        try {
 
113
            evc.close();
 
114
        } finally {
 
115
            jgdi.close();
 
116
        }
 
117
    }
 
118
    
 
119
    public void testPETaskEvents() throws Exception {
 
120
        
 
121
        
 
122
        jgdi.disableQueues(new String[]{queue.getName()}, false);
 
123
        
 
124
        int numberOfTasks = 2;
 
125
        int taskRuntime = 10;
 
126
        
 
127
        File peJobFile = new File("util/scripts/pe_job.sh");
 
128
        File peTaskFile = new File("util/scripts/pe_task.sh");
 
129
        
 
130
        int jobid = JobSubmitter.submitJob(getCurrentCluster(), new String[]{"-pe", pe.getName(), Integer.toString(numberOfTasks), peJobFile.getAbsolutePath(), peTaskFile.getAbsolutePath(), Integer.toString(numberOfTasks), Integer.toString(taskRuntime)});
 
131
        
 
132
        Map<EventTypeEnum,Integer> map = new HashMap<EventTypeEnum,Integer>();
 
133
        map.put(EventTypeEnum.PETaskAdd, 1);
 
134
        map.put(EventTypeEnum.PETaskDel, 1);
 
135
        map.put(EventTypeEnum.JobDel, 1);
 
136
        
 
137
        evc.subscribe(map.keySet());
 
138
        evc.setFlush(map);
 
139
        
 
140
        PETaskEventListener lis = new PETaskEventListener(jobid);
 
141
        evc.addEventListener(lis);
 
142
        
 
143
        evc.commit();
 
144
        
 
145
        Thread.sleep(2);
 
146
        
 
147
        jgdi.enableQueues(new String[]{queue.getName()}, false);
 
148
        
 
149
        
 
150
        assertTrue("timeout while waiting for job finish event", lis.waitForJobFinish(taskRuntime * 10));
 
151
        assertEquals("Too few pe task add events", numberOfTasks, lis.getAddEventCount());
 
152
        assertEquals("Too few pe task del events", numberOfTasks - 1, lis.getDelEventCount());
 
153
    }
 
154
    
 
155
    
 
156
    class PETaskEventListener implements EventListener {
 
157
        
 
158
        private final Object finishSync = new Object();
 
159
        private int addEventCount = 0;
 
160
        private int delEventCount = 0;
 
161
        private int jobid;
 
162
        private boolean finished = false;
 
163
        
 
164
        public PETaskEventListener(int jobid) {
 
165
            this.jobid = jobid;
 
166
        }
 
167
        
 
168
        public void eventOccured(Event evt) {
 
169
            
 
170
            if (evt instanceof PETaskAddEvent) {
 
171
                PETaskAddEvent jte = (PETaskAddEvent) evt;
 
172
                if (jte.getJobId() == jobid) {
 
173
                    addEventCount++;
 
174
                    logger.info("pe task " + jte.getJobId() + "." + jte.getTaskNumber() + " started");
 
175
                }
 
176
            } else if (evt instanceof PETaskDelEvent) {
 
177
                PETaskDelEvent tde = (PETaskDelEvent) evt;
 
178
                if (tde.getJobId() == jobid) {
 
179
                    delEventCount++;
 
180
                    logger.info("pe task " + tde.getJobId() + "." + tde.getTaskNumber() + " deleted");
 
181
                }
 
182
            } else if (evt instanceof JobDelEvent) {
 
183
                JobDelEvent jde = (JobDelEvent) evt;
 
184
                if (jde.getJobNumber() == jobid) {
 
185
                    logger.info("got job delete event: " + jde);
 
186
                    synchronized (finishSync) {
 
187
                        finished = true;
 
188
                        finishSync.notifyAll();
 
189
                    }
 
190
                }
 
191
            }
 
192
        }
 
193
        
 
194
        public boolean waitForJobFinish(int timeout) throws InterruptedException {
 
195
            long end = System.currentTimeMillis() + (timeout * 1000);
 
196
            synchronized (finishSync) {
 
197
                while (!finished) {
 
198
                    long waittime = end - System.currentTimeMillis();
 
199
                    if (waittime < 0) {
 
200
                        logger.info("timeout while waiting for final usage event");
 
201
                        return false;
 
202
                    }
 
203
                    finishSync.wait(waittime);
 
204
                }
 
205
            }
 
206
            return true;
 
207
        }
 
208
        
 
209
        public int getAddEventCount() {
 
210
            return addEventCount;
 
211
        }
 
212
        
 
213
        public int getDelEventCount() {
 
214
            return delEventCount;
 
215
        }
 
216
    }
 
217
    
 
218
    public static Test suite() {
 
219
        TestSuite suite = new TestSuite(PETaskEventTestCase.class);
 
220
        return suite;
 
221
    }
 
222
}