Search in sources :

Example 31 with TestConstraint

use of com.birbit.android.jobqueue.TestConstraint 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 32 with TestConstraint

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

the class JobQueueTestBase method testDelayUntilWithNetworkRequirementAndRegularDelayedJob2.

@Test
public void testDelayUntilWithNetworkRequirementAndRegularDelayedJob2() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(1000).requireUnmeteredNetwork());
    JobHolder holder2 = createNewJobHolder(new Params(2).delayInMs(500));
    jobQueue.insert(holder2);
    jobQueue.insert(holder1);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(500000000L));
}
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 33 with TestConstraint

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

the class JobQueueTestBase method testJobFields.

@Test
public void testJobFields() throws Exception {
    long sessionId = (long) (Math.random() * 1000);
    JobQueue jobQueue = createNewJobQueueWithSessionId(sessionId);
    int priority = (int) (Math.random() * 1000);
    JobHolder jobHolder = createNewJobHolder(new Params(priority));
    int runCount = (int) (Math.random() * 10);
    jobHolder.setRunCount(runCount);
    jobQueue.insert(jobHolder);
    for (int i = 0; i < 2; i++) {
        JobHolder received = jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer));
        assertThat("job id should be preserved", received.getId(), equalTo(jobHolder.getId()));
        assertThat("job priority should be preserved", received.getPriority(), equalTo(priority));
        assertThat("job session id should be assigned", received.getRunningSessionId(), equalTo(sessionId));
        assertThat("job run count should be incremented", received.getRunCount(), equalTo(runCount + i + 1));
        jobQueue.insertOrReplace(received);
    }
}
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 34 with TestConstraint

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

the class JobQueueTestBase method testDelayUntilWithUnmeteredNetworkRequirement.

@Test
public void testDelayUntilWithUnmeteredNetworkRequirement() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(1000).requireUnmeteredNetwork());
    jobQueue.insert(holder1);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(1000000000L));
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    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 35 with TestConstraint

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

the class JobQueueTestBase method testDelayUntilWithUnmeteredNetworkRequirement5.

@Test
public void testDelayUntilWithUnmeteredNetworkRequirement5() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(2000).requireUnmeteredNetwork().delayInMs(1000));
    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)

Aggregations

TestConstraint (com.birbit.android.jobqueue.TestConstraint)39 JobQueue (com.birbit.android.jobqueue.JobQueue)38 Test (org.junit.Test)38 JobHolder (com.birbit.android.jobqueue.JobHolder)37 Params (com.birbit.android.jobqueue.Params)35 Constraint (com.birbit.android.jobqueue.Constraint)5 TagConstraint (com.birbit.android.jobqueue.TagConstraint)5 Job (com.birbit.android.jobqueue.Job)1 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)1 CountDownLatch (java.util.concurrent.CountDownLatch)1