Search in sources :

Example 16 with JobQueue

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

the class JobQueueTestBase method testCountReadyJobs.

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

Example 17 with JobQueue

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

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.path.android.jobqueue.JobQueue) Test(org.junit.Test)

Aggregations

JobQueue (com.path.android.jobqueue.JobQueue)17 Test (org.junit.Test)16 JobHolder (com.path.android.jobqueue.JobHolder)15 Params (com.path.android.jobqueue.Params)13 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)2 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 BaseJob (com.path.android.jobqueue.BaseJob)1 Job (com.path.android.jobqueue.Job)1 JobManager (com.path.android.jobqueue.JobManager)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1