Search in sources :

Example 6 with JobQueue

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

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

Example 8 with JobQueue

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

Example 9 with JobQueue

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

the class JobQueueTestBase method testDelayUntilWithNetworkRequirementAndRegularDelayedJob.

@Test
public void testDelayUntilWithNetworkRequirementAndRegularDelayedJob() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(1000).requireNetwork());
    JobHolder holder2 = createNewJobHolder(new Params(2).delayInMs(500));
    jobQueue.insert(holder1);
    jobQueue.insert(holder2);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    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 10 with JobQueue

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

the class JobQueueTestBase method testDelayUntilWithNetworkRequirement4.

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

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