Search in sources :

Example 1 with TestConstraint

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

the class JobQueueTestBase method testGroupId.

@Test
public void testGroupId() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder1 = createNewJobHolder(new Params(0).groupBy("group1"));
    JobHolder jobHolder2 = createNewJobHolder(new Params(0).groupBy("group1"));
    JobHolder jobHolder3 = createNewJobHolder(new Params(0).groupBy("group2"));
    JobHolder jobHolder4 = createNewJobHolder(new Params(0).groupBy("group2"));
    JobHolder jobHolder5 = createNewJobHolder(new Params(0).groupBy("group1"));
    jobQueue.insert(jobHolder1);
    jobQueue.insert(jobHolder2);
    jobQueue.insert(jobHolder3);
    jobQueue.insert(jobHolder4);
    jobQueue.insert(jobHolder5);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setExcludeRunning(true);
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group2" }));
    JobHolder received = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("first jobs should be from group group1 if group2 is excluded", received.getJob().getRunGroupId(), equalTo("group1"));
    assertThat("correct job should be returned if groupId is provided", received.getId(), equalTo(jobHolder1.getId()));
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1", "group2" }));
    assertThat("no jobs should be returned if all groups are excluded", jobQueue.nextJobAndIncRunCount(constraint), is(nullValue()));
    JobHolder jobHolder6 = createNewJobHolder(new Params(0));
    jobQueue.insert(jobHolder6);
    JobHolder tmpReceived = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("both groups are disabled, null group job should be returned", tmpReceived.getId(), is(jobHolder6.getId()));
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1" }));
    assertThat("if group1 is excluded, next job should be from group2", jobQueue.nextJobAndIncRunCount(constraint).getJob().getRunGroupId(), equalTo("group2"));
    //to test re-run case, add the job back in
    assertThat(jobQueue.insertOrReplace(received), is(true));
    //ask for it again, should return the same holder because it is grouped
    constraint.clear();
    constraint.setExcludeRunning(true);
    JobHolder received2 = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("for grouped jobs, re-fetching job should work fine", received2.getId(), equalTo(received.getId()));
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1" }));
    JobHolder received3 = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("if a group is excluded, next available from another group should be returned", received3.getId(), equalTo(jobHolder4.getId()));
    //add two more non-grouped jobs
    JobHolder jobHolder7 = createNewJobHolder(new Params(0));
    jobQueue.insert(jobHolder7);
    JobHolder jobHolder8 = createNewJobHolder(new Params(0));
    jobQueue.insert(jobHolder8);
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1", "group2" }));
    JobHolder holder4 = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("if all grouped jobs are excluded, next non-grouped job should be returned", holder4.getId(), equalTo(jobHolder7.getId()));
    jobQueue.insertOrReplace(holder4);
    //for non-grouped jobs, run counts should be respected
    assertThat("if all grouped jobs are excluded, re-inserted highest priority job should still be returned", jobQueue.nextJobAndIncRunCount(constraint).getId(), equalTo(jobHolder7.getId()));
}
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 2 with TestConstraint

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

the class JobQueueTestBase method testDelayUntilWithUnmeteredNetworkRequirementAndRegularDelayedJob.

@Test
public void testDelayUntilWithUnmeteredNetworkRequirementAndRegularDelayedJob() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(1000).requireUnmeteredNetwork());
    JobHolder holder2 = createNewJobHolder(new Params(2).delayInMs(500));
    jobQueue.insert(holder1);
    jobQueue.insert(holder2);
    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 3 with TestConstraint

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

the class JobQueueTestBase method testCountReadyJobs.

