Search in sources :

Example 1 with JobHolder

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

the class JobQueueTestBase method testDelayUntilWithPriority.

@Test
public void testDelayUntilWithPriority() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    long now = System.nanoTime();
    JobHolder lowPriorityHolder = createNewJobHolderWithDelayUntil(new Params(5), now + 10000 * JobManager.NS_PER_MS);
    JobHolder highPriorityHolder = createNewJobHolderWithDelayUntil(new Params(10), now + 20000 * JobManager.NS_PER_MS);
    jobQueue.insert(lowPriorityHolder);
    jobQueue.insert(highPriorityHolder);
    assertThat("when asked, if lower priority job has less delay until, we should return it", jobQueue.getNextJobDelayUntilNs(true), equalTo(lowPriorityHolder.getDelayUntilNs()));
}
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 2 with JobHolder

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

the class JobQueueTestBase method testRemove.

@Test
public void testRemove() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder holder = createNewJobHolder();
    jobQueue.insert(holder);
    Long jobId = holder.getId();
    assertThat(jobQueue.nextJobAndIncRunCount(true, null).getId(), equalTo(jobId));
    assertThat(jobQueue.nextJobAndIncRunCount(true, null), is(nullValue()));
    jobQueue.remove(holder);
    assertThat(jobQueue.nextJobAndIncRunCount(true, null), is(nullValue()));
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Test(org.junit.Test)

Example 3 with JobHolder

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

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

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

the class JobQueueTestBase method testPriorityWithDelayedJobs.

@Test
public void testPriorityWithDelayedJobs() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder delayedPriority_5 = createNewJobHolder(new Params(5));
    org.fest.reflect.field.Invoker<Long> delayUntilField = getDelayUntilNsField(delayedPriority_5);
    delayUntilField.set(System.nanoTime() - 1000);
    JobHolder delayedPriority_2 = createNewJobHolder(new Params(2));
    delayUntilField = getDelayUntilNsField(delayedPriority_2);
    delayUntilField.set(System.nanoTime() - 500);
    JobHolder nonDelayedPriority_6 = createNewJobHolder(new Params(6));
    JobHolder nonDelayedPriority_3 = createNewJobHolder(new Params(3));
    JobHolder nonDelayedPriority_2 = createNewJobHolder(new Params(2));
    jobQueue.insert(delayedPriority_5);
    jobQueue.insert(delayedPriority_2);
    jobQueue.insert(nonDelayedPriority_6);
    jobQueue.insert(nonDelayedPriority_2);
    jobQueue.insert(nonDelayedPriority_3);
    int lastPriority = Integer.MAX_VALUE;
    for (int i = 0; i < 5; i++) {
        JobHolder next = jobQueue.nextJobAndIncRunCount(true, null);
        assertThat("next job should not be null", next, notNullValue());
        assertThat("next job's priority should be lower then previous for job " + i, next.getPriority() <= lastPriority, is(true));
        lastPriority = next.getPriority();
    }
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Params(com.path.android.jobqueue.Params) 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