Search in sources :

Example 36 with TestConstraint

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

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

the class JobQueueTestBase method testDeadlineDoesNotAffectTags.

@Test
public void testDeadlineDoesNotAffectTags() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder = createNewJobHolder(new Params(0).overrideDeadlineToRunInMs(10));
    jobQueue.insert(jobHolder);
    mockTimer.incrementMs(100);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setTags(new String[] { "a" });
    constraint.setTagConstraint(TagConstraint.ANY);
    assertThat(jobQueue.findJobs(constraint), is(Collections.EMPTY_SET));
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(nullValue()));
}
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 38 with TestConstraint

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

the class JobQueueTestBase method testDelayUntilWithExcludeGroups.

@Test
public void testDelayUntilWithExcludeGroups() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    long now = mockTimer.nanoTime();
    JobHolder networkJobHolder = createNewJobHolderWithDelayUntil(new Params(0).requireNetwork().groupBy("group1"), now + 200000 * JobManager.NS_PER_MS);
    JobHolder noNetworkJobHolder = createNewJobHolderWithDelayUntil(new Params(0).groupBy("group2"), now + 500000 * JobManager.NS_PER_MS);
    jobQueue.insert(networkJobHolder);
    jobQueue.insert(noNetworkJobHolder);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    constraint.setExcludeRunning(true);
    assertThat("if there is no network, delay until should be provided for no network job", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder.getDelayUntilNs()));
    assertThat("if there is no network, delay until should be provided for no network job", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder.getDelayUntilNs()));
    constraint.setExcludeGroups(Arrays.asList("group2"));
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat("if there is no network, but the group is disabled, delay until should be null", jobQueue.getNextJobDelayUntilNs(constraint), nullValue());
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    constraint.setExcludeGroups(Arrays.asList("group1", "group2"));
    assertThat("if there is network, but both groups are disabled, delay until should be null", jobQueue.getNextJobDelayUntilNs(constraint), nullValue());
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    constraint.setExcludeGroups(Arrays.asList("group1"));
    assertThat("if there is network, but group1 is disabled, delay should come from group2", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder.getDelayUntilNs()));
    constraint.setExcludeGroups(Arrays.asList("group2"));
    assertThat("if there is network, but group2 is disabled, delay should come from group1", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(networkJobHolder.getDelayUntilNs()));
    JobHolder noNetworkJobHolder2 = createNewJobHolderWithDelayUntil(new Params(0), now + 100000 * JobManager.NS_PER_MS);
    constraint.setExcludeGroups(Arrays.asList("group1", "group2"));
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    jobQueue.insert(noNetworkJobHolder2);
    assertThat("if there is a 3rd job and other gorups are disabled. 3rd job's delay should be " + "returned", jobQueue.getNextJobDelayUntilNs(constraint), equalTo(noNetworkJobHolder2.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 39 with TestConstraint

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

the class JobQueueTestBase method testDelayUntilWithUnmeteredNetworkRequirement2.

@Test
public void testDelayUntilWithUnmeteredNetworkRequirement2() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(3000).requireUnmeteredNetwork().delayInMs(2000));
    jobQueue.insert(holder1);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.UNMETERED);
    assertThat(jobQueue.getNextJobDelayUntilNs(constraint), is(2000000000L));
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    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)

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