Search in sources :

Example 41 with JobHolder

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);
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) TagConstraint(com.birbit.android.jobqueue.TagConstraint) Constraint(com.birbit.android.jobqueue.Constraint) TestConstraint(com.birbit.android.jobqueue.TestConstraint) Test(org.junit.Test)

Example 42 with JobHolder

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));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 43 with JobHolder

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));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 44 with JobHolder

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()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 45 with JobHolder

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();
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) TagConstraint(com.birbit.android.jobqueue.TagConstraint) Constraint(com.birbit.android.jobqueue.Constraint) TestConstraint(com.birbit.android.jobqueue.TestConstraint) Test(org.junit.Test)

Aggregations

JobHolder (com.birbit.android.jobqueue.JobHolder)62 Test (org.junit.Test)50 Params (com.birbit.android.jobqueue.Params)46 JobQueue (com.birbit.android.jobqueue.JobQueue)42 TestConstraint (com.birbit.android.jobqueue.TestConstraint)39 Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)7 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)7 JobManager (com.birbit.android.jobqueue.JobManager)6 Job (com.birbit.android.jobqueue.Job)4 Cursor (android.database.Cursor)2 Consumer (com.birbit.android.jobqueue.ConsumerManager.Consumer)2 Configuration (com.birbit.android.jobqueue.config.Configuration)2 MessageFactory (com.birbit.android.jobqueue.messaging.MessageFactory)2 MessageQueueConsumer (com.birbit.android.jobqueue.messaging.MessageQueueConsumer)2 PriorityMessageQueue (com.birbit.android.jobqueue.messaging.PriorityMessageQueue)2 SafeMessageQueue (com.birbit.android.jobqueue.messaging.SafeMessageQueue)2 RunJobMessage (com.birbit.android.jobqueue.messaging.message.RunJobMessage)2 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2