Search in sources :

Example 1 with RetryConstraint

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

the class RetryLogicTest method testCancel.

public void testCancel(boolean persistent) throws InterruptedException {
    canRun = true;
    final Throwable[] retryThrowable = new Throwable[1];
    final Throwable[] cancelThrowable = new Throwable[1];
    retryProvider = new RetryProvider() {

        @Override
        public RetryConstraint build(Job job, Throwable throwable, int runCount, int maxRunCount) {
            retryThrowable[0] = throwable;
            return RetryConstraint.CANCEL;
        }
    };
    onCancelCallback = new CancelCallback() {

        @Override
        public void on(Job job, @CancelReason int cancelReason, @Nullable Throwable throwable) {
            assertThat("should call cancel only once", cancelThrowable[0], is(nullValue()));
            cancelThrowable[0] = throwable;
        }
    };
    RetryJob job = new RetryJob(new Params(1).setPersistent(persistent));
    job.retryLimit = 3;
    onRunLatch = new CountDownLatch(3);
    createJobManager().addJob(job);
    assertThat(onRunLatch.await(2, TimeUnit.SECONDS), is(false));
    assertThat("it should run 1 time", runCount, is(1));
    assertThat(cancelLatch.await(2, TimeUnit.SECONDS), is(true));
    assertThat(retryThrowable[0], instanceOf(RuntimeException.class));
    assertThat(cancelThrowable[0], instanceOf(RuntimeException.class));
    assertThat(retryThrowable[0], is(cancelThrowable[0]));
}
Also used : RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Params(com.birbit.android.jobqueue.Params) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) CountDownLatch(java.util.concurrent.CountDownLatch) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint)

Example 2 with RetryConstraint

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

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

the class FetchTweetsJob method shouldReRunOnThrowable.

@Override
protected RetryConstraint shouldReRunOnThrowable(Throwable throwable, int runCount, int maxRunCount) {
    if (throwable instanceof TwitterException) {
        //if it is a 4xx error, stop
        TwitterException twitterException = (TwitterException) throwable;
        int errorCode = twitterException.getErrorCode();
        return errorCode < 400 || errorCode > 499 ? RetryConstraint.RETRY : RetryConstraint.CANCEL;
    }
    return RetryConstraint.RETRY;
}
Also used : TwitterException(twitter4j.TwitterException) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint)

Example 4 with RetryConstraint

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

the class PostTweetJob method shouldReRunOnThrowable.

@Override
protected RetryConstraint shouldReRunOnThrowable(Throwable throwable, int runCount, int maxRunCount) {
    if (throwable instanceof TwitterException) {
        //if it is a 4xx error, stop
        TwitterException twitterException = (TwitterException) throwable;
        int errorCode = twitterException.getErrorCode();
        return errorCode < 400 || errorCode > 499 ? RetryConstraint.RETRY : RetryConstraint.CANCEL;
    }
    return RetryConstraint.RETRY;
}
Also used : TwitterException(twitter4j.TwitterException) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint)

Example 5 with RetryConstraint

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

the class RunFailingJobTest method runFailingJob.

@Test
public void runFailingJob() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    JobManager jobManager = createJobManager();
    jobManager.addJob(new Job(new Params(0).requireNetwork()) {

        @Override
        public void onAdded() {
        }

        @Override
        public void onRun() throws Throwable {
            throw new RuntimeException();
        }

        @Override
        protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
            latch.countDown();
        }

        @Override
        protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
            return RetryConstraint.CANCEL;
        }
    });
    latch.await(10, TimeUnit.SECONDS);
    MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
Also used : RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Job(com.birbit.android.jobqueue.Job) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Test(org.junit.Test)

Aggregations

RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)10 Job (com.birbit.android.jobqueue.Job)8 Params (com.birbit.android.jobqueue.Params)8 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 JobManager (com.birbit.android.jobqueue.JobManager)4 TargetApi (android.annotation.TargetApi)3 ArrayList (java.util.ArrayList)3 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 TwitterException (twitter4j.TwitterException)2 Pair (android.util.Pair)1 CallbackManager (com.birbit.android.jobqueue.CallbackManager)1 HashMap (java.util.HashMap)1 Semaphore (java.util.concurrent.Semaphore)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1