Skip to content

Commit

Permalink
Fix random failing JobTest#testSetRule eclipse-platform#1038
Browse files Browse the repository at this point in the history
The test case JobTest#testSetRule randomly fails because the used
short-running job, on which a rule is set, may finish before the test
attempts to set a rule on it.

This change rewrites the test to:
- Use a long-running job to ensure that it does not finish too early
- Properly wait for the job to start running once it has been woken up
- Use proper assertions with helpful error messages
- Does not silently proceed once an unexpected exception occurred.

Fixes eclipse-platform#1038
  • Loading branch information
HeikoKlare committed Dec 18, 2023
1 parent ea80630 commit fb17185
Showing 1 changed file with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1665,31 +1665,28 @@ protected IStatus run(IProgressMonitor monitor) {
* see bug #43459
*/
@Test
public void testSetRule() {
public void testSetRule() throws InterruptedException {
//setting a scheduling rule for a job after it was already scheduled should throw an exception
shortJob.setRule(new IdentityRule());
assertTrue("1.0", shortJob.getRule() instanceof IdentityRule);
shortJob.schedule(1000000);
assertThrows(RuntimeException.class, () -> shortJob.setRule(new PathRule("/testSetRule")));
longJob.setRule(new IdentityRule());
assertThat(longJob.getRule(), instanceOf(IdentityRule.class));
longJob.schedule(1000000);
assertThrows(RuntimeException.class, () -> longJob.setRule(new PathRule("/testSetRule")));

//wake up the sleeping job
shortJob.wakeUp();
longJob.wakeUp();
waitForState(longJob, Job.RUNNING);

//setting the rule while running should fail
assertThrows(RuntimeException.class, () -> shortJob.setRule(new PathRule("/testSetRule/B")));
assertThrows(RuntimeException.class, () -> longJob.setRule(new PathRule("/testSetRule/B")));

try {
//wait for the job to complete
shortJob.join();
} catch (InterruptedException e2) {
//ignore
}
longJob.cancel();
longJob.join();

//after the job has finished executing, the scheduling rule for it can once again be reset
shortJob.setRule(new PathRule("/testSetRule/B/C/D"));
assertTrue("1.2", shortJob.getRule() instanceof PathRule);
shortJob.setRule(null);
assertNull("1.3", shortJob.getRule());
longJob.setRule(new PathRule("/testSetRule/B/C/D"));
assertThat(longJob.getRule(), instanceOf(PathRule.class));
longJob.setRule(null);
assertNull("job still has a rule assigned: " + longJob, longJob.getRule());
}

@Test
Expand Down

0 comments on commit fb17185

Please sign in to comment.