use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class RetryLogicTest method testCancel.
public void testCancel(boolean persistent) throws InterruptedException {
canRun = true;
final Throwable[] retryThrowable = new Throwable[1];
final Throwable[] cancelThrowable = new Throwable[1];
retryProvider = new RetryProvider() {
@Override
public RetryConstraint build(Job job, Throwable throwable, int runCount, int maxRunCount) {
retryThrowable[0] = throwable;
return RetryConstraint.CANCEL;
}
};
onCancelCallback = new CancelCallback() {
@Override
public void on(Job job, @CancelReason int cancelReason, @Nullable Throwable throwable) {
assertThat("should call cancel only once", cancelThrowable[0], is(nullValue()));
cancelThrowable[0] = throwable;
}
};
RetryJob job = new RetryJob(new Params(1).setPersistent(persistent));
job.retryLimit = 3;
onRunLatch = new CountDownLatch(3);
createJobManager().addJob(job);
assertThat(onRunLatch.await(2, TimeUnit.SECONDS), is(false));
assertThat("it should run 1 time", runCount, is(1));
assertThat(cancelLatch.await(2, TimeUnit.SECONDS), is(true));
assertThat(retryThrowable[0], instanceOf(RuntimeException.class));
assertThat(cancelThrowable[0], instanceOf(RuntimeException.class));
assertThat(retryThrowable[0], is(cancelThrowable[0]));
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class RetryLogicTest method testChangePriorityAndObserveExecutionOrder.
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void testChangePriorityAndObserveExecutionOrder(boolean persistent) throws InterruptedException {
cancelLatch = new CountDownLatch(2);
RetryJob job1 = new RetryJob(new Params(10).setPersistent(persistent).groupBy("group"));
job1.identifier = "1";
RetryJob job2 = new RetryJob(new Params(5).setPersistent(persistent).groupBy("group"));
job2.identifier = "2";
JobManager jobManager = createJobManager();
jobManager.stop();
jobManager.addJob(job1);
jobManager.addJob(job2);
retryProvider = new RetryProvider() {
@Override
public RetryConstraint build(Job job, Throwable throwable, int runCount, int maxRunCount) {
RetryJob retryJob = (RetryJob) job;
if ("1".equals(retryJob.identifier)) {
if (retryJob.getPriority() == 1) {
return RetryConstraint.CANCEL;
}
RetryConstraint retryConstraint = new RetryConstraint(true);
retryConstraint.setNewPriority(1);
return retryConstraint;
} else {
return RetryConstraint.CANCEL;
}
}
};
final List<String> runOrder = new ArrayList<>();
onRunCallback = new Callback() {
@Override
public void on(Job job) {
runOrder.add(((RetryJob) job).identifier);
}
};
canRun = true;
jobManager.start();
assertThat("both jobs should be canceled eventually", cancelLatch.await(3, TimeUnit.MINUTES), is(true));
assertThat("jobs should run a total of 3 times", runCount, is(3));
final List<String> expectedRunOrder = Arrays.asList("1", "2", "1");
assertThat("expected run order count should match", runOrder.size(), is(expectedRunOrder.size()));
for (int i = 0; i < expectedRunOrder.size(); i++) {
assertThat("at iteration " + i + ", this job should run", runOrder.get(i), is(expectedRunOrder.get(i)));
}
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testTagsWithMultipleHolders.
@Test
public void testTagsWithMultipleHolders() {
JobQueue jobQueue = createNewJobQueue();
final String tag1 = UUID.randomUUID().toString();
String tag2 = UUID.randomUUID().toString();
while (tag2.equals(tag1)) {
tag2 = UUID.randomUUID().toString();
}
String tag3 = UUID.randomUUID().toString();
while (tag3.equals(tag1) || tag3.equals(tag2)) {
tag3 = UUID.randomUUID().toString();
}
JobHolder holder1 = createNewJobHolder(new Params(0).addTags(tag1, tag2));
JobHolder holder2 = createNewJobHolder(new Params(0).addTags(tag1, tag3));
jobQueue.insert(holder1);
jobQueue.insert(holder2);
Set<JobHolder> twoJobs = jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1));
Set<String> resultIds = ids(twoJobs);
assertThat("two jobs should be returned", twoJobs.size(), is(2));
assertThat("should have job id 1", resultIds, hasItems(holder1.getId(), holder2.getId()));
for (String tag : new String[] { tag2, tag3 }) {
Set<JobHolder> oneJob = jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag));
resultIds = ids(oneJob);
assertThat("one job should be returned", oneJob.size(), is(1));
if (tag.equals(tag2)) {
assertThat("should have job id 1", resultIds, hasItems(holder1.getId()));
} else {
assertThat("should have job id 2", resultIds, hasItems(holder2.getId()));
}
}
jobQueue.remove(holder1);
assertTags("after one of the jobs is removed", jobQueue, holder2);
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testGroupId.
@Test
public void testGroupId() throws Exception {
JobQueue jobQueue = createNewJobQueue();
JobHolder jobHolder1 = createNewJobHolder(new Params(0).groupBy("group1"));
JobHolder jobHolder2 = createNewJobHolder(new Params(0).groupBy("group1"));
JobHolder jobHolder3 = createNewJobHolder(new Params(0).groupBy("group2"));
JobHolder jobHolder4 = createNewJobHolder(new Params(0).groupBy("group2"));
JobHolder jobHolder5 = createNewJobHolder(new Params(0).groupBy("group1"));
jobQueue.insert(jobHolder1);
jobQueue.insert(jobHolder2);
jobQueue.insert(jobHolder3);
jobQueue.insert(jobHolder4);
jobQueue.insert(jobHolder5);
TestConstraint constraint = new TestConstraint(mockTimer);
constraint.setExcludeRunning(true);
constraint.setExcludeGroups(Arrays.asList(new String[] { "group2" }));
JobHolder received = jobQueue.nextJobAndIncRunCount(constraint);
assertThat("first jobs should be from group group1 if group2 is excluded", received.getJob().getRunGroupId(), equalTo("group1"));
assertThat("correct job should be returned if groupId is provided", received.getId(), equalTo(jobHolder1.getId()));
constraint.setExcludeGroups(Arrays.asList(new String[] { "group1", "group2" }));
assertThat("no jobs should be returned if all groups are excluded", jobQueue.nextJobAndIncRunCount(constraint), is(nullValue()));
JobHolder jobHolder6 = createNewJobHolder(new Params(0));
jobQueue.insert(jobHolder6);
JobHolder tmpReceived = jobQueue.nextJobAndIncRunCount(constraint);
assertThat("both groups are disabled, null group job should be returned", tmpReceived.getId(), is(jobHolder6.getId()));
constraint.setExcludeGroups(Arrays.asList(new String[] { "group1" }));
assertThat("if group1 is excluded, next job should be from group2", jobQueue.nextJobAndIncRunCount(constraint).getJob().getRunGroupId(), equalTo("group2"));
//to test re-run case, add the job back in
assertThat(jobQueue.insertOrReplace(received), is(true));
//ask for it again, should return the same holder because it is grouped
constraint.clear();
constraint.setExcludeRunning(true);
JobHolder received2 = jobQueue.nextJobAndIncRunCount(constraint);
assertThat("for grouped jobs, re-fetching job should work fine", received2.getId(), equalTo(received.getId()));
constraint.setExcludeGroups(Arrays.asList(new String[] { "group1" }));
JobHolder received3 = jobQueue.nextJobAndIncRunCount(constraint);
assertThat("if a group is excluded, next available from another group should be returned", received3.getId(), equalTo(jobHolder4.getId()));
//add two more non-grouped jobs
JobHolder jobHolder7 = createNewJobHolder(new Params(0));
jobQueue.insert(jobHolder7);
JobHolder jobHolder8 = createNewJobHolder(new Params(0));
jobQueue.insert(jobHolder8);
constraint.setExcludeGroups(Arrays.asList(new String[] { "group1", "group2" }));
JobHolder holder4 = jobQueue.nextJobAndIncRunCount(constraint);
assertThat("if all grouped jobs are excluded, next non-grouped job should be returned", holder4.getId(), equalTo(jobHolder7.getId()));
jobQueue.insertOrReplace(holder4);
//for non-grouped jobs, run counts should be respected
assertThat("if all grouped jobs are excluded, re-inserted highest priority job should still be returned", jobQueue.nextJobAndIncRunCount(constraint).getId(), equalTo(jobHolder7.getId()));
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testDelayUntilWithUnmeteredNetworkRequirementAndRegularDelayedJob.
@Test
public void testDelayUntilWithUnmeteredNetworkRequirementAndRegularDelayedJob() {
JobQueue jobQueue = createNewJobQueue();
JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(1000).requireUnmeteredNetwork());
JobHolder holder2 = createNewJobHolder(new Params(2).delayInMs(500));
jobQueue.insert(holder1);
jobQueue.insert(holder2);
TestConstraint constraint = new TestConstraint(mockTimer);
constraint.setMaxNetworkType(NetworkUtil.METERED);
assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(500000000L));
}
Aggregations