Search in sources :

Example 6 with JobManager

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

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

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

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

the class AddInBackgroundTest method addInBackground.

public void addInBackground(boolean delayed, boolean useCallback) throws InterruptedException {
    long currentThreadId = Thread.currentThread().getId();
    final AtomicLong onAddedThreadId = new AtomicLong();
    final CountDownLatch addedLatch = new CountDownLatch(2);
    Job dummyJob = new DummyJob(new Params(1).setDelayMs(delayed ? 1000 : 0)) {

        @Override
        public void onAdded() {
            super.onAdded();
            onAddedThreadId.set(Thread.currentThread().getId());
            addedLatch.countDown();
        }
    };
    JobManager jobManager = createJobManager();
    jobManager.stop();
    final AtomicLong jobId = new AtomicLong(0);
    if (useCallback) {
        jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {

            @Override
            public void onAdded(long id) {
                jobId.set(id);
                addedLatch.countDown();
            }
        });
    } else {
        addedLatch.countDown();
        jobManager.addJobInBackground(dummyJob);
    }
    addedLatch.await();
    MatcherAssert.assertThat("thread ids should be different. delayed:" + delayed, currentThreadId, CoreMatchers.not(onAddedThreadId.get()));
    if (useCallback) {
        JobQueue queue = getNonPersistentQueue(jobManager);
        JobHolder holder = queue.findJobById(jobId.longValue());
        MatcherAssert.assertThat("there should be a job in the holder. id:" + jobId.longValue() + ", delayed:" + delayed + ", use cb:" + useCallback, holder, CoreMatchers.notNullValue());
        MatcherAssert.assertThat("id callback should have the proper id:", holder.getBaseJob(), CoreMatchers.is((BaseJob) dummyJob));
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) AtomicLong(java.util.concurrent.atomic.AtomicLong) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) JobQueue(com.path.android.jobqueue.JobQueue) BaseJob(com.path.android.jobqueue.BaseJob) Params(com.path.android.jobqueue.Params) JobManager(com.path.android.jobqueue.JobManager) AsyncAddCallback(com.path.android.jobqueue.AsyncAddCallback) CountDownLatch(java.util.concurrent.CountDownLatch) BaseJob(com.path.android.jobqueue.BaseJob) DummyJob(com.path.android.jobqueue.test.jobs.DummyJob) Job(com.path.android.jobqueue.Job)

Example 10 with JobManager

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

the class CountTest method testCount.

@Test
public void testCount() throws Exception {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    for (int i = 0; i < 10; i++) {
        jobManager.addJob(new DummyJob(new Params(0).persist()));
        MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 1));
        jobManager.addJob(new DummyJob(new Params(0).persist()));
        MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 2));
    }
    jobManager.start();
    Thread.sleep(2000);
    MatcherAssert.assertThat((int) 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)

Aggregations

JobManager (com.path.android.jobqueue.JobManager)27 Test (org.junit.Test)21 Params (com.path.android.jobqueue.Params)20 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)18 CountDownLatch (java.util.concurrent.CountDownLatch)8 JobHolder (com.path.android.jobqueue.JobHolder)6 Configuration (com.path.android.jobqueue.config.Configuration)6 BaseJob (com.path.android.jobqueue.BaseJob)4 Job (com.path.android.jobqueue.Job)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 DependencyInjector (com.path.android.jobqueue.di.DependencyInjector)2 CustomLogger (com.path.android.jobqueue.log.CustomLogger)2 ArrayList (java.util.ArrayList)2 DaggerTestComponent (com.android.example.devsummit.archdemo.di.component.DaggerTestComponent)1 TestComponent (com.android.example.devsummit.archdemo.di.component.TestComponent)1 ApplicationModule (com.android.example.devsummit.archdemo.di.module.ApplicationModule)1 TestApplicationModule (com.android.example.devsummit.archdemo.di.module.TestApplicationModule)1 LoggingBus (com.android.example.devsummit.archdemo.event.LoggingBus)1 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 JobQueue (com.path.android.jobqueue.JobQueue)1