Search in sources :

Example 26 with JobQueue

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

the class JobQueueTestBase method testDeadline.

private void testDeadline(boolean cancel) throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    Params params = new Params(0).requireNetwork();
    if (cancel) {
        params.overrideDeadlineToCancelInMs(100);
    } else {
        params.overrideDeadlineToRunInMs(100);
    }
    JobHolder requireNetwork = createNewJobHolder(params);
    jobQueue.insert(requireNetwork);
    TestConstraint testConstraint = new TestConstraint(mockTimer);
    testConstraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat("when a job w/ a deadline is given, it should not be returned if not ready", jobQueue.nextJobAndIncRunCount(testConstraint), is(nullValue()));
    assertThat("when a job w/ a deadline is given, it should show up in next job ready query", jobQueue.getNextJobDelayUntilNs(testConstraint), is(100 * JobManager.NS_PER_MS));
    mockTimer.incrementMs(100);
    JobHolder nextJob = jobQueue.nextJobAndIncRunCount(testConstraint);
    assertThat("when a job reaches deadline, it should be returned", nextJob, is(notNullValue()));
    assertThat("when a job reaches deadline, it should be returned", nextJob.getId(), is(requireNetwork.getId()));
    assertThat(nextJob.shouldCancelOnDeadline(), is(cancel));
}
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)

Example 27 with JobQueue

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

the class JobQueueTestBase method testNoDeadline.

@Test
public void testNoDeadline() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder requireNetwork = createNewJobHolder(new Params(0).requireNetwork());
    jobQueue.insert(requireNetwork);
    TestConstraint testConstraint = new TestConstraint(mockTimer);
    testConstraint.setMaxNetworkType(NetworkUtil.DISCONNECTED);
    assertThat("when a job w/o a deadline is given, it should not be returned if not ready", jobQueue.nextJobAndIncRunCount(testConstraint), is(nullValue()));
    assertThat("when a job w/o a deadline is given, it should not be returned in next ready", jobQueue.getNextJobDelayUntilNs(testConstraint), is(nullValue()));
}
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 JobQueue

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

the class JobQueueTestBase method testDelayUntilWithUnmeteredNetworkRequirement4.

@Test
public void testDelayUntilWithUnmeteredNetworkRequirement4() {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder1 = createNewJobHolder(new Params(2).overrideDeadlineToRunInMs(3000).requireUnmeteredNetwork().delayInMs(2000));
    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)

Example 29 with JobQueue

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

the class JobQueueTestBase method testRemove.

@Test
public void testRemove() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder = createNewJobHolder();
    jobQueue.insert(holder);
    TestConstraint constraint = new TestConstraint(mockTimer);
    constraint.setExcludeRunning(true);
    assertThat(jobQueue.nextJobAndIncRunCount(constraint).getId(), equalTo(holder.getId()));
    assertThat(jobQueue.nextJobAndIncRunCount(constraint), is(nullValue()));
    jobQueue.remove(holder);
    assertThat(jobQueue.nextJobAndIncRunCount(constraint), is(nullValue()));
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) TestConstraint(com.birbit.android.jobqueue.TestConstraint) JobQueue(com.birbit.android.jobqueue.JobQueue) Test(org.junit.Test)

Example 30 with JobQueue

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

the class JobQueueTestBase method testFindJobHolderById.

@Test
public void testFindJobHolderById() {
    JobQueue jobQueue = createNewJobQueue();
    assertJob(jobQueue, "non existing job", UUID.randomUUID().toString(), null);
    final int LIMIT = 100;
    JobHolder[] holders = new JobHolder[LIMIT];
    String[] ids = new String[LIMIT];
    for (int i = 0; i < LIMIT; i++) {
        holders[i] = createNewJobHolder(new Params((int) (Math.random() * 50)).setPersistent(Math.random() < .5).setRequiresNetwork(Math.random() < .5));
        ids[i] = holders[i].getId();
        jobQueue.insert(holders[i]);
        assertJob(jobQueue, "job by id should work for inserted job", ids[i], holders[i]);
    }
    final int REMOVE_CNT = LIMIT / 2;
    for (int i = 0; i < REMOVE_CNT; i++) {
        int ind = (int) (Math.random() * LIMIT);
        if (holders[ind] == null) {
            continue;
        }
        //remove some randomly, up to half
        jobQueue.remove(holders[ind]);
        holders[ind] = null;
    }
    //re-query all, ensure we can still find non-removed jobs and not find removed jobs
    for (int i = 0; i < LIMIT; i++) {
        if (holders[i] != null) {
            assertJob(jobQueue, "if job is still in the Q, it should be returned", ids[i], holders[i]);
            //re add job
            jobQueue.insertOrReplace(holders[i]);
            //re-test after re-add
            assertJob(jobQueue, "after re-insert, if job is still in the Q, it should be returned", ids[i], holders[i]);
        } else {
            assertJob(jobQueue, "removed job should not be returned in id query", ids[i], null);
        }
    }
    jobQueue.clear();
    for (int i = 0; i < LIMIT; i++) {
        assertJob(jobQueue, "after clear, find by id should return null", ids[i], null);
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) 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

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