Search in sources :

Example 6 with JobHolder

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

the class JobQueueTestBase method testSessionId.

@Test
public void testSessionId() throws Exception {
    long sessionId = (long) (Math.random() * 100000);
    JobQueue jobQueue = createNewJobQueueWithSessionId(sessionId);
    JobHolder jobHolder = createNewJobHolder();
    jobQueue.insert(jobHolder);
    jobHolder = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat("session id should be attached to next job", jobHolder.getRunningSessionId(), equalTo(sessionId));
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Test(org.junit.Test)

Example 7 with JobHolder

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

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

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

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

the class DelayTest method testDelay.

public void testDelay(boolean persist) throws Exception {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(1000).setPersistent(persist));
    DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
    long jobId = jobManager.addJob(delayedJob);
    long nonDelayedJobId = jobManager.addJob(nonDelayedJob);
    Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
    Invoker<Void> removeJobMethod = getRemoveJobMethod(jobManager);
    JobHolder receivedJob = nextJobMethod.invoke();
    MatcherAssert.assertThat("non-delayed job should be served", receivedJob, notNullValue());
    MatcherAssert.assertThat("non-delayed job should id should match", receivedJob.getId(), equalTo(nonDelayedJobId));
    removeJobMethod.invoke(receivedJob);
    MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    Thread.sleep(500);
    MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    Thread.sleep(2000);
    MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
    receivedJob = nextJobMethod.invoke();
    MatcherAssert.assertThat("now should be able to receive the delayed job.", receivedJob, notNullValue());
    if (receivedJob != null) {
        MatcherAssert.assertThat("received job should be the delayed job", receivedJob.getId(), equalTo(jobId));
    }
}
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)

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