use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CallbackTest method cancelViaShouldReRun.
@Test
public void cancelViaShouldReRun() throws Throwable {
JobManagerCallback callback = mock(JobManagerCallback.class);
final PublicJob job = spy(new PublicJob(new Params(0)));
doNothing().when(job).onAdded();
doThrow(new Exception()).when(job).onRun();
doReturn(3).when(job).getRetryLimit();
doReturn(RetryConstraint.CANCEL).when(job).shouldReRunOnThrowable(any(Throwable.class), anyInt(), anyInt());
final JobManager jobManager = createJobManager();
jobManager.addCallback(callback);
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
jobManager.addJob(job);
}
@Override
public void assertJob(Job job) {
}
});
verify(callback).onJobAdded(job);
verify(callback, times(1)).onJobRun(job, JobManagerCallback.RESULT_CANCEL_CANCELLED_VIA_SHOULD_RE_RUN);
verify(callback).onJobCancelled(eq(job), eq(false), any(Throwable.class));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CallbackTest method cancelViaCancelCall.
@Test
public void cancelViaCancelCall() throws Throwable {
JobManagerCallback callback = mock(JobManagerCallback.class);
final CountDownLatch startLatch = new CountDownLatch(1);
final CountDownLatch endLatch = new CountDownLatch(1);
final Throwable[] jobError = new Throwable[1];
PublicJob job = spy(new PublicJob(new Params(1).addTags("tag1")) {
@Override
public void onRun() throws Throwable {
startLatch.countDown();
try {
Assert.assertThat(endLatch.await(30, TimeUnit.SECONDS), CoreMatchers.is(true));
} catch (Throwable t) {
jobError[0] = t;
}
throw new Exception("blah");
}
});
doCallRealMethod().when(job).onRun();
doReturn(3).when(job).getRetryLimit();
JobManager jobManager = createJobManager();
jobManager.addCallback(callback);
jobManager.addJob(job);
Assert.assertThat(startLatch.await(30, TimeUnit.SECONDS), CoreMatchers.is(true));
jobManager.cancelJobsInBackground(null, TagConstraint.ANY, "tag1");
//noinspection StatementWithEmptyBody
while (!job.isCancelled()) {
// busy wait until cancel arrives
//noinspection SLEEP_IN_CODE
Thread.sleep(100);
}
endLatch.countDown();
while (jobManager.getJobStatus(job.getId()) != JobStatus.UNKNOWN) {
// busy wait until job cancel is handled
//noinspection SLEEP_IN_CODE
Thread.sleep(100);
}
MatcherAssert.assertThat(jobError[0], CoreMatchers.nullValue());
verify(job, times(0)).shouldReRunOnThrowable(any(Throwable.class), anyInt(), anyInt());
jobManager.stopAndWaitUntilConsumersAreFinished();
verify(callback).onJobAdded(job);
verify(callback, times(1)).onJobRun(job, JobManagerCallback.RESULT_CANCEL_CANCELLED_WHILE_RUNNING);
verify(callback).onJobCancelled(job, true, null);
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CallbackTest method successNonPersistent.
@Test
public void successNonPersistent() throws Throwable {
JobManagerCallback callback = mock(JobManagerCallback.class);
final Job job = spy(new PublicJob(new Params(0)));
doNothing().when(job).onAdded();
doNothing().when(job).onRun();
final JobManager jobManager = createJobManager();
jobManager.addCallback(callback);
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
jobManager.addJob(job);
}
@Override
public void assertJob(Job job) {
}
});
verify(job).onAdded();
verify(job).onRun();
verify(callback).onJobAdded(job);
verify(callback).onJobRun(job, JobManagerCallback.RESULT_SUCCEED);
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CallbackTest method cancelViaRetryLimit.
@Test
public void cancelViaRetryLimit() throws Throwable {
final Throwable error = new Exception();
JobManagerCallback callback = mock(JobManagerCallback.class);
final PublicJob job = spy(new PublicJob(new Params(0)));
doNothing().when(job).onAdded();
doThrow(error).when(job).onRun();
doReturn(3).when(job).getRetryLimit();
doReturn(RetryConstraint.RETRY).when(job).shouldReRunOnThrowable(any(Throwable.class), anyInt(), anyInt());
final JobManager jobManager = createJobManager();
jobManager.addCallback(callback);
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
jobManager.addJob(job);
}
@Override
public void assertJob(Job job) {
}
});
verify(callback).onJobAdded(job);
verify(callback, times(2)).onJobRun(job, JobManagerCallback.RESULT_FAIL_WILL_RETRY);
verify(callback, times(1)).onJobRun(job, JobManagerCallback.RESULT_CANCEL_REACHED_RETRY_LIMIT);
verify(callback).onJobCancelled(job, false, error);
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CancelBeforeRunningTest method testCancelBeforeRunning.
@Test
public void testCancelBeforeRunning() {
JobManager jobManager = createJobManager();
jobManager.stop();
DummyJob nonPersistentJob = new DummyJob(new Params(0).addTags("dummyTag"));
DummyJob persistentJob = new DummyJob(new Params(0).addTags("dummyTag").persist());
jobManager.addJob(nonPersistentJob);
jobManager.addJob(persistentJob);
CancelResult result = jobManager.cancelJobs(TagConstraint.ANY, "dummyTag");
assertThat("both jobs should be cancelled", result.getCancelledJobs().size(), is(2));
assertThat("both jobs should be cancelled", result.getFailedToCancel().size(), is(0));
for (Job j : result.getCancelledJobs()) {
DummyJob job = (DummyJob) j;
if (!job.isPersistent()) {
assertThat("job is still added", job.getOnAddedCnt(), is(1));
}
assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
assertThat("job is NOT run", job.getOnRunCnt(), is(0));
}
}
Aggregations