Search in sources :

Example 21 with JobHolder

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

the class NonPersistentPriorityQueue method nextJobAndIncRunCount.

/**
 * {@inheritDoc}
 */
@Override
public JobHolder nextJobAndIncRunCount(boolean hasNetwork, Collection<String> excludeGroups) {
    JobHolder jobHolder = jobs.peek(hasNetwork, excludeGroups);
    if (jobHolder != null) {
        // check if job can run
        if (jobHolder.getDelayUntilNs() > System.nanoTime()) {
            jobHolder = null;
        } else {
            jobHolder.setRunningSessionId(sessionId);
            jobHolder.setRunCount(jobHolder.getRunCount() + 1);
            jobs.remove(jobHolder);
        }
    }
    return jobHolder;
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder)

Example 22 with JobHolder

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

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

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

Example 25 with JobHolder

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

the class NetworkNextJobTest method testNetworkNextJob.

@Test
public void testNetworkNextJob() throws Exception {
    DummyNetworkUtil dummyNetworkUtil = new DummyNetworkUtil();
    JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).networkUtil(dummyNetworkUtil));
    jobManager.stop();
    DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
    long dummyJobId = jobManager.addJob(dummyJob);
    dummyNetworkUtil.setHasNetwork(false);
    Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
    MatcherAssert.assertThat("when there isn't any network, next job should return null", nextJobMethod.invoke(), nullValue());
    MatcherAssert.assertThat("even if there is network, job manager should return correct count", jobManager.count(), equalTo(1));
    dummyNetworkUtil.setHasNetwork(true);
    JobHolder retrieved = nextJobMethod.invoke();
    MatcherAssert.assertThat("when network is recovered, next job should be retrieved", retrieved, notNullValue());
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) 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