Search in sources :

Example 1 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.

the class InjectorTest method testInjector.

@Test
public void testInjector() throws Exception {
    Configuration.Builder builder = new Configuration.Builder(Robolectric.application);
    final JobManagerTestBase.ObjectReference injectedJobReference = new JobManagerTestBase.ObjectReference();
    final AtomicInteger injectionCallCount = new AtomicInteger(0);
    DependencyInjector dependencyInjector = new DependencyInjector() {

        @Override
        public void inject(BaseJob job) {
            injectedJobReference.setObject(job);
            injectionCallCount.incrementAndGet();
        }
    };
    builder.injector(dependencyInjector);
    JobManager jobManager = createJobManager(builder);
    jobManager.stop();
    jobManager.addJob(new DummyJob(new Params(4)));
    MatcherAssert.assertThat("injection should be called after adding a non-persistent job", injectionCallCount.get(), equalTo(1));
    jobManager.addJob(new DummyJob(new Params(1).persist()));
    MatcherAssert.assertThat("injection should be called after adding a persistent job", injectionCallCount.get(), equalTo(2));
    JobHolder holder = getNextJobMethod(jobManager).invoke();
    MatcherAssert.assertThat("injection should NOT be called for non persistent job", holder.getBaseJob(), not(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called once for non persistent job", injectionCallCount.get(), equalTo(2));
    holder = getNextJobMethod(jobManager).invoke();
    MatcherAssert.assertThat("injection should be called for persistent job", holder.getBaseJob(), equalTo(injectedJobReference.getObject()));
    MatcherAssert.assertThat("injection should be called two times for persistent job", injectionCallCount.get(), equalTo(3));
}
Also used : DependencyInjector(com.path.android.jobqueue.di.DependencyInjector) Configuration(com.path.android.jobqueue.config.Configuration) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) JobHolder(com.path.android.jobqueue.JobHolder) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.Test)

Example 2 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.

the class SessionIdTest method testSessionId.

@Test
public void testSessionId() throws Exception {
    JobManager jobManager = createJobManager();
    Long sessionId = Reflection.field("sessionId").ofType(long.class).in(jobManager).get();
    jobManager.stop();
    Job[] jobs = new Job[] { new DummyJob(new Params(0)), new DummyJob(new Params(0).persist()) };
    for (Job job : jobs) {
        jobManager.addJob(job);
    }
    Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
    for (int i = 0; i < jobs.length; i++) {
        JobHolder jobHolder = nextJobMethod.invoke();
        MatcherAssert.assertThat("session id should be correct for job " + i, jobHolder.getRunningSessionId(), equalTo(sessionId));
    }
}
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) Job(com.path.android.jobqueue.Job) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Test(org.junit.Test)

Example 3 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.

the class SlowOnAddedTest method testPersistent.

@Test
public void testPersistent() throws InterruptedException {
    JobManager jobManager = createJobManager();
    MyDummyPersistentJob.persistentJobLatch = new CountDownLatch(1);
    for (int i = 0; i < 50; i++) {
        jobManager.addJob(new DummyJob(new Params(1).persist()));
    }
    jobManager.addJob(new MyDummyPersistentJob(2));
    MyDummyPersistentJob.persistentJobLatch.await();
    assertThat("even if job is persistent, onAdded should be called b4 onRun", MyDummyPersistentJob.onAddedCountWhenOnRun, equalTo(1));
}
Also used : DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.

the class SlowOnAddedTest method testNonPersistent.

@Test
public void testNonPersistent() throws InterruptedException {
    JobManager jobManager = createJobManager();
    CountDownLatch runLatch = new CountDownLatch(1);
    MyDummyJob job = new MyDummyJob(new Params(2), runLatch);
    for (int i = 0; i < 50; i++) {
        jobManager.addJob(new DummyJob(new Params(1)));
    }
    jobManager.addJob(job);
    runLatch.await();
    assertThat("on added should be called before on run", job.onAddedCntWhenRun, equalTo(1));
}
Also used : DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with DummyJob

use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.

the class JobQueueTestBase method testJobFields.

@Test
public void testJobFields() throws Exception {
    long sessionId = (long) (Math.random() * 1000);
    JobQueue jobQueue = createNewJobQueueWithSessionId(sessionId);
    JobHolder jobHolder = createNewJobHolder();
    int priority = (int) (Math.random() * 1000);
    jobHolder.setPriority(priority);
    DummyJob dummyJob = new DummyJob(new Params(0));
    jobHolder.setBaseJob(dummyJob);
    int runCount = (int) (Math.random() * 10);
    jobHolder.setRunCount(runCount);
    long id = jobQueue.insert(jobHolder);
    for (int i = 0; i < 2; i++) {
        JobHolder received = jobQueue.nextJobAndIncRunCount(true, null);
        assertThat("job id should be preserved", received.getId(), equalTo(id));
        assertThat("job priority should be preserved", received.getPriority(), equalTo(priority));
        assertThat("job session id should be assigned", received.getRunningSessionId(), equalTo(sessionId));
        assertThat("job run count should be incremented", received.getRunCount(), equalTo(runCount + i + 1));
        jobQueue.insertOrReplace(received);
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) Test(org.junit.Test)

Aggregations

Params (com.path.android.jobqueue.Params)22 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)22 Test (org.junit.Test)19 JobManager (com.path.android.jobqueue.JobManager)18 JobHolder (com.path.android.jobqueue.JobHolder)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 Configuration (com.path.android.jobqueue.config.Configuration)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BaseJob (com.path.android.jobqueue.BaseJob)3 Job (com.path.android.jobqueue.Job)3 ArrayList (java.util.ArrayList)3 JobQueue (com.path.android.jobqueue.JobQueue)2 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 AtomicLong (java.util.concurrent.atomic.AtomicLong)1