Search in sources :

Example 26 with Params

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

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

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

the class SingleIdTest method testSingleIdRunning.

private void testSingleIdRunning(boolean persistent) throws InterruptedException {
    JobManager jobManager = createJobManager();
    String singleId = "dorks";
    CountDownLatch latchWait = new CountDownLatch(1);
    CountDownLatch latchRunning = new CountDownLatch(1);
    DummyJob dummyJob1 = new SerializableDummyTwoLatchJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId), latchWait, latchRunning);
    addJob(jobManager, dummyJob1);
    jobManager.start();
    //let job1 start running
    latchRunning.await(5, TimeUnit.SECONDS);
    jobManager.stop();
    assertThat("should not be marked ready", jobManager.count(), is(0));
    CountDownLatch latchRunning2 = new CountDownLatch(1);
    DummyJob dummyJob2 = new SerializableDummyLatchJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId), latchRunning2);
    addJob(jobManager, dummyJob2);
    assertThat("should add new job if first job was running", jobManager.count(), is(1));
    DummyJob dummyJob3 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId));
    addJob(jobManager, dummyJob3);
    assertThat("should not add new job if already queued", jobManager.count(), is(1));
    //let job1 finish
    latchWait.countDown();
    jobManager.start();
    //wait until job2 runs
    assertThat("job should have run", latchRunning2.await(5, TimeUnit.SECONDS), is(true));
    jobManager.stopAndWaitUntilConsumersAreFinished();
    assertThat("job should not have run", dummyJob3.getOnRunCnt(), is(0));
    assertThat("should have called onCancel", dummyJob3.getOnCancelCnt(), is(1));
    DummyJob dummyJob4 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId));
    addJob(jobManager, dummyJob4);
    assertThat("should be added if all others have run", jobManager.count(), is(1));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 29 with Params

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

the class SlowOnAddedTest method testPersistent.

@Test
public void testPersistent() throws InterruptedException {
    JobManager jobManager = createJobManager();
    MyDummyPersistentJob.persistentJobLatch = new CountDownLatch(1);
    for (int i = 0; i < 50; i++) {
        jobManager.addJob(new DummyJob(new Params(1).persist()));
    }
    jobManager.addJob(new MyDummyPersistentJob(2));
    MyDummyPersistentJob.persistentJobLatch.await();
    assertThat("even if job is persistent, onAdded should be called b4 onRun", MyDummyPersistentJob.onAddedCountWhenOnRun, equalTo(1));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Test(org.junit.Test)

Example 30 with Params

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

the class JobQueueTestBase method testPriorityWithReAdd.

@Test
public void testPriorityWithReAdd() 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;
    for (int i = 0; i < JOB_LIMIT; i++) {
        JobHolder holder = jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer));
        assertThat(holder.getPriority() <= minPriority, is(true));
        jobQueue.insertOrReplace(holder);
    }
    assertThat(jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer)), notNullValue());
}
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)

Aggregations

Params (com.birbit.android.jobqueue.Params)95 Test (org.junit.Test)75 JobManager (com.birbit.android.jobqueue.JobManager)47 JobHolder (com.birbit.android.jobqueue.JobHolder)46 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)41 JobQueue (com.birbit.android.jobqueue.JobQueue)40 TestConstraint (com.birbit.android.jobqueue.TestConstraint)37 Job (com.birbit.android.jobqueue.Job)31 CountDownLatch (java.util.concurrent.CountDownLatch)31 Configuration (com.birbit.android.jobqueue.config.Configuration)17 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)12 TargetApi (android.annotation.TargetApi)9 CancelResult (com.birbit.android.jobqueue.CancelResult)9 Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)8 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)6 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)4 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)3