Search in sources :

Example 21 with JobManager

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

the class PriorityTest method testPriority.

@Test
public void testPriority() throws Exception {
    JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).maxConsumerCount(1).timer(mockTimer));
    testPriority(jobManager, false);
}
Also used : Configuration(com.birbit.android.jobqueue.config.Configuration) JobManager(com.birbit.android.jobqueue.JobManager) Test(org.junit.Test)

Example 22 with JobManager

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

the class RetryLogicTest method testChangePriorityAndObserveExecutionOrder.

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void testChangePriorityAndObserveExecutionOrder(boolean persistent) throws InterruptedException {
    cancelLatch = new CountDownLatch(2);
    RetryJob job1 = new RetryJob(new Params(10).setPersistent(persistent).groupBy("group"));
    job1.identifier = "1";
    RetryJob job2 = new RetryJob(new Params(5).setPersistent(persistent).groupBy("group"));
    job2.identifier = "2";
    JobManager jobManager = createJobManager();
    jobManager.stop();
    jobManager.addJob(job1);
    jobManager.addJob(job2);
    retryProvider = new RetryProvider() {

        @Override
        public RetryConstraint build(Job job, Throwable throwable, int runCount, int maxRunCount) {
            RetryJob retryJob = (RetryJob) job;
            if ("1".equals(retryJob.identifier)) {
                if (retryJob.getPriority() == 1) {
                    return RetryConstraint.CANCEL;
                }
                RetryConstraint retryConstraint = new RetryConstraint(true);
                retryConstraint.setNewPriority(1);
                return retryConstraint;
            } else {
                return RetryConstraint.CANCEL;
            }
        }
    };
    final List<String> runOrder = new ArrayList<>();
    onRunCallback = new Callback() {

        @Override
        public void on(Job job) {
            runOrder.add(((RetryJob) job).identifier);
        }
    };
    canRun = true;
    jobManager.start();
    assertThat("both jobs should be canceled eventually", cancelLatch.await(3, TimeUnit.MINUTES), is(true));
    assertThat("jobs should run a total of 3 times", runCount, is(3));
    final List<String> expectedRunOrder = Arrays.asList("1", "2", "1");
    assertThat("expected run order count should match", runOrder.size(), is(expectedRunOrder.size()));
    for (int i = 0; i < expectedRunOrder.size(); i++) {
        assertThat("at iteration " + i + ", this job should run", runOrder.get(i), is(expectedRunOrder.get(i)));
    }
}
Also used : ArrayList(java.util.ArrayList) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) TargetApi(android.annotation.TargetApi)

Example 23 with JobManager

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

the class SingleIdTest method testSingleIdRunning.

private void testSingleIdRunning(boolean persistent) throws InterruptedException {
    JobManager jobManager = createJobManager();
    String singleId = "dorks";
    CountDownLatch latchWait = new CountDownLatch(1);
    CountDownLatch latchRunning = new CountDownLatch(1);
    DummyJob dummyJob1 = new SerializableDummyTwoLatchJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId), latchWait, latchRunning);
    addJob(jobManager, dummyJob1);
    jobManager.start();
    //let job1 start running
    latchRunning.await(5, TimeUnit.SECONDS);
    jobManager.stop();
    assertThat("should not be marked ready", jobManager.count(), is(0));
    CountDownLatch latchRunning2 = new CountDownLatch(1);
    DummyJob dummyJob2 = new SerializableDummyLatchJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId), latchRunning2);
    addJob(jobManager, dummyJob2);
    assertThat("should add new job if first job was running", jobManager.count(), is(1));
    DummyJob dummyJob3 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId));
    addJob(jobManager, dummyJob3);
    assertThat("should not add new job if already queued", jobManager.count(), is(1));
    //let job1 finish
    latchWait.countDown();
    jobManager.start();
    //wait until job2 runs
    assertThat("job should have run", latchRunning2.await(5, TimeUnit.SECONDS), is(true));
    jobManager.stopAndWaitUntilConsumersAreFinished();
    assertThat("job should not have run", dummyJob3.getOnRunCnt(), is(0));
    assertThat("should have called onCancel", dummyJob3.getOnCancelCnt(), is(1));
    DummyJob dummyJob4 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId).setGroupId(singleId));
    addJob(jobManager, dummyJob4);
    assertThat("should be added if all others have run", jobManager.count(), is(1));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with JobManager

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

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.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Test(org.junit.Test)

Example 25 with JobManager

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

the class AddedCountTest method testAddedCount.

private void testAddedCount(DummyJob dummyJob) {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    jobManager.addJob(dummyJob);
    MatcherAssert.assertThat(1, equalTo(dummyJob.getOnAddedCnt()));
}
Also used : JobManager(com.birbit.android.jobqueue.JobManager)

Aggregations

JobManager (com.birbit.android.jobqueue.JobManager)58 Params (com.birbit.android.jobqueue.Params)47 Test (org.junit.Test)40 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)35 Job (com.birbit.android.jobqueue.Job)26 CountDownLatch (java.util.concurrent.CountDownLatch)26 Configuration (com.birbit.android.jobqueue.config.Configuration)22 CancelResult (com.birbit.android.jobqueue.CancelResult)9 TargetApi (android.annotation.TargetApi)8 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)8 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)8 JobHolder (com.birbit.android.jobqueue.JobHolder)6 ArrayList (java.util.ArrayList)6 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Semaphore (java.util.concurrent.Semaphore)3 SuppressLint (android.annotation.SuppressLint)2 NonNull (android.support.annotation.NonNull)2 Constraint (com.birbit.android.jobqueue.Constraint)2 TagConstraint (com.birbit.android.jobqueue.TagConstraint)2