Skip to content

Commit 583ce4a

Browse files
committed
Fix AbstractJobTest#waitForCompletion() and Bug_32039#testBug() #1115
The test case Bug_32039#testBug() randomly fails because * AbstractJobTest#waitForCompletion() is can succeed even if the job is not complete and * Bug_32039#testBug() does not ensure a proper execution order of rule acquisitions This change fixes the waitForCompletion() method in terms of enforcing a Duration instead of an integer to be passed as a timeout to avoid faulty units and in terms of really throwing an exception of the job does not complete (in time). It also ensures that the rule acquisition in the Bug_32039#testBug() method always happens in the same order. Since the correction of waitForCompletion() reveals that IJobManagerTest#testBug57656() and IJobManagerTest#testScheduleRace() contained bugs that made the test rely on waitForCompletion() not working properly, they are fixed as well by correcting the job execution times and terminate conditions. Fixes #1115
1 parent e9f9536 commit 583ce4a

File tree

5 files changed

+50
-30
lines changed

5 files changed

+50
-30
lines changed

runtime/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/AbstractJobTest.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
*******************************************************************************/
1515
package org.eclipse.core.tests.runtime.jobs;
1616

17+
import static org.assertj.core.api.Assertions.assertThat;
1718
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
1919

20+
import java.time.Duration;
2021
import org.eclipse.core.internal.jobs.JobListeners;
2122
import org.eclipse.core.internal.jobs.JobManager;
2223
import org.eclipse.core.runtime.jobs.IJobManager;
@@ -43,28 +44,25 @@ protected void sleep(long duration) {
4344

4445
/**
4546
* Ensures job completes within the given time.
46-
* @param waitTime time in milliseconds
4747
*/
48-
protected void waitForCompletion(Job job, int waitTime) {
49-
int i = 0;
50-
int tickLength = 1;
51-
int ticks = waitTime / tickLength;
52-
long start = now();
53-
while (job.getState() != Job.NONE && now() - start < waitTime) {
54-
sleep(tickLength);
55-
// sanity test to avoid hanging tests
56-
if (i++ > ticks && now() - start > waitTime) {
57-
dumpState();
58-
assertTrue("Timeout waiting for job to complete", false);
59-
}
48+
protected void waitForCompletion(Job job, Duration timeoutDuration) {
49+
Duration startTime = Duration.ofMillis(now());
50+
Duration timeout = startTime.plus(timeoutDuration);
51+
while (job.getState() != Job.NONE && !timeout.minusMillis(now()).isNegative()) {
52+
Thread.yield();
53+
}
54+
int finalJobState = job.getState();
55+
if (finalJobState != Job.NONE) {
56+
dumpState();
57+
assertThat(finalJobState).as("timeout waiting for job to complete").isEqualTo(Job.NONE);
6058
}
6159
}
6260

6361
/**
6462
* Ensures given job completes within a second.
6563
*/
6664
protected void waitForCompletion(Job job) {
67-
waitForCompletion(job, 1000);
65+
waitForCompletion(job, Duration.ofSeconds(1));
6866
}
6967

7068
/**

runtime/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/Bug_320329.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
*******************************************************************************/
1414
package org.eclipse.core.tests.runtime.jobs;
1515

16+
import java.time.Duration;
17+
import org.eclipse.core.runtime.IProgressMonitor;
18+
import org.eclipse.core.runtime.IStatus;
1619
import org.eclipse.core.runtime.NullProgressMonitor;
1720
import org.eclipse.core.runtime.jobs.ISchedulingRule;
1821
import org.eclipse.core.runtime.jobs.Job;
1922
import org.eclipse.core.runtime.jobs.MultiRule;
23+
import org.eclipse.core.tests.harness.TestBarrier2;
2024
import org.eclipse.core.tests.harness.TestJob;
2125
import org.junit.Test;
2226

@@ -27,22 +31,30 @@ public class Bug_320329 extends AbstractJobTest {
2731

2832
@Test
2933
public void testBug() {
34+
TestBarrier2 job2State = new TestBarrier2();
3035
Job j1 = new TestJob("job1", 10, 5);// 50 ms
31-
Job j2 = new TestJob("job2");
36+
Job j2 = new TestJob("job2") {
37+
@Override
38+
public IStatus run(IProgressMonitor monitor) {
39+
job2State.upgradeTo(TestBarrier2.STATUS_RUNNING);
40+
return super.run(monitor);
41+
}
42+
};
3243
ISchedulingRule rule1 = new IdentityRule();
3344
ISchedulingRule rule2 = new IdentityRule();
3445

3546
j1.setRule(rule1);
3647
j2.setRule(MultiRule.combine(rule1, rule2));
3748
j1.schedule();
3849
j2.schedule();
50+
job2State.waitForStatus(TestBarrier2.STATUS_RUNNING);
3951

4052
// busy wait here
4153
Job.getJobManager().beginRule(rule2, new NullProgressMonitor());
4254

4355
// Clean up
4456
Job.getJobManager().endRule(rule2);
45-
waitForCompletion(j1, 60);
46-
waitForCompletion(j2, 60);
57+
waitForCompletion(j1, Duration.ofMillis(60));
58+
waitForCompletion(j2, Duration.ofMillis(60));
4759
}
4860
}

runtime/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/IJobManagerTest.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static org.junit.Assert.assertThrows;
2222
import static org.junit.Assert.assertTrue;
2323

24+
import java.time.Duration;
2425
import java.util.ArrayList;
2526
import java.util.Arrays;
2627
import java.util.Collections;
@@ -397,16 +398,19 @@ public void testBug48073() {
397398
*/
398399
@Test
399400
public void testBug57656() {
400-
TestJob jobA = new TestJob("Job1");
401-
TestJob jobB = new TestJob("Job2");
401+
TestJob jobA = new TestJob("Job1", 10000, 10);
402+
TestJob jobB = new TestJob("Job2", 1, 1);
402403
//schedule jobA
403-
jobA.schedule(50);
404+
jobA.schedule(100);
404405
//schedule jobB so it gets behind jobA in the queue
405-
jobB.schedule(100);
406+
jobB.schedule(101);
406407
//now put jobA to sleep indefinitely
407408
jobA.sleep();
408-
//jobB should still run within ten seconds
409-
waitForCompletion(jobB, 300);
409+
// jobB should still run even though jobA scheduled before did not start
410+
waitForCompletion(jobB, Duration.ofSeconds(1));
411+
jobA.terminate();
412+
jobA.cancel();
413+
waitForCompletion(jobA, Duration.ofSeconds(5));
410414
}
411415

412416
/**
@@ -1744,10 +1748,13 @@ public void testReverseOrder() throws InterruptedException {
17441748
}
17451749

17461750
/**
1747-
* Tests conditions where there is a race to schedule the same job multiple times.
1751+
* Tests conditions where there is a race to schedule the same job multiple
1752+
* times.
1753+
*
1754+
* @throws InterruptedException
17481755
*/
17491756
@Test
1750-
public void testScheduleRace() {
1757+
public void testScheduleRace() throws InterruptedException {
17511758
final int[] count = new int[1];
17521759
final boolean[] running = new boolean[] {false};
17531760
final boolean[] failure = new boolean[] {false};
@@ -1784,7 +1791,8 @@ public void scheduled(IJobChangeEvent event) {
17841791
}
17851792
});
17861793
testJob.schedule();
1787-
waitForCompletion(testJob, 5000);
1794+
testJob.join();
1795+
waitForCompletion(testJob, Duration.ofSeconds(5));
17881796
assertTrue("1.0", !failure[0]);
17891797
}
17901798

runtime/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/JobGroupTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static org.junit.Assert.assertTrue;
2525
import static org.junit.Assert.fail;
2626

27+
import java.time.Duration;
2728
import java.util.ArrayList;
2829
import java.util.Arrays;
2930
import java.util.Collection;
@@ -1441,7 +1442,7 @@ public IStatus run(IProgressMonitor monitor) {
14411442
};
14421443
job.setJobGroup(jobGroup);
14431444
job.schedule();
1444-
waitForCompletion(job, 100);
1445+
waitForCompletion(job, Duration.ofMillis(100));
14451446

14461447
boolean completed = jobGroup.join(1000, null);
14471448
assertTrue("2.0", completed);

runtime/tests/org.eclipse.core.tests.runtime/src/org/eclipse/core/tests/runtime/jobs/YieldTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertThrows;
2020
import static org.junit.Assert.assertTrue;
2121

22+
import java.time.Duration;
2223
import java.util.ArrayList;
2324
import java.util.Arrays;
2425
import java.util.Collections;
@@ -430,7 +431,7 @@ public void run() {
430431
barrier.waitForStatus(TestBarrier2.STATUS_START);
431432
t.start();
432433

433-
waitForCompletion(yielding, 5000);
434+
waitForCompletion(yielding, Duration.ofSeconds(5));
434435
assertTrue(yielding.getResult().isOK());
435436
}
436437

@@ -518,7 +519,7 @@ protected IStatus run(IProgressMonitor monitor) {
518519
barrier.waitForStatus(TestBarrier2.STATUS_START);
519520
conflicting.schedule();
520521

521-
waitForCompletion(conflicting, 5000);
522+
waitForCompletion(conflicting, Duration.ofSeconds(5));
522523
assertTrue(conflicting.getResult().isOK());
523524
barrier.waitForStatus(TestBarrier2.STATUS_BLOCKED);
524525
}

0 commit comments

Comments
 (0)