Search in sources :

Example 6 with Params

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

Example 7 with Params

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

the class JobQueueTestBase method testDelayUntil.

@Test
public void testDelayUntil() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    long now = System.nanoTime();
    JobHolder networkJobHolder = createNewJobHolderWithDelayUntil(new Params(0).requireNetwork(), now + 200000 * JobManager.NS_PER_MS);
    JobHolder noNetworkJobHolder = createNewJobHolderWithDelayUntil(new Params(0), now + 500000 * JobManager.NS_PER_MS);
    jobQueue.insert(networkJobHolder);
    jobQueue.insert(noNetworkJobHolder);
    assertThat("if there is no network, delay until should be provided for no network job", jobQueue.getNextJobDelayUntilNs(false), equalTo(noNetworkJobHolder.getDelayUntilNs()));
    assertThat("if there is network, delay until should be provided for network job because it is " + "sooner", jobQueue.getNextJobDelayUntilNs(true), equalTo(networkJobHolder.getDelayUntilNs()));
    JobHolder noNetworkJobHolder2 = createNewJobHolderWithDelayUntil(new Params(0), now + 100000 * JobManager.NS_PER_MS);
    jobQueue.insert(noNetworkJobHolder2);
    assertThat("if there is network, any job's delay until should be returned", jobQueue.getNextJobDelayUntilNs(true), equalTo(noNetworkJobHolder2.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 8 with Params

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

Example 9 with Params

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

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

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