Search in sources :

Example 6 with JobManager

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

the class CallbackTest method cancelViaShouldReRun.

@Test
public void cancelViaShouldReRun() throws Throwable {
    JobManagerCallback callback = mock(JobManagerCallback.class);
    final PublicJob job = spy(new PublicJob(new Params(0)));
    doNothing().when(job).onAdded();
    doThrow(new Exception()).when(job).onRun();
    doReturn(3).when(job).getRetryLimit();
    doReturn(RetryConstraint.CANCEL).when(job).shouldReRunOnThrowable(any(Throwable.class), anyInt(), anyInt());
    final JobManager jobManager = createJobManager();
    jobManager.addCallback(callback);
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            jobManager.addJob(job);
        }

        @Override
        public void assertJob(Job job) {
        }
    });
    verify(callback).onJobAdded(job);
    verify(callback, times(1)).onJobRun(job, JobManagerCallback.RESULT_CANCEL_CANCELLED_VIA_SHOULD_RE_RUN);
    verify(callback).onJobCancelled(eq(job), eq(false), any(Throwable.class));
}
Also used : Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) JobManagerCallback(com.birbit.android.jobqueue.callback.JobManagerCallback) Test(org.junit.Test)

Example 7 with JobManager

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

the class CallbackTest method cancelViaCancelCall.

@Test
public void cancelViaCancelCall() throws Throwable {
    JobManagerCallback callback = mock(JobManagerCallback.class);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch endLatch = new CountDownLatch(1);
    final Throwable[] jobError = new Throwable[1];
    PublicJob job = spy(new PublicJob(new Params(1).addTags("tag1")) {

        @Override
        public void onRun() throws Throwable {
            startLatch.countDown();
            try {
                Assert.assertThat(endLatch.await(30, TimeUnit.SECONDS), CoreMatchers.is(true));
            } catch (Throwable t) {
                jobError[0] = t;
            }
            throw new Exception("blah");
        }
    });
    doCallRealMethod().when(job).onRun();
    doReturn(3).when(job).getRetryLimit();
    JobManager jobManager = createJobManager();
    jobManager.addCallback(callback);
    jobManager.addJob(job);
    Assert.assertThat(startLatch.await(30, TimeUnit.SECONDS), CoreMatchers.is(true));
    jobManager.cancelJobsInBackground(null, TagConstraint.ANY, "tag1");
    //noinspection StatementWithEmptyBody
    while (!job.isCancelled()) {
        // busy wait until cancel arrives
        //noinspection SLEEP_IN_CODE
        Thread.sleep(100);
    }
    endLatch.countDown();
    while (jobManager.getJobStatus(job.getId()) != JobStatus.UNKNOWN) {
        // busy wait until job cancel is handled
        //noinspection SLEEP_IN_CODE
        Thread.sleep(100);
    }
    MatcherAssert.assertThat(jobError[0], CoreMatchers.nullValue());
    verify(job, times(0)).shouldReRunOnThrowable(any(Throwable.class), anyInt(), anyInt());
    jobManager.stopAndWaitUntilConsumersAreFinished();
    verify(callback).onJobAdded(job);
    verify(callback, times(1)).onJobRun(job, JobManagerCallback.RESULT_CANCEL_CANCELLED_WHILE_RUNNING);
    verify(callback).onJobCancelled(job, true, null);
}
Also used : Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) JobManagerCallback(com.birbit.android.jobqueue.callback.JobManagerCallback) Test(org.junit.Test)

Example 8 with JobManager

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

the class CallbackTest method successNonPersistent.

@Test
public void successNonPersistent() throws Throwable {
    JobManagerCallback callback = mock(JobManagerCallback.class);
    final Job job = spy(new PublicJob(new Params(0)));
    doNothing().when(job).onAdded();
    doNothing().when(job).onRun();
    final JobManager jobManager = createJobManager();
    jobManager.addCallback(callback);
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            jobManager.addJob(job);
        }

        @Override
        public void assertJob(Job job) {
        }
    });
    verify(job).onAdded();
    verify(job).onRun();
    verify(callback).onJobAdded(job);
    verify(callback).onJobRun(job, JobManagerCallback.RESULT_SUCCEED);
}
Also used : Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) JobManagerCallback(com.birbit.android.jobqueue.callback.JobManagerCallback) Test(org.junit.Test)

Example 9 with JobManager

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

the class CallbackTest method cancelViaRetryLimit.

@Test
public void cancelViaRetryLimit() throws Throwable {
    final Throwable error = new Exception();
    JobManagerCallback callback = mock(JobManagerCallback.class);
    final PublicJob job = spy(new PublicJob(new Params(0)));
    doNothing().when(job).onAdded();
    doThrow(error).when(job).onRun();
    doReturn(3).when(job).getRetryLimit();
    doReturn(RetryConstraint.RETRY).when(job).shouldReRunOnThrowable(any(Throwable.class), anyInt(), anyInt());
    final JobManager jobManager = createJobManager();
    jobManager.addCallback(callback);
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            jobManager.addJob(job);
        }

        @Override
        public void assertJob(Job job) {
        }
    });
    verify(callback).onJobAdded(job);
    verify(callback, times(2)).onJobRun(job, JobManagerCallback.RESULT_FAIL_WILL_RETRY);
    verify(callback, times(1)).onJobRun(job, JobManagerCallback.RESULT_CANCEL_REACHED_RETRY_LIMIT);
    verify(callback).onJobCancelled(job, false, error);
}
Also used : Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) JobManagerCallback(com.birbit.android.jobqueue.callback.JobManagerCallback) Test(org.junit.Test)

Example 10 with JobManager

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

the class CancelBeforeRunningTest method testCancelBeforeRunning.

@Test
public void testCancelBeforeRunning() {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    DummyJob nonPersistentJob = new DummyJob(new Params(0).addTags("dummyTag"));
    DummyJob persistentJob = new DummyJob(new Params(0).addTags("dummyTag").persist());
    jobManager.addJob(nonPersistentJob);
    jobManager.addJob(persistentJob);
    CancelResult result = jobManager.cancelJobs(TagConstraint.ANY, "dummyTag");
    assertThat("both jobs should be cancelled", result.getCancelledJobs().size(), is(2));
    assertThat("both jobs should be cancelled", result.getFailedToCancel().size(), is(0));
    for (Job j : result.getCancelledJobs()) {
        DummyJob job = (DummyJob) j;
        if (!job.isPersistent()) {
            assertThat("job is still added", job.getOnAddedCnt(), is(1));
        }
        assertThat("job is cancelled", job.getOnCancelCnt(), is(1));
        assertThat("job is NOT run", job.getOnRunCnt(), is(0));
    }
}
Also used : CancelResult(com.birbit.android.jobqueue.CancelResult) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

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