use of com.birbit.android.jobqueue.Job in project android-priority-jobqueue by yigit.
the class PriorityTest method testPriority.
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void testPriority(JobManager jobManager, boolean persist) throws Exception {
priorityRunLatch = new CountDownLatch(2);
DummyJobWithRunOrderAssert.globalRunCount = new AtomicInteger(0);
Job job1 = new DummyJobWithRunOrderAssert(2, new Params(1).setPersistent(persist));
Job job2 = new DummyJobWithRunOrderAssert(1, new Params(2).setPersistent(persist));
jobManager.stop();
jobManager.addJob(job1);
jobManager.addJob(job2);
jobManager.start();
MatcherAssert.assertThat(priorityRunLatch.await(1, TimeUnit.MINUTES), is(true));
//ensure both jobs did run
MatcherAssert.assertThat((int) priorityRunLatch.getCount(), equalTo(0));
}
use of com.birbit.android.jobqueue.Job 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.Job 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.Job in project android-priority-jobqueue by yigit.
the class SqliteJobQueueTest method testCustomSerializer.
@Test
public void testCustomSerializer() throws Exception {
final CountDownLatch calledForSerialize = new CountDownLatch(1);
final CountDownLatch calledForDeserialize = new CountDownLatch(1);
SqliteJobQueue.JobSerializer jobSerializer = new SqliteJobQueue.JavaSerializer() {
@Override
public byte[] serialize(Object object) throws IOException {
calledForSerialize.countDown();
return super.serialize(object);
}
@Override
public <T extends Job> T deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
calledForDeserialize.countDown();
return super.deserialize(bytes);
}
};
SqliteJobQueue jobQueue = new SqliteJobQueue(new Configuration.Builder(RuntimeEnvironment.application).id("__" + mockTimer.nanoTime()).jobSerializer(jobSerializer).inTestMode().timer(mockTimer).build(), mockTimer.nanoTime(), jobSerializer);
jobQueue.insert(createNewJobHolder(new Params(0)));
calledForSerialize.await(1, TimeUnit.SECONDS);
MatcherAssert.assertThat("custom serializer should be called for serialize", (int) calledForSerialize.getCount(), CoreMatchers.equalTo(0));
MatcherAssert.assertThat("custom serializer should NOT be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(1));
jobQueue.nextJobAndIncRunCount(new TestConstraint(mockTimer));
MatcherAssert.assertThat("custom serializer should be called for deserialize", (int) calledForDeserialize.getCount(), CoreMatchers.equalTo(0));
}
use of com.birbit.android.jobqueue.Job in project android-priority-jobqueue by yigit.
the class SqliteJobQueue method createJobHolderFromCursor.
private JobHolder createJobHolderFromCursor(Cursor cursor) throws InvalidJobException {
String jobId = cursor.getString(DbOpenHelper.ID_COLUMN.columnIndex);
Job job;
try {
job = safeDeserialize(jobStorage.load(jobId));
} catch (IOException e) {
throw new InvalidJobException("cannot load job from disk", e);
}
if (job == null) {
throw new InvalidJobException("null job");
}
// load tags
Set<String> tags = loadTags(jobId);
//noinspection WrongConstant,UnnecessaryLocalVariable
JobHolder holder = new JobHolder.Builder().insertionOrder(cursor.getLong(DbOpenHelper.INSERTION_ORDER_COLUMN.columnIndex)).priority(cursor.getInt(DbOpenHelper.PRIORITY_COLUMN.columnIndex)).groupId(cursor.getString(DbOpenHelper.GROUP_ID_COLUMN.columnIndex)).runCount(cursor.getInt(DbOpenHelper.RUN_COUNT_COLUMN.columnIndex)).job(job).id(jobId).tags(tags).persistent(true).deadline(cursor.getLong(DbOpenHelper.DEADLINE_COLUMN.columnIndex), cursor.getInt(DbOpenHelper.CANCEL_ON_DEADLINE_COLUMN.columnIndex) == 1).createdNs(cursor.getLong(DbOpenHelper.CREATED_NS_COLUMN.columnIndex)).delayUntilNs(cursor.getLong(DbOpenHelper.DELAY_UNTIL_NS_COLUMN.columnIndex)).runningSessionId(cursor.getLong(DbOpenHelper.RUNNING_SESSION_ID_COLUMN.columnIndex)).requiredNetworkType(cursor.getInt(DbOpenHelper.REQUIRED_NETWORK_TYPE_COLUMN.columnIndex)).build();
return holder;
}
Aggregations