|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2012 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* ivan.motsch@bsiag.com - initial example |
| 10 |
* IBM Corporation - converted to JUnit test |
| 11 |
*******************************************************************************/ |
| 12 |
package org.eclipse.core.tests.runtime.jobs; |
| 13 |
|
| 14 |
import java.util.concurrent.Semaphore; |
| 15 |
import org.eclipse.core.runtime.*; |
| 16 |
import org.eclipse.core.runtime.jobs.Job; |
| 17 |
|
| 18 |
/** |
| 19 |
* Tests that scheduling multiple jobs with a delay and then blocking all active |
| 20 |
* worker threads doesn't cause job manager to starve. This is a regression |
| 21 |
* test for a situation where all worker threads were blocked and no workers |
| 22 |
* were available to process sleeping jobs awaiting execution. For details see: |
| 23 |
* https://bugs.eclipse.org/bugs/show_bug.cgi?id=366170 |
| 24 |
*/ |
| 25 |
public class Bug_366170 extends AbstractJobManagerTest { |
| 26 |
private Semaphore m_jobBStopHint = new Semaphore(1); |
| 27 |
|
| 28 |
public void testBug() throws Exception { |
| 29 |
System.out.println("--- Running the examle ---"); |
| 30 |
m_jobBStopHint.acquire(); |
| 31 |
scheduleJobA(200); |
| 32 |
scheduleJobC(300); |
| 33 |
|
| 34 |
Thread.sleep(2000L); |
| 35 |
|
| 36 |
//lock should now be free if C is finished |
| 37 |
assertTrue("Failed: Job C was not run", m_jobBStopHint.tryAcquire()); |
| 38 |
} |
| 39 |
|
| 40 |
private void scheduleJobA(long delay) throws Exception { |
| 41 |
Job job = new Job("A") { |
| 42 |
@Override |
| 43 |
public boolean shouldSchedule() { |
| 44 |
System.out.println("schedule " + getName()); |
| 45 |
return true; |
| 46 |
} |
| 47 |
|
| 48 |
protected IStatus run(IProgressMonitor monitor) { |
| 49 |
System.out.println("begin " + getName()); |
| 50 |
try { |
| 51 |
scheduleJobB(0).join(); |
| 52 |
} catch (Throwable t) { |
| 53 |
t.printStackTrace(); |
| 54 |
} |
| 55 |
System.out.println("end " + getName()); |
| 56 |
return Status.OK_STATUS; |
| 57 |
} |
| 58 |
}; |
| 59 |
job.setSystem(true); |
| 60 |
job.schedule(delay); |
| 61 |
} |
| 62 |
|
| 63 |
private Job scheduleJobB(long delay) throws Exception { |
| 64 |
Job job = new Job("B") { |
| 65 |
@Override |
| 66 |
public boolean shouldSchedule() { |
| 67 |
System.out.println("schedule " + getName()); |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
protected IStatus run(IProgressMonitor monitor) { |
| 73 |
System.out.println("begin " + getName()); |
| 74 |
try { |
| 75 |
m_jobBStopHint.acquire(); |
| 76 |
} catch (InterruptedException e) { |
| 77 |
e.printStackTrace(); |
| 78 |
} |
| 79 |
m_jobBStopHint.release(); |
| 80 |
System.out.println("end " + getName()); |
| 81 |
return Status.OK_STATUS; |
| 82 |
} |
| 83 |
}; |
| 84 |
job.setSystem(true); |
| 85 |
job.schedule(delay); |
| 86 |
return job; |
| 87 |
} |
| 88 |
|
| 89 |
private void scheduleJobC(long delay) throws Exception { |
| 90 |
Job job = new Job("C") { |
| 91 |
@Override |
| 92 |
public boolean shouldSchedule() { |
| 93 |
System.out.println("schedule " + getName()); |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
protected IStatus run(IProgressMonitor monitor) { |
| 99 |
System.out.println("begin " + getName()); |
| 100 |
m_jobBStopHint.release(); |
| 101 |
System.out.println("end " + getName()); |
| 102 |
return Status.OK_STATUS; |
| 103 |
} |
| 104 |
}; |
| 105 |
job.setSystem(true); |
| 106 |
job.schedule(delay); |
| 107 |
} |
| 108 |
|
| 109 |
} |