Search in sources :

Example 31 with Params

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

the class SqliteJobQueueTest method testCustomSerializer.

@Test
public void testCustomSerializer() throws Exception {
    final CountDownLatch calledForSerialize = new CountDownLatch(1);
    final CountDownLatch calledForDeserialize = new CountDownLatch(1);
    SqliteJobQueue.JobSerializer jobSerializer = new SqliteJobQueue.JavaSerializer() {

        @Override
        public byte[] serialize(Object object) throws IOException {
            calledForSerialize.countDown();
            return super.serialize(object);
        }

        @Override
        public <T extends BaseJob> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
            calledForDeserialize.countDown();
            return super.deserialize(bytes);
        }
    };
    SqliteJobQueue jobQueue = new SqliteJobQueue(Robolectric.application, System.nanoTime(), "__" + System.nanoTime(), jobSerializer);
    jobQueue.insert(createNewJobHolder(new Params(0)));
    calledForSerialize.await(1, TimeUnit.SECONDS);
    MatcherAssert.assertThat("custom serializer should be called for serialize", (int) calledForSerialize.getCount(), CoreMatchers.equalTo(0));
    MatcherAssert.assertThat("custom serializer should NOT be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(1));
    jobQueue.nextJobAndIncRunCount(true, null);
    MatcherAssert.assertThat("custom serializer should be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(0));
}
Also used : SqliteJobQueue(com.path.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 32 with Params

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

the class AddedCountTest method testAddedCount.

@Test
public void testAddedCount() throws Exception {
    testAddedCount(new DummyJob(new Params(0)));
    testAddedCount(new DummyJob(new Params(0).persist()));
}
Also used : DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Example 33 with Params

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

the class ClearTest method testClear.

@Test
public void testClear() throws Exception {
    JobManager jobManager = createJobManager();
    final int LIMIT = 20;
    for (int i = 0; i < LIMIT; i++) {
        jobManager.addJob(new DummyJob(new Params(0).setPersistent(i % 2 == 1)));
    }
    jobManager.clear();
    MatcherAssert.assertThat("after clear, count should be 0", jobManager.count(), equalTo(0));
}
Also used : DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) Test(org.junit.Test)

Example 34 with Params

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

the class GroupingTest method testGrouping.

@Test
public void testGrouping() throws Exception {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
    Invoker<Void> removeJobMethod = getRemoveJobMethod(jobManager);
    long jobId1 = jobManager.addJob(new DummyJob(new Params(0).groupBy("group1")));
    long jobId2 = jobManager.addJob(new DummyJob(new Params(0).groupBy("group1")));
    long jobId3 = jobManager.addJob(new DummyJob(new Params(0).persist().groupBy("group2")));
    long jobId4 = jobManager.addJob(new DummyJob(new Params(0).persist().groupBy("group1")));
    JobHolder nextJob = nextJobMethod.invoke();
    MatcherAssert.assertThat("next job should be the first job from group1", nextJob.getId(), equalTo(jobId1));
    JobHolder group2Job = nextJobMethod.invoke();
    MatcherAssert.assertThat("since group 1 is running now, next job should be from group 2", group2Job.getId(), equalTo(jobId3));
    removeJobMethod.invoke(nextJob);
    JobHolder group1NextJob = nextJobMethod.invoke();
    MatcherAssert.assertThat("after removing job from group 1, another job from group1 should be returned", group1NextJob.getId(), equalTo(jobId2));
    MatcherAssert.assertThat("when jobs from both groups are running, no job should be returned from next job", nextJobMethod.invoke(), is(nullValue()));
    removeJobMethod.invoke(group2Job);
    MatcherAssert.assertThat("even after group2 job is complete, no jobs should be returned since we only have group1 jobs left", nextJobMethod.invoke(), is(nullValue()));
}
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)

Example 35 with Params

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

the class RunManyNonPersistentTest method runManyNonPersistentJobs.

@Test
public void runManyNonPersistentJobs() throws Exception {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    int limit = 2;
    final CountDownLatch latch = new CountDownLatch(limit);
    for (int i = 0; i < limit; i++) {
        jobManager.addJob(new DummyLatchJob(new Params(i), latch));
    }
    jobManager.start();
    latch.await(10, TimeUnit.SECONDS);
    MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
Also used : Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) 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