use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testBasicAddRemoveCount.
@Test
public void testBasicAddRemoveCount() throws Exception {
final int ADD_COUNT = 6;
JobQueue jobQueue = createNewJobQueue();
assertThat((int) jobQueue.count(), equalTo(0));
TestConstraint constraint = new TestConstraint(mockTimer);
constraint.setExcludeRunning(true);
assertThat(jobQueue.nextJobAndIncRunCount(constraint), nullValue());
for (int i = 0; i < ADD_COUNT; i++) {
JobHolder holder = createNewJobHolder();
jobQueue.insert(holder);
assertThat((int) jobQueue.count(), equalTo(i + 1));
assertThat(holder.getInsertionOrder(), equalTo(i + 1L));
jobQueue.insertOrReplace(holder);
assertThat((int) jobQueue.count(), equalTo(i + 1));
}
JobHolder firstHolder = jobQueue.nextJobAndIncRunCount(constraint);
assertThat(firstHolder.getRunCount(), equalTo(1));
//size should be down 1
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 1));
//should return another job
JobHolder secondHolder = jobQueue.nextJobAndIncRunCount(constraint);
assertThat(secondHolder.getRunCount(), equalTo(1));
//size should be down 2
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
//second holder and first holder should have different ids
assertThat(firstHolder.getId(), not(secondHolder.getId()));
jobQueue.remove(secondHolder);
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
jobQueue.remove(secondHolder);
//non existed job removed, count should be the same
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
jobQueue.remove(firstHolder);
assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testPriorityWithReAdd.
@Test
public void testPriorityWithReAdd() throws Exception {
int JOB_LIMIT = 20;
JobQueue jobQueue = createNewJobQueue();
//create and add JOB_LIMIT jobs with random priority
for (int i = 0; i < JOB_LIMIT; i++) {
jobQueue.insert(createNewJobHolder(new Params((int) (Math.random() * 10))));
}
//ensure we get jobs in correct priority order
int minPriority = Integer.MAX_VALUE;
for (int i = 0; i < JOB_LIMIT; i++) {
JobHolder holder = jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer));
assertThat(holder.getPriority() <= minPriority, is(true));
jobQueue.insertOrReplace(holder);
}
assertThat(jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer)), notNullValue());
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testDelayUntilWithNetworkRequirementAndRegularDelayedJob.
@Test
public void testDelayUntilWithNetworkRequirementAndRegularDelayedJob() {
JobQueue jobQueue = createNewJobQueue();
JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(1000).requireNetwork());
JobHolder holder2 = createNewJobHolder(new Params(2).delayInMs(500));
jobQueue.insert(holder1);
jobQueue.insert(holder2);
TestConstraint constraint = new TestConstraint(mockTimer);
constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(500000000L));
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testDelayUntilWithNetworkRequirement4.
@Test
public void testDelayUntilWithNetworkRequirement4() {
JobQueue jobQueue = createNewJobQueue();
JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(3000).delayInMs(2000).requireNetwork());
jobQueue.insert(holder1);
TestConstraint constraint = new TestConstraint(mockTimer);
constraint.setMaxNetworkType(NetworkUtil.UNMETERED);
assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(2000000000L));
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testFindByTags.
@Test
public void testFindByTags() {
JobQueue jobQueue = createNewJobQueue();
assertThat("empty queue should return 0", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), "abc")).size(), is(0));
jobQueue.insert(createNewJobHolder());
Set<JobHolder> result = jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), "blah"));
assertThat("if job does not have a tag, it should return 0", result.size(), is(0));
final String tag1 = UUID.randomUUID().toString();
JobHolder holder = createNewJobHolder(new Params(0).addTags(tag1));
jobQueue.insert(holder);
assertTags("holder with 1 tag", jobQueue, holder);
jobQueue.insertOrReplace(holder);
assertTags("holder with 1 tag reinserted", jobQueue, holder);
jobQueue.remove(holder);
assertThat("when job is removed, it should return none", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1)).size(), is(0));
JobHolder holder2 = createNewJobHolder(new Params(0).addTags(tag1));
jobQueue.insert(holder2);
assertThat("it should return the job", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1)).size(), is(1));
jobQueue.onJobCancelled(holder2);
assertThat("when queried w/ exclude cancelled, it should not return the job", jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1)).size(), is(0));
}
Aggregations