Search in sources :

Example 6 with JobQueue

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

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

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

the class JobQueueTestBase method testBasicAddRemoveCount.

@Test
public void testBasicAddRemoveCount() throws Exception {
    final int ADD_COUNT = 6;
    JobQueue jobQueue = createNewJobQueue();
    assertThat((int) jobQueue.count(), equalTo(0));
    assertThat(jobQueue.nextJobAndIncRunCount(true, null), nullValue());
    for (int i = 0; i < ADD_COUNT; i++) {
        JobHolder holder = createNewJobHolder();
        jobQueue.insert(holder);
        assertThat((int) jobQueue.count(), equalTo(i + 1));
        assertThat(holder.getId(), notNullValue());
        jobQueue.insertOrReplace(holder);
        assertThat((int) jobQueue.count(), equalTo(i + 1));
    }
    JobHolder firstHolder = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat(firstHolder.getRunCount(), equalTo(1));
    // size should be down 1
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 1));
    // should return another job
    JobHolder secondHolder = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat(secondHolder.getRunCount(), equalTo(1));
    // size should be down 2
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
    // second holder and first holder should have different ids
    assertThat(firstHolder.getId(), not(secondHolder.getId()));
    jobQueue.remove(secondHolder);
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
    jobQueue.remove(secondHolder);
    // non existed job removed, count should be the same
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
    jobQueue.remove(firstHolder);
    assertThat((int) jobQueue.count(), equalTo(ADD_COUNT - 2));
}
Also used : JobHolder(com.path.android.jobqueue.JobHolder) JobQueue(com.path.android.jobqueue.JobQueue) Test(org.junit.Test)

Example 9 with JobQueue

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

the class JobQueueTestBase method testGroupId.

@Test
public void testGroupId() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    long jobId1 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
    long jobId2 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
    long jobId3 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group2")));
    long jobId4 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group2")));
    long jobId5 = jobQueue.insert(createNewJobHolder(new Params(0).groupBy("group1")));
    JobHolder holder1 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group2" }));
    assertThat("first jobs should be from group group2 if group1 is excluded", holder1.getBaseJob().getRunGroupId(), equalTo("group1"));
    assertThat("correct job should be returned if groupId is provided", holder1.getId(), equalTo(jobId1));
    assertThat("no jobs should be returned if all groups are excluded", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })), is(nullValue()));
    long jobId6 = jobQueue.insert(createNewJobHolder(new Params(0)));
    assertThat("both groups are disabled, null group job should be returned", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })).getId(), is(jobId6));
    assertThat("if group1 is excluded, next job should be from group2", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1" })).getBaseJob().getRunGroupId(), equalTo("group2"));
    // to test re-run case, add the job back in
    jobQueue.insertOrReplace(holder1);
    // ask for it again, should return the same holder because it is grouped
    JobHolder holder2 = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat("for grouped jobs, re-fetching job should work fine", holder2.getId(), equalTo(holder1.getId()));
    JobHolder holder3 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1" }));
    assertThat("if a group it excluded, next available from another group should be returned", holder3.getId(), equalTo(jobId4));
    // add two more non-grouped jobs
    long jobId7 = jobQueue.insert(createNewJobHolder(new Params(0)));
    long jobId8 = jobQueue.insert(createNewJobHolder(new Params(0)));
    JobHolder holder4 = jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" }));
    assertThat("if all grouped jobs are excluded, non-grouped jobs should be returned", holder4.getId(), equalTo(jobId7));
    jobQueue.insertOrReplace(holder4);
    // for non-grouped jobs, run counts should be respected
    assertThat("if all grouped jobs are excluded, re-inserted highest priority job should still be returned", jobQueue.nextJobAndIncRunCount(true, Arrays.asList(new String[] { "group1", "group2" })).getId(), equalTo(jobId7));
}
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 10 with JobQueue

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

the class JobQueueTestBase method testNetwork.

@Test
public void testNetwork() throws Exception {
    JobQueue jobQueue = createNewJobQueue();
    JobHolder jobHolder = createNewJobHolder(new Params(0));
    jobQueue.insert(jobHolder);
    assertThat("no network job should be returned even if there is no netowrk", jobQueue.nextJobAndIncRunCount(false, null), notNullValue());
    jobQueue.remove(jobHolder);
    jobHolder = createNewJobHolder(new Params(0).requireNetwork());
    assertThat("if there isn't any network, job with network requirement should not return", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
    assertThat("if there is network, job with network requirement should be returned", jobQueue.nextJobAndIncRunCount(true, null), nullValue());
    jobQueue.remove(jobHolder);
    jobHolder = createNewJobHolder(new Params(1));
    JobHolder jobHolder2 = createNewJobHolder(new Params(5).requireNetwork());
    long firstJobId = jobQueue.insert(jobHolder);
    long secondJobId = jobQueue.insert(jobHolder2);
    JobHolder retrieved = jobQueue.nextJobAndIncRunCount(false, null);
    assertThat("one job should be returned w/o network", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("no network job should be returned although it has lower priority", retrieved.getId(), equalTo(firstJobId));
    }
    assertThat("no other job should be returned w/o network", jobQueue.nextJobAndIncRunCount(false, null), nullValue());
    retrieved = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat("if network is back, network requiring job should be returned", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("when there is network, network job should be returned", retrieved.getId(), equalTo(secondJobId));
    }
    // add first job back
    jobQueue.insertOrReplace(jobHolder);
    // add second job back
    jobQueue.insertOrReplace(jobHolder2);
    retrieved = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat("if network is back, job w/ higher priority should be returned", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("if network is back, job w/ higher priority should be returned", retrieved.getId(), equalTo(secondJobId));
    }
    jobQueue.insertOrReplace(jobHolder2);
    JobHolder highestPriorityJob = createNewJobHolder(new Params(10));
    long highestPriorityJobId = jobQueue.insert(highestPriorityJob);
    retrieved = jobQueue.nextJobAndIncRunCount(true, null);
    assertThat("w/ or w/o network, highest priority should be returned", retrieved, notNullValue());
    if (retrieved != null) {
        assertThat("w/ or w/o network, highest priority should be returned", retrieved.getId(), equalTo(highestPriorityJobId));
    }
// TODO test delay until
}
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

JobQueue (com.path.android.jobqueue.JobQueue)17 Test (org.junit.Test)16 JobHolder (com.path.android.jobqueue.JobHolder)15 Params (com.path.android.jobqueue.Params)13 DummyJob (com.path.android.jobqueue.test.jobs.DummyJob)2 AsyncAddCallback (com.path.android.jobqueue.AsyncAddCallback)1 BaseJob (com.path.android.jobqueue.BaseJob)1 Job (com.path.android.jobqueue.Job)1 JobManager (com.path.android.jobqueue.JobManager)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1