Skip to content

Commit

Permalink
Add test to JobGroupTest
Browse files Browse the repository at this point in the history
Add the test method
org.eclipse.core.tests.runtime.jobs.JobGroupTest.testShouldCancel_5().

See discussion:
#654
  • Loading branch information
fedejeanne committed Sep 19, 2023
1 parent 00e8ea7 commit baa0a8e
Showing 1 changed file with 98 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,36 @@

package org.eclipse.core.tests.runtime.jobs;

import static org.hamcrest.number.OrderingComparison.comparesEqualTo;
import static org.junit.Assert.assertNotEquals;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.concurrent.atomic.AtomicLong;
import junit.framework.AssertionFailedError;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.jobs.*;
import org.eclipse.core.tests.harness.*;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.IJobManager;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.core.runtime.jobs.JobChangeAdapter;
import org.eclipse.core.runtime.jobs.JobGroup;
import org.eclipse.core.tests.harness.FussyProgressMonitor;
import org.eclipse.core.tests.harness.TestBarrier2;
import org.eclipse.core.tests.harness.TestJob;
import org.hamcrest.MatcherAssert;

/**
* Tests for {@link JobGroup}.
Expand Down Expand Up @@ -1293,6 +1313,80 @@ public IStatus run(IProgressMonitor monitor) {
assertTrue("2.0", numShouldCancelCalled[0] < NUM_JOBS_LIMIT + 10);
}

/**
* Tested scenario: - 2 jobs are started in parallel and the first one returns a
* CANCEL_STATUS while the second one is still running. This lets
* <code>JobGroup.shouldCancel</code> return <code>true</code> which triggers
* the cancelation of any running job.
*
* Expected result: The second job is explicitly canceled
*
*/
public void testShouldCancel_5() {
final CountDownLatch readyToRun = new CountDownLatch(2);
final CountDownLatch startRunning = new CountDownLatch(1);
final CountDownLatch explicitlyCanceled = new CountDownLatch(1);

// the job group allows for 2 threads so both jobs will be started in
// parallel
final JobGroup jobGroup = new JobGroup("JobGroup", 2, 2) {
@Override
protected boolean shouldCancel(IStatus lastCompletedJobResult, int numberOfFailedJobs,
int numberOfCanceledJobs) {
// cancel the group as soon as one job in it returns the CANCEL_STATUS
return numberOfCanceledJobs > 0;
}
};

Job returnsCancelStatusJob = Job.create("The one that cancels", __ -> {
readyToRun.countDown();
awaitLatch(startRunning);

return Status.CANCEL_STATUS;
});

Job canceledJob = new Job("The one that is explicitly canceled") {
@Override
protected IStatus run(IProgressMonitor monitor) {
readyToRun.countDown();
awaitLatch(startRunning);

// give it time: this Job needs to be running at the time the other job finishes
awaitLatch(explicitlyCanceled);
return Status.OK_STATUS;
}

@Override
protected void canceling() {
explicitlyCanceled.countDown();
}
};

returnsCancelStatusJob.setJobGroup(jobGroup);
returnsCancelStatusJob.schedule();

canceledJob.setJobGroup(jobGroup);
canceledJob.schedule();

// Wait until both jobs are ready to run and let them start running
awaitLatch(readyToRun);
startRunning.countDown();

waitForCompletion(jobGroup);

MatcherAssert.assertThat("The second job should have been explicitly canceled", //
explicitlyCanceled.getCount(), comparesEqualTo(0L));
}

private void awaitLatch(final CountDownLatch latch) {
try {
latch.await(10_000, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
fail("", e);
}
}


public void testDefaultComputeGroupResult() {
final int status[] = {IStatus.OK, IStatus.INFO, IStatus.WARNING, IStatus.ERROR, IStatus.CANCEL};
final JobGroup jobGroup = new JobGroup("JobGroup", 1, status.length) {
Expand Down

0 comments on commit baa0a8e

Please sign in to comment.