Search in sources :

Example 6 with Constraint

use of com.birbit.android.jobqueue.Constraint 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)

Example 7 with Constraint

use of com.birbit.android.jobqueue.Constraint in project android-priority-jobqueue by yigit.

the class JobQueueTestBase method testPriority.

@Test
public void testPriority() 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;
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setExcludeRunning(true);
    for (int i = 0; i < JOB_LIMIT; i++) {
        JobHolder holder = jobQueue.nextJobAndIncRunCount(constraint);
        assertThat(holder.getPriority() <= minPriority, is(true));
    }
    assertThat(jobQueue.nextJobAndIncRunCount(constraint), nullValue());
}
Also used : TestConstraint(com.birbit.android.jobqueue.TestConstraint) 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 8 with Constraint

use of com.birbit.android.jobqueue.Constraint in project android-priority-jobqueue by yigit.

the class JobQueueTestBase method testDueDelayUntilWithPriority.

@Test
public void testDueDelayUntilWithPriority() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    mockTimer.setNow(2000);
    long now = mockTimer.nanoTime();
    JobHolder lowPriorityHolder = createNewJobHolderWithDelayUntil(new Params(5), now - 1000 * JobManager.NS_PER_MS);
    JobHolder highPriorityHolder = createNewJobHolderWithDelayUntil(new Params(10), now - 10000 * JobManager.NS_PER_MS);
    jobQueue.insert(lowPriorityHolder);
    jobQueue.insert(highPriorityHolder);
    long soonJobDelay = 2000;
    JobHolder highestPriorityDelayedJob = createNewJobHolderWithDelayUntil(new Params(12), now + soonJobDelay * JobManager.NS_PER_MS);
    jobQueue.insert(highestPriorityDelayedJob);
    Constraint constraint = new Constraint();
    constraint.setNowInNs(mockTimer.nanoTime());
    assertThat("when asked, if job's due has passed, highest priority jobs's delay until should be " + "returned", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(highPriorityHolder.getDelayUntilNs()));
    //make sure soon job is valid now
    mockTimer.incrementMs(soonJobDelay + 1);
    assertThat("when a job's time come, it should be returned", jobQueue.nextJobAndIncRunCount(constraint).getId(), equalTo(highestPriorityDelayedJob.getId()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) JobQueue(com.birbit.android.jobqueue.JobQueue) TagConstraint(com.birbit.android.jobqueue.TagConstraint) Constraint(com.birbit.android.jobqueue.Constraint) TestConstraint(com.birbit.android.jobqueue.TestConstraint) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Aggregations

Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)6 JobHolder (com.birbit.android.jobqueue.JobHolder)5 JobQueue (com.birbit.android.jobqueue.JobQueue)5 Test (org.junit.Test)5 Params (com.birbit.android.jobqueue.Params)4 TestConstraint (com.birbit.android.jobqueue.TestConstraint)4 SuppressLint (android.annotation.SuppressLint)1 Job (com.birbit.android.jobqueue.Job)1 JobManager (com.birbit.android.jobqueue.JobManager)1 QueueFactory (com.birbit.android.jobqueue.QueueFactory)1 Configuration (com.birbit.android.jobqueue.config.Configuration)1 SimpleInMemoryPriorityQueue (com.birbit.android.jobqueue.inMemoryQueue.SimpleInMemoryPriorityQueue)1 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)1 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1