Search in sources :

Example 1 with JobQueue

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

the class JobQueueTestBase method testTagsWithMultipleHolders.

@Test
public void testTagsWithMultipleHolders() {
    JobQueue jobQueue = createNewJobQueue();
    final String tag1 = UUID.randomUUID().toString();
    String tag2 = UUID.randomUUID().toString();
    while (tag2.equals(tag1)) {
        tag2 = UUID.randomUUID().toString();
    }
    String tag3 = UUID.randomUUID().toString();
    while (tag3.equals(tag1) || tag3.equals(tag2)) {
        tag3 = UUID.randomUUID().toString();
    }
    JobHolder holder1 = createNewJobHolder(new Params(0).addTags(tag1, tag2));
    JobHolder holder2 = createNewJobHolder(new Params(0).addTags(tag1, tag3));
    jobQueue.insert(holder1);
    jobQueue.insert(holder2);
    Set<JobHolder> twoJobs = jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag1));
    Set<String> resultIds = ids(twoJobs);
    assertThat("two jobs should be returned", twoJobs.size(), is(2));
    assertThat("should have job id 1", resultIds, hasItems(holder1.getId(), holder2.getId()));
    for (String tag : new String[] { tag2, tag3 }) {
        Set<JobHolder> oneJob = jobQueue.findJobs(forTags(mockTimer, ANY, Collections.<String>emptyList(), tag));
        resultIds = ids(oneJob);
        assertThat("one job should be returned", oneJob.size(), is(1));
        if (tag.equals(tag2)) {
            assertThat("should have job id 1", resultIds, hasItems(holder1.getId()));
        } else {
            assertThat("should have job id 2", resultIds, hasItems(holder2.getId()));
        }
    }
    jobQueue.remove(holder1);
    assertTags("after one of the jobs is removed", jobQueue, holder2);
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) JobQueue(com.birbit.android.jobqueue.JobQueue) Params(com.birbit.android.jobqueue.Params) Test(org.junit.Test)

Example 2 with JobQueue

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

the class JobQueueTestBase method testTruncate.

@Test
public void testTruncate() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    final int LIMIT = 20;
    for (int i = 0; i < LIMIT; i++) {
        jobQueue.insert(createNewJobHolder());
    }
    assertThat("queue should have all jobs", jobQueue.count(), equalTo(LIMIT));
    jobQueue.clear();
    assertThat("after clear, queue should be empty", jobQueue.count(), equalTo(0));
    for (int i = 0; i < LIMIT; i++) {
        jobQueue.insert(createNewJobHolder());
    }
    assertThat("if we add jobs again, count should match", jobQueue.count(), equalTo(LIMIT));
}
Also used : 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)

Example 3 with JobQueue

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

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

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

Aggregations

JobQueue (com.birbit.android.jobqueue.JobQueue)45 Test (org.junit.Test)44 JobHolder (com.birbit.android.jobqueue.JobHolder)42 TestConstraint (com.birbit.android.jobqueue.TestConstraint)41 Params (com.birbit.android.jobqueue.Params)40 Constraint (com.birbit.android.jobqueue.Constraint)9 TagConstraint (com.birbit.android.jobqueue.TagConstraint)8 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