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]));
}
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)));
}
}
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;
}
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;
}
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));
}
Aggregations