use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class DelayTest method testDelay.
public void testDelay(boolean persist) throws Throwable {
JobManager jobManager = createJobManager();
jobManager.stop();
DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(1000).setPersistent(persist));
DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
jobManager.addJob(delayedJob);
jobManager.addJob(nonDelayedJob);
JobHolder receivedJob = nextJob(jobManager);
MatcherAssert.assertThat("non-delayed job should be served", receivedJob, notNullValue());
MatcherAssert.assertThat("non-delayed job should id should match", receivedJob.getId(), equalTo(nonDelayedJob.getId()));
removeJob(jobManager, receivedJob);
MatcherAssert.assertThat("delayed job should not be served", nextJob(jobManager), nullValue());
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
mockTimer.incrementMs(500);
MatcherAssert.assertThat("delayed job should not be served", nextJob(jobManager), nullValue());
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
mockTimer.incrementMs(2000);
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
receivedJob = nextJob(jobManager);
MatcherAssert.assertThat("now should be able to receive the delayed job.", receivedJob, notNullValue());
if (receivedJob != null) {
MatcherAssert.assertThat("received job should be the delayed job", receivedJob.getId(), equalTo(delayedJob.getId()));
}
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class GroupingTest method testGrouping.
@Test
public void testGrouping() throws Throwable {
JobManager jobManager = createJobManager();
jobManager.stop();
String jobId1 = addJob(jobManager, new DummyJob(new Params(0).groupBy("group1")));
String jobId2 = addJob(jobManager, new DummyJob(new Params(0).groupBy("group1")));
String jobId3 = addJob(jobManager, new DummyJob(new Params(0).persist().groupBy("group2")));
String jobId4 = addJob(jobManager, new DummyJob(new Params(0).persist().groupBy("group1")));
JobHolder nextJob = nextJob(jobManager);
MatcherAssert.assertThat("next job should be the first job from group1", nextJob.getId(), equalTo(jobId1));
JobHolder group2Job = nextJob(jobManager, Collections.singletonList("group1"));
MatcherAssert.assertThat("since group 1 is running now, next job should be from group 2", group2Job.getId(), equalTo(jobId3));
removeJob(jobManager, nextJob);
JobHolder group1NextJob = nextJob(jobManager, Arrays.asList("group2"));
MatcherAssert.assertThat("after removing job from group 1, another job from group1 should be returned", group1NextJob.getId(), equalTo(jobId2));
MatcherAssert.assertThat("when jobs from both groups are running, no job should be returned from next job", nextJob(jobManager, Arrays.asList("group1", "group2")), is(nullValue()));
removeJob(jobManager, group2Job);
MatcherAssert.assertThat("even after group2 job is complete, no jobs should be returned" + "since we only have group1 jobs left", nextJob(jobManager, Arrays.asList("group1")), is(nullValue()));
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class NetworkNextJobTest method testNetworkNextJob.
@Test
public void testNetworkNextJob() throws Throwable {
DummyNetworkUtil dummyNetworkUtil = new DummyNetworkUtil();
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
jobManager.stop();
DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
jobManager.addJob(dummyJob);
dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
MatcherAssert.assertThat("when there isn't any network, next job should return null", nextJob(jobManager), nullValue());
MatcherAssert.assertThat("even if there is network, job manager should return correct count", jobManager.count(), equalTo(1));
dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED);
JobHolder retrieved = nextJob(jobManager);
MatcherAssert.assertThat("when network is recovered, next job should be retrieved", retrieved, notNullValue());
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method createNewJobHolderWithDelayUntil.
private JobHolder createNewJobHolderWithDelayUntil(Params params, long delayUntil) {
JobHolder jobHolder = createNewJobHolder(params);
getDelayUntilNsField(jobHolder).set(delayUntil);
return jobHolder;
}
use of com.birbit.android.jobqueue.JobHolder 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);
}
Aggregations