Search in sources :

Example 11 with JobHolder

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

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));
    assertThat(jobQueue.nextJobAndIncRunCount(true, null), nullValue());
    for (int i = 0; i < ADD_COUNT; i++) {
        JobHolder holder = createNewJobHolder();
        jobQueue.insert(holder);
        assertThat((int) jobQueue.count(), equalTo(i + 1));
        assertThat(holder.getId(), notNullValue());
        jobQueue.insertOrReplace(holder);
        assertThat((int) jobQueue.count(), equalTo(i + 1));
    }
    JobHolder firstHolder = jobQueue.nextJobAndIncRunCount(true, null);
    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(true, null);
    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 : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Test(org.junit.Test)

Example 12 with JobHolder

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

the class JobQueueTestBase method testGroupId.

@Test
public void testGroupId() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    long jobId1 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
    long jobId2 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
    long jobId3 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group2")));
    long jobId4 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group2")));
    long jobId5 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
    JobHolder holder1 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group2" }));
    assertThat("first jobs should be from group group2 if group1 is excluded", holder1.getBaseJob().getRunGroupId(), equalTo("group1"));
    assertThat("correct job should be returned if groupId is provided", holder1.getId(), equalTo(jobId1));
    assertThat("no jobs should be returned if all groups are excluded", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })), is(nullValue()));
    long jobId6 = jobQueue.insert(createNewJobHolder(new Params(0)));
    assertThat("both groups are disabled, null group job should be returned", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })).getId(), is(jobId6));
    assertThat("if group1 is excluded, next job should be from group2", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1" })).getBaseJob().getRunGroupId(), equalTo("group2"));
    // to test re-run case, add the job back in
    jobQueue.insertOrReplace(holder1);
    // ask for it again, should return the same holder because it is grouped
    JobHolder holder2 = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat("for grouped jobs, re-fetching job should work fine", holder2.getId(), equalTo(holder1.getId()));
    JobHolder holder3 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1" }));
    assertThat("if a group it excluded, next available from another group should be returned", holder3.getId(), equalTo(jobId4));
    // add two more non-grouped jobs
    long jobId7 = jobQueue.insert(createNewJobHolder(new Params(0)));
    long jobId8 = jobQueue.insert(createNewJobHolder(new Params(0)));
    JobHolder holder4 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" }));
    assertThat("if all grouped jobs are excluded, non-grouped jobs should be returned", holder4.getId(), equalTo(jobId7));
    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(true, Arrays.asList(new String[] { "group1", "group2" })).getId(), equalTo(jobId7));
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 13 with JobHolder

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

the class JobQueueTestBase method testNetwork.

@Test
public void testNetwork() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder = createNewJobHolder(new Params(0));
    jobQueue.insert(jobHolder);
    assertThat("no network job should be returned even if there is no netowrk", jobQueue.nextJobAndIncRunCount(false, null), 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(false, null), nullValue());
    assertThat("if there is network, job with network requirement should be returned", jobQueue.nextJobAndIncRunCount(true, null), nullValue());
    jobQueue.remove(jobHolder);
    jobHolder = createNewJobHolder(new Params(1));
    JobHolder jobHolder2 = createNewJobHolder(new Params(5).requireNetwork());
    long firstJobId = jobQueue.insert(jobHolder);
    long secondJobId = jobQueue.insert(jobHolder2);
    JobHolder retrieved = jobQueue.nextJobAndIncRunCount(false, null);
    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(firstJobId));
    }
    assertThat("no other job should be returned w/o network", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
    retrieved = jobQueue.nextJobAndIncRunCount(true, null);
    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(secondJobId));
    }
    // add first job back
    jobQueue.insertOrReplace(jobHolder);
    // add second job back
    jobQueue.insertOrReplace(jobHolder2);
    retrieved = jobQueue.nextJobAndIncRunCount(true, null);
    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(secondJobId));
    }
    jobQueue.insertOrReplace(jobHolder2);
    JobHolder highestPriorityJob = createNewJobHolder(new Params(10));
    long highestPriorityJobId = jobQueue.insert(highestPriorityJob);
    retrieved = jobQueue.nextJobAndIncRunCount(true, null);
    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(highestPriorityJobId));
    }
// TODO test delay until
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 14 with JobHolder

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

the class JobQueueTestBase method testFindJobHolderById.

@Test
public void testFindJobHolderById() {
    JobQueue jobQueue = createNewJobQueue();
    assertJob(jobQueue, "non existing job (negative id)", -4, null);
    assertJob(jobQueue, "non existing job (positive id)", +4, null);
    final int LIMIT = 100;
    JobHolder[] holders = new JobHolder[LIMIT];
    long[] ids = new long[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] = 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.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 15 with JobHolder

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

the class NonPersistentJobQueueTest method testTooManyQueueChanges.

/**
 * issue #21 https://github.com/path/android-priority-jobqueue/issues/21
 */
@Test
public void testTooManyQueueChanges() throws InterruptedException {
    JobQueue jobQueue = createNewJobQueue();
    int limit = 10000;
    long delayMs = 2000;
    long then = System.nanoTime() + delayMs * JobManager.NS_PER_MS;
    for (int i = 0; i < limit; i++) {
        jobQueue.insert(createNewJobHolder(new Params(0).requireNetwork().delayInMs(delayMs)));
    }
    MatcherAssert.assertThat("all jobs require network, should return null", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
    long sleep = then - System.nanoTime();
    sleep += JobManager.NS_PER_MS * 1000;
    if (sleep > 0) {
        Thread.sleep(sleep / JobManager.NS_PER_MS);
    }
    // should be able to get it w/o an overflow
    for (int i = 0; i < limit; i++) {
        JobHolder holder = jobQueue.nextJobAndIncRunCount(true, null);
        MatcherAssert.assertThat("should get a next job", holder, notNullValue());
        jobQueue.remove(holder);
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Aggregations

JobHolder (com.path.android.jobqueue.JobHolder)27 Test (org.junit.Test)18 Params (com.path.android.jobqueue.Params)17 JobQueue (com.path.android.jobqueue.JobQueue)15 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)7 JobManager (com.path.android.jobqueue.JobManager)6 BaseJob (com.path.android.jobqueue.BaseJob)2 Job (com.path.android.jobqueue.Job)2 Cursor (android.database.Cursor)1 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 Configuration (com.path.android.jobqueue.config.Configuration)1 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)1 HashSet (java.util.HashSet)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1