use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testFindJobHolderById.
@Test
public void testFindJobHolderById() {
JobQueue jobQueue = createNewJobQueue();
assertJob(jobQueue, "non existing job", UUID.randomUUID().toString(), null);
final int LIMIT = 100;
JobHolder[] holders = new JobHolder[LIMIT];
String[] ids = new String[LIMIT];
for (int i = 0; i < LIMIT; i++) {
holders[i] = createNewJobHolder(new Params((int) (Math.random() * 50)).setPersistent(Math.random() < .5).setRequiresNetwork(Math.random() < .5));
ids[i] = holders[i].getId();
jobQueue.insert(holders[i]);
assertJob(jobQueue, "job by id should work for inserted job", ids[i], holders[i]);
}
final int REMOVE_CNT = LIMIT / 2;
for (int i = 0; i < REMOVE_CNT; i++) {
int ind = (int) (Math.random() * LIMIT);
if (holders[ind] == null) {
continue;
}
//remove some randomly, up to half
jobQueue.remove(holders[ind]);
holders[ind] = null;
}
//re-query all, ensure we can still find non-removed jobs and not find removed jobs
for (int i = 0; i < LIMIT; i++) {
if (holders[i] != null) {
assertJob(jobQueue, "if job is still in the Q, it should be returned", ids[i], holders[i]);
//re add job
jobQueue.insertOrReplace(holders[i]);
//re-test after re-add
assertJob(jobQueue, "after re-insert, if job is still in the Q, it should be returned", ids[i], holders[i]);
} else {
assertJob(jobQueue, "removed job should not be returned in id query", ids[i], null);
}
}
jobQueue.clear();
for (int i = 0; i < LIMIT; i++) {
assertJob(jobQueue, "after clear, find by id should return null", ids[i], null);
}
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testDelayUntilWithNetworkRequirement3.
@Test
public void testDelayUntilWithNetworkRequirement3() {
JobQueue jobQueue = createNewJobQueue();
JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(2000).delayInMs(1000).requireNetwork());
jobQueue.insert(holder1);
TestConstraint constraint = new TestConstraint(mockTimer);
constraint.setMaxNetworkType(NetworkUtil.UNMETERED);
assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(1000000000L));
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testDelayUntilWithNetworkRequirement2.
@Test
public void testDelayUntilWithNetworkRequirement2() {
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.DISCONNECTED);
assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(3000000000L));
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testDelayUntilWithPriority.
@Test
public void testDelayUntilWithPriority() throws Exception {
JobQueue jobQueue = createNewJobQueue();
JobHolder lowPriorityHolder = createNewJobHolderWithDelayUntil(new Params(5), 1);
JobHolder highPriorityHolder = createNewJobHolderWithDelayUntil(new Params(10), 2);
jobQueue.insert(lowPriorityHolder);
jobQueue.insert(highPriorityHolder);
assertThat("when asked, if lower priority job has less delay until, we should return it", jobQueue.getNextJobDelayUntilNs(new TestConstraint(mockTimer)), equalTo(lowPriorityHolder.getDelayUntilNs()));
}
use of com.birbit.android.jobqueue.JobHolder in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method testPriorityWithDelayedJobs.
@Test
public void testPriorityWithDelayedJobs() throws Exception {
JobQueue jobQueue = createNewJobQueue();
JobHolder delayedPriority_5 = createNewJobHolder(new Params(5));
org.fest.reflect.field.Invoker<Long> delayUntilField = getDelayUntilNsField(delayedPriority_5);
delayUntilField.set(mockTimer.nanoTime() - 1000);
JobHolder delayedPriority_2 = createNewJobHolder(new Params(2));
delayUntilField = getDelayUntilNsField(delayedPriority_2);
delayUntilField.set(mockTimer.nanoTime() - 500);
JobHolder nonDelayedPriority_6 = createNewJobHolder(new Params(6));
JobHolder nonDelayedPriority_3 = createNewJobHolder(new Params(3));
JobHolder nonDelayedPriority_2 = createNewJobHolder(new Params(2));
jobQueue.insert(delayedPriority_5);
jobQueue.insert(delayedPriority_2);
jobQueue.insert(nonDelayedPriority_6);
jobQueue.insert(nonDelayedPriority_2);
jobQueue.insert(nonDelayedPriority_3);
TestConstraint constraint = new TestConstraint(mockTimer);
int lastPriority = Integer.MAX_VALUE;
for (int i = 0; i < 5; i++) {
JobHolder next = jobQueue.nextJobAndIncRunCount(constraint);
assertThat("next job should not be null", next, notNullValue());
assertThat("next job's priority should be lower then previous for job " + i, next.getPriority() <= lastPriority, is(true));
lastPriority = next.getPriority();
}
}
Aggregations