@Test
public void testCountReadyJobs() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    TestConstraint constraint = new TestConstraint(mockTimer);
    assertThat("initial count should be 0 for ready jobs", jobQueue.countReadyJobs(constraint), equalTo(0));
    //add some jobs
    jobQueue.insert(createNewJobHolder());
    jobQueue.insert(createNewJobHolder(new Params(0).requireNetwork()));
    long now = mockTimer.nanoTime();
    long delay = 1000;
    constraint.setTimeLimit(now);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    constraint.setExcludeRunning(true);
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0), now + TimeUnit.MILLISECONDS.toNanos(delay)));
    assertThat("ready count should be 1 if there is no network", jobQueue.countReadyJobs(constraint), equalTo(1));
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    assertThat("ready count should be 2 if there is network", jobQueue.countReadyJobs(constraint), equalTo(2));
    mockTimer.incrementMs(delay + 1);
    constraint.setTimeLimit(mockTimer.nanoTime());
    assertThat("when needed delay time passes, ready count should be 3", jobQueue.countReadyJobs(constraint), equalTo(3));
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat("when needed delay time passes but no network, ready count should be 2", jobQueue.countReadyJobs(constraint), equalTo(2));
    jobQueue.insert(createNewJobHolder(new Params(5).groupBy("group1")));
    jobQueue.insert(createNewJobHolder(new Params(5).groupBy("group1")));
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    assertThat("when more than 1 job from same group is created, ready jobs should increment only by 1", jobQueue.countReadyJobs(constraint), equalTo(4));
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1" }));
    assertThat("excluding groups should work", jobQueue.countReadyJobs(constraint), equalTo(3));
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group3423" }));
    assertThat("giving a non-existing group should not fool the count", jobQueue.countReadyJobs(constraint), equalTo(4));
    jobQueue.insert(createNewJobHolder(new Params(3).groupBy("group2")));
    constraint.clear();
    constraint.setTimeLimit(mockTimer.nanoTime());
    constraint.setMaxNetworkType(NetworkUtil.UNMETERED);
    constraint.setExcludeRunning(true);
    assertThat("when a job from another group is added, ready job count should inc", jobQueue.countReadyJobs(constraint), equalTo(5));
    now = mockTimer.nanoTime();
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(3).groupBy("group3"), now + TimeUnit.MILLISECONDS.toNanos(delay)));
    assertThat("when a delayed job from another group is added, ready count should not change", jobQueue.countReadyJobs(constraint), equalTo(5));
    jobQueue.insert(createNewJobHolder(new Params(3).groupBy("group3")));
    assertThat("when another job from delayed group is added, ready job count should inc", jobQueue.countReadyJobs(constraint), equalTo(6));
    mockTimer.incrementMs(delay);
    constraint.setTimeLimit(mockTimer.nanoTime());
    assertThat("when delay passes and a job from existing group becomes available, ready job count should not change", jobQueue.countReadyJobs(constraint), equalTo(6));
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1", "group3" }));
    assertThat("when some groups are excluded, count should be correct", jobQueue.countReadyJobs(constraint), equalTo(4));
    //jobs w/ same group id but with different persistence constraints should not fool the count
    now = mockTimer.nanoTime();
    constraint.setTimeLimit(mockTimer.nanoTime());
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).persist().groupBy("group10"), now + 1000));
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).groupBy("group10"), now + 1000));
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).persist().groupBy("group10"), now - 1000));
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).groupBy("group10"), now - 1000));
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1", "group3" }));
    assertThat("when many jobs are added w/ different constraints but same group id, ready count should not be fooled", jobQueue.countReadyJobs(constraint), equalTo(5));
    constraint.clear();
    constraint.setExcludeRunning(true);
    constraint.setMaxNetworkType(NetworkUtil.UNMETERED);
    assertThat("when many jobs are added w/ different constraints but same group id, ready count should not be fooled", jobQueue.countReadyJobs(constraint), equalTo(7));
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    constraint.setExcludeGroups(Arrays.asList(new String[] { "group1", "group3" }));
    assertThat("when many jobs are added w/ different constraints but same group id, ready count should not be fooled", jobQueue.countReadyJobs(constraint), equalTo(4));
}
Also used : TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 4 with TestConstraint

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

the class JobQueueTestBase method testNetwork.

@Test
public void testNetwork() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder = createNewJobHolder(new Params(0));
    jobQueue.insert(jobHolder);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat("no network job should be returned even if there is no netowrk", jobQueue.nextJobAndIncRunCount(constraint), notNullValue());
    jobQueue.remove(jobHolder);
    jobHolder = createNewJobHolder(new Params(0).requireNetwork());
    assertThat("if there isn't any network, job with network requirement should not return", jobQueue.nextJobAndIncRunCount(constraint), nullValue());
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    assertThat("if there is network, job with network requirement should be returned", jobQueue.nextJobAndIncRunCount(constraint), nullValue());
    jobQueue.remove(jobHolder);
    jobHolder = createNewJobHolder(new Params(1));
    JobHolder jobHolder2 = createNewJobHolder(new Params(5).requireNetwork());
    jobQueue.insert(jobHolder);
    jobQueue.insert(jobHolder2);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    constraint.setExcludeRunning(true);
    JobHolder retrieved = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("one job should be returned w/o network", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("no network job should be returned although it has lower priority", retrieved.getId(), equalTo(jobHolder.getId()));
    }
    assertThat("no other job should be returned w/o network", jobQueue.nextJobAndIncRunCount(constraint), nullValue());
    constraint.setMaxNetworkType(NetworkUtil.METERED);
    retrieved = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("if network is back, network requiring job should be returned", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("when there is network, network job should be returned", retrieved.getId(), equalTo(jobHolder2.getId()));
    }
    //add first job back
    jobQueue.insertOrReplace(jobHolder);
    //add second job back
    jobQueue.insertOrReplace(jobHolder2);
    retrieved = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("if network is back, job w/ higher priority should be returned", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("if network is back, job w/ higher priority should be returned", retrieved.getId(), equalTo(jobHolder2.getId()));
    }
    jobQueue.insertOrReplace(jobHolder2);
    JobHolder highestPriorityJob = createNewJobHolder(new Params(10));
    jobQueue.insert(highestPriorityJob);
    retrieved = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat("w/ or w/o network, highest priority should be returned", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("w/ or w/o network, highest priority should be returned", retrieved.getId(), equalTo(highestPriorityJob.getId()));
    }
//TODO test delay until
}
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 5 with TestConstraint

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

the class JobQueueTestBase method testBasicAddRemoveCount.

@Test
public void testBasicAddRemoveCount() throws Exception {
    final int ADD_COUNT = 6;
    JobQueue jobQueue = createNewJobQueue();
    assertThat((int) jobQueue.count(), equalTo(0));
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setExcludeRunning(true);
    assertThat(jobQueue.nextJobAndIncRunCount(constraint), nullValue());
    for (int i = 0; i < ADD_COUNT; i++) {
        JobHolder holder = createNewJobHolder();
        jobQueue.insert(holder);
        assertThat((int) jobQueue.count(), equalTo(i + 1));
        assertThat(holder.getInsertionOrder(), equalTo(i + 1L));
        jobQueue.insertOrReplace(holder);
        assertThat((int) jobQueue.count(), equalTo(i + 1));
    }
    JobHolder firstHolder = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat(firstHolder.getRunCount(), equalTo(1));
    //size should be down 1
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 1));
    //should return another job
    JobHolder secondHolder = jobQueue.nextJobAndIncRunCount(constraint);
    assertThat(secondHolder.getRunCount(), equalTo(1));
    //size should be down 2
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
    //second holder and first holder should have different ids
    assertThat(firstHolder.getId(), not(secondHolder.getId()));
    jobQueue.remove(secondHolder);
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
    jobQueue.remove(secondHolder);
    //non existed job removed, count should be the same
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
    jobQueue.remove(firstHolder);
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
}
Also used : TestConstraint(com.birbit.android.jobqueue.TestConstraint) 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) 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