Search in sources :

Example 11 with JobQueue

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

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

Example 13 with JobQueue

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

the class JobQueueTestBase method testDueDelayUntilWithPriority.

@Test
public void testDueDelayUntilWithPriority() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    long now = System.nanoTime();
    JobHolder lowPriorityHolder = createNewJobHolderWithDelayUntil(new Params(5), now - 1000 * JobManager.NS_PER_MS);
    JobHolder highPriorityHolder = createNewJobHolderWithDelayUntil(new Params(10), now - 10000 * JobManager.NS_PER_MS);
    jobQueue.insert(lowPriorityHolder);
    jobQueue.insert(highPriorityHolder);
    long soonJobDelay = 2000;
    JobHolder highestPriorityDelayedJob = createNewJobHolderWithDelayUntil(new Params(12), now + soonJobDelay * JobManager.NS_PER_MS);
    long highestPriorityDelayedJobId = jobQueue.insert(highestPriorityDelayedJob);
    assertThat("when asked, if job's due has passed, highest priority jobs's delay until should be " + "returned", jobQueue.getNextJobDelayUntilNs(true), equalTo(highPriorityHolder.getDelayUntilNs()));
    // make sure soon job is valid now
    Thread.sleep(soonJobDelay);
    assertThat("when a job's time come, it should be returned", jobQueue.nextJobAndIncRunCount(true, null).getId(), equalTo(highestPriorityDelayedJobId));
}
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 JobQueue

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

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(true, null);
        assertThat(holder.getPriority() <= minPriority, is(true));
        jobQueue.insertOrReplace(holder);
    }
    assertThat(jobQueue.nextJobAndIncRunCount(true, null), notNullValue());
}
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 JobQueue

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

the class JobQueueTestBase method testPriority.

@Test
public void testPriority() 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(true, null);
        assertThat(holder.getPriority() <= minPriority, is(true));
    }
    assertThat(jobQueue.nextJobAndIncRunCount(true, null), nullValue());
}
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

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