1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
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
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.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
32
package com.sun.grid.jgdi.event;
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;
43
import java.util.HashMap;
45
import java.util.logging.Level;
46
import junit.framework.Test;
47
import junit.framework.TestSuite;
52
public class PETaskEventTestCase extends BaseTestCase {
55
private EventClient evc;
57
/** Creates a new instance of PETaskEventTestCase */
58
public PETaskEventTestCase(String testName) {
62
private ParallelEnvironment pe;
63
private ClusterQueue queue;
65
protected void setUp() throws Exception {
68
evc = createEventClient(0);
71
String peName = "pe" + System.currentTimeMillis();
73
pe = new ParallelEnvironmentImpl();
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");
83
jgdi.addParallelEnvironment(pe);
85
queue = jgdi.getClusterQueue("all.q");
87
String queueName = peName + ".q";
88
queue.setName(queueName);
89
queue.putJobSlots("@/", 2);
91
queue.addDefaultPe(peName);
92
jgdi.addClusterQueue(queue);
94
logger.fine("SetUp done");
97
protected void tearDown() throws Exception {
100
jgdi.deleteClusterQueue(queue);
101
} catch (JGDIException ex) {
102
logger.log(Level.WARNING, ex.getLocalizedMessage(), ex);
107
jgdi.deleteParallelEnvironment(pe);
108
} catch (JGDIException ex) {
109
logger.log(Level.WARNING, ex.getLocalizedMessage(), ex);
119
public void testPETaskEvents() throws Exception {
122
jgdi.disableQueues(new String[]{queue.getName()}, false);
124
int numberOfTasks = 2;
125
int taskRuntime = 10;
127
File peJobFile = new File("util/scripts/pe_job.sh");
128
File peTaskFile = new File("util/scripts/pe_task.sh");
130
int jobid = JobSubmitter.submitJob(getCurrentCluster(), new String[]{"-pe", pe.getName(), Integer.toString(numberOfTasks), peJobFile.getAbsolutePath(), peTaskFile.getAbsolutePath(), Integer.toString(numberOfTasks), Integer.toString(taskRuntime)});
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);
137
evc.subscribe(map.keySet());
140
PETaskEventListener lis = new PETaskEventListener(jobid);
141
evc.addEventListener(lis);
147
jgdi.enableQueues(new String[]{queue.getName()}, false);
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());
156
class PETaskEventListener implements EventListener {
158
private final Object finishSync = new Object();
159
private int addEventCount = 0;
160
private int delEventCount = 0;
162
private boolean finished = false;
164
public PETaskEventListener(int jobid) {
168
public void eventOccured(Event evt) {
170
if (evt instanceof PETaskAddEvent) {
171
PETaskAddEvent jte = (PETaskAddEvent) evt;
172
if (jte.getJobId() == jobid) {
174
logger.info("pe task " + jte.getJobId() + "." + jte.getTaskNumber() + " started");
176
} else if (evt instanceof PETaskDelEvent) {
177
PETaskDelEvent tde = (PETaskDelEvent) evt;
178
if (tde.getJobId() == jobid) {
180
logger.info("pe task " + tde.getJobId() + "." + tde.getTaskNumber() + " deleted");
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) {
188
finishSync.notifyAll();
194
public boolean waitForJobFinish(int timeout) throws InterruptedException {
195
long end = System.currentTimeMillis() + (timeout * 1000);
196
synchronized (finishSync) {
198
long waittime = end - System.currentTimeMillis();
200
logger.info("timeout while waiting for final usage event");
203
finishSync.wait(waittime);
209
public int getAddEventCount() {
210
return addEventCount;
213
public int getDelEventCount() {
214
return delEventCount;
218
public static Test suite() {
219
TestSuite suite = new TestSuite(PETaskEventTestCase.class);