Search in sources :

Example 1 with JobManagerCallback

use of com.birbit.android.jobqueue.callback.JobManagerCallback 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 2 with JobManagerCallback

use of com.birbit.android.jobqueue.callback.JobManagerCallback 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 3 with JobManagerCallback

use of com.birbit.android.jobqueue.callback.JobManagerCallback 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 4 with JobManagerCallback

use of com.birbit.android.jobqueue.callback.JobManagerCallback 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 5 with JobManagerCallback

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

the class CallbackTest method successPersistent.

@Test
public void successPersistent() throws Throwable {
    JobManagerCallback callback = mock(JobManagerCallback.class);
    final Job job = new PersistentDummyJob();
    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(any(PersistentDummyJob.class));
    verify(callback).onJobRun(any(PersistentDummyJob.class), eq(JobManagerCallback.RESULT_SUCCEED));
}
Also used : JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) JobManagerCallback(com.birbit.android.jobqueue.callback.JobManagerCallback) Test(org.junit.Test)

Aggregations

JobManager (com.birbit.android.jobqueue.JobManager)5 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)5 Test (org.junit.Test)5 Job (com.birbit.android.jobqueue.Job)4 Params (com.birbit.android.jobqueue.Params)4 CountDownLatch (java.util.concurrent.CountDownLatch)1