use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class HitDeadlineAfterException method failAndHitDeadline.
@Test
public void failAndHitDeadline() throws InterruptedException {
final JobManager jobManager = createJobManager();
final AtomicBoolean calledShouldReRun = new AtomicBoolean(false);
final AtomicInteger reason = new AtomicInteger();
final DummyJob job = new DummyJob(new Params(0).overrideDeadlineToCancelInMs(100)) {
@Override
public void onRun() throws Throwable {
super.onRun();
mockTimer.incrementMs(150);
throw new RuntimeException("why not fail");
}
@Override
protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
reason.set(cancelReason);
super.onCancel(cancelReason, throwable);
}
@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
calledShouldReRun.set(true);
return RetryConstraint.RETRY;
}
};
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
jobManager.addJob(job);
}
@Override
public void assertJob(Job job) {
}
});
MatcherAssert.assertThat(reason.get(), CoreMatchers.is(CancelReason.REACHED_DEADLINE));
MatcherAssert.assertThat(calledShouldReRun.get(), CoreMatchers.is(false));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CallbackTest method successPersistent.
@Test
public void successPersistent() throws Throwable {
JobManagerCallback callback = mock(JobManagerCallback.class);
final Job job = new PersistentDummyJob();
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(any(PersistentDummyJob.class));
verify(callback).onJobRun(any(PersistentDummyJob.class), eq(JobManagerCallback.RESULT_SUCCEED));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CancelBeforeRunningTest method testCancelBeforeRunningWithGroups.
@Test
public void testCancelBeforeRunningWithGroups() throws InterruptedException {
JobManager jobManager = createJobManager();
jobManager.stop();
DummyJob nonPersistentJob = new DummyJob(new Params(0).addTags("dummyTag").groupBy("group1"));
DummyJob persistentJob = new DummyJob(new Params(0).addTags("dummyTag").persist().groupBy("group2"));
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));
}
assertThat("there should not be any jobs in the queue", jobManager.count(), is(0));
jobManager.start();
DummyJob nonPersistentJob2 = new DummyJob(new Params(0).addTags("dummyTag").groupBy("group1")) {
@Override
public void onRun() throws Throwable {
super.onRun();
nonPersistentJobLatch.countDown();
}
};
DummyJob persistentJob2 = new PersistentDummyJob(new Params(0).addTags("dummyTag").groupBy("group2"));
jobManager.addJob(persistentJob2);
jobManager.addJob(nonPersistentJob2);
assertThat("a new job in the same group with canceled job should run", nonPersistentJobLatch.await(1, TimeUnit.SECONDS), is(true));
assertThat("a new persistent job in the same group with canceled job should run", persistentJobLatch.await(2, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CancelFailingJobsTest method testCancelWithoutNetworkPersistent.
public void testCancelWithoutNetworkPersistent(boolean async, TagConstraint constraint) throws InterruptedException {
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil));
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
jobManager.addJob(new DummyJob(new Params(1).persist().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(2).persist().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(3).persist().groupBy("group").addTags("tag")));
final CancelResult[] result = new CancelResult[1];
if (async) {
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
result[0] = cancelResult;
cancelLatch.countDown();
}
}, constraint, "tag");
cancelLatch.await(2, TimeUnit.SECONDS);
} else {
result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
}
assertThat("all jobs should be canceled", result[0].getCancelledJobs().size(), is(3));
assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
final CountDownLatch runLatch = persistentLatches[latchCounter++];
jobManager.addJob(new PersistentDummyJob(new Params(3).persist().groupBy("group").addTags("tag"), latchCounter - 1));
networkUtil.setNetworkStatus(NetworkUtil.METERED, true);
assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class CancelFailingJobsTest method testCancelWithoutNetwork.
public void testCancelWithoutNetwork(boolean async, TagConstraint constraint) throws InterruptedException {
final int jobCount = 30;
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil).timer(mockTimer));
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
for (int i = 0; i < jobCount; i++) {
jobManager.addJob(new FailingJob(new Params(i).groupBy("group").addTags("tag")));
}
final CancelResult[] result = new CancelResult[1];
if (async) {
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
result[0] = cancelResult;
cancelLatch.countDown();
}
}, constraint, "tag");
assertThat(cancelLatch.await(jobCount, TimeUnit.SECONDS), is(true));
} else {
result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
}
assertThat("all jobs should be cancelled", result[0].getCancelledJobs().size(), is(jobCount));
assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
final CountDownLatch runLatch = new CountDownLatch(1);
jobManager.addJob(new DummyJob(new Params(1).groupBy("group").addTags("tag")) {
@Override
public void onRun() throws Throwable {
super.onRun();
runLatch.countDown();
}
});
networkUtil.setNetworkStatus(NetworkUtil.UNMETERED, true);
assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
Aggregations