Search in sources :

Example 26 with Params

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

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

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

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

the class JobQueueTestBase method testCountReadyJobs.

@Test
public void testCountReadyJobs() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    assertThat("initial count should be 0 for ready jobs", jobQueue.countReadyJobs(true, null), equalTo(0));
    // add some jobs
    jobQueue.insert(createNewJobHolder());
    jobQueue.insert(createNewJobHolder(new Params(0).requireNetwork()));
    long now = System.nanoTime();
    long delay = 1000;
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0), now + TimeUnit.MILLISECONDS.toNanos(delay)));
    assertThat("ready count should be 1 if there is no network", jobQueue.countReadyJobs(false, null), equalTo(1));
    assertThat("ready count should be 2 if there is network", jobQueue.countReadyJobs(true, null), equalTo(2));
    Thread.sleep(delay);
    assertThat("when needed delay time passes, ready count should be 3", jobQueue.countReadyJobs(true, null), equalTo(3));
    assertThat("when needed delay time passes but no network, ready count should be 2", jobQueue.countReadyJobs(false, null), equalTo(2));
    jobQueue.insert(createNewJobHolder(new Params(5).groupBy("group1")));
    jobQueue.insert(createNewJobHolder(new Params(5).groupBy("group1")));
    assertThat("when more than 1 job from same group is created, ready jobs should increment only by 1", jobQueue.countReadyJobs(true, null), equalTo(4));
    assertThat("excluding groups should work", jobQueue.countReadyJobs(true, Arrays.asList(new String[] { "group1" })), equalTo(3));
    assertThat("giving a non-existing group should not fool the count", jobQueue.countReadyJobs(true, Arrays.asList(new String[] { "group3423" })), equalTo(4));
    jobQueue.insert(createNewJobHolder(new Params(3).groupBy("group2")));
    assertThat("when a job from another group is added, ready job count should inc", jobQueue.countReadyJobs(true, null), equalTo(5));
    now = System.nanoTime();
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(3).groupBy("group3"), now + TimeUnit.MILLISECONDS.toNanos(delay)));
    assertThat("when a delayed job from another group is added, ready count should not change", jobQueue.countReadyJobs(true, null), equalTo(5));
    jobQueue.insert(createNewJobHolder(new Params(3).groupBy("group3")));
    assertThat("when another job from delayed group is added, ready job count should inc", jobQueue.countReadyJobs(true, null), equalTo(6));
    Thread.sleep(delay);
    assertThat("when delay passes and a job from existing group becomes available, ready job count should not change", jobQueue.countReadyJobs(true, null), equalTo(6));
    assertThat("when some groups are excluded, count should be correct", jobQueue.countReadyJobs(true, Arrays.asList(new String[] { "group1", "group3" })), equalTo(4));
    // jobs w/ same group id but with different persistence constraints should not fool the count
    now = System.nanoTime();
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).persist().groupBy("group10"), now + 1000));
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).groupBy("group10"), now + 1000));
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).persist().groupBy("group10"), now - 1000));
    jobQueue.insert(createNewJobHolderWithDelayUntil(new Params(0).groupBy("group10"), now - 1000));
    assertThat("when many jobs are added w/ different constraints but same group id, ready count should not be fooled", jobQueue.countReadyJobs(true, Arrays.asList(new String[] { "group1", "group3" })), equalTo(5));
    assertThat("when many jobs are added w/ different constraints but same group id, ready count should not be fooled", jobQueue.countReadyJobs(true, null), equalTo(7));
    assertThat("when many jobs are added w/ different constraints but same group id, ready count should not be fooled", jobQueue.countReadyJobs(false, Arrays.asList(new String[] { "group1", "group3" })), equalTo(4));
}
Also used : JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 30 with Params

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

Params (com.path.android.jobqueue.Params)36 Test (org.junit.Test)33 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)22 JobManager (com.path.android.jobqueue.JobManager)20 JobHolder (com.path.android.jobqueue.JobHolder)17 JobQueue (com.path.android.jobqueue.JobQueue)13 CountDownLatch (java.util.concurrent.CountDownLatch)8 Configuration (com.path.android.jobqueue.config.Configuration)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 BaseJob (com.path.android.jobqueue.BaseJob)4 Job (com.path.android.jobqueue.Job)3 ArrayList (java.util.ArrayList)3 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)2 CustomLogger (com.path.android.jobqueue.log.CustomLogger)2 Semaphore (java.util.concurrent.Semaphore)2 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 JobStatus (com.path.android.jobqueue.JobStatus)1 JobConsumerExecutor (com.path.android.jobqueue.executor.JobConsumerExecutor)1 SqliteJobQueue (com.path.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)1 LinkedList (java.util.LinkedList)1