use of com.birbit.android.jobqueue.Job in project android-priority-jobqueue by yigit.
the class GroupingTest method testGroupingRaceCondition.
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Test
public void testGroupingRaceCondition() throws Exception {
DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).maxConsumerCount(10).networkUtil(dummyNetworkUtil).timer(mockTimer));
dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
//add a bunch of network requring jobs
final String GROUP_ID = "shared_group_id";
final int AFTER_ADDED_JOBS_COUNT = 5;
final int NOT_SET_JOB_ID = -1;
final AtomicInteger firstRunJob = new AtomicInteger(NOT_SET_JOB_ID);
final int FIRST_JOB_ID = -10;
final CountDownLatch onAddedCalled = new CountDownLatch(1);
final CountDownLatch remainingJobsOnAddedCalled = new CountDownLatch(AFTER_ADDED_JOBS_COUNT);
final CountDownLatch aJobRun = new CountDownLatch(1);
jobManager.addCallback(new JobManagerCallbackAdapter() {
@Override
public void onJobRun(@NonNull Job job, int resultCode) {
aJobRun.countDown();
}
});
jobManager.addJobInBackground(new DummyJob(new Params(10).requireNetwork().groupBy(GROUP_ID)) {
@Override
public void onAdded() {
super.onAdded();
onAddedCalled.countDown();
}
@Override
public void onRun() throws Throwable {
super.onRun();
firstRunJob.compareAndSet(NOT_SET_JOB_ID, FIRST_JOB_ID);
}
});
//ensure first jobs on added is called
onAddedCalled.await();
for (int i = 0; i < AFTER_ADDED_JOBS_COUNT; i++) {
final int finalI = i;
jobManager.addJob(new DummyJob(new Params(5).groupBy(GROUP_ID).requireNetwork()) {
final int id = finalI + 1;
@Override
public void onAdded() {
super.onAdded();
remainingJobsOnAddedCalled.countDown();
}
@Override
public void onRun() throws Throwable {
super.onRun();
firstRunJob.compareAndSet(NOT_SET_JOB_ID, id);
}
});
}
dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, true);
//wait until all jobs are completed
aJobRun.await(1, TimeUnit.MINUTES);
MatcherAssert.assertThat("highest priority job should run if it is added before others", firstRunJob.get(), is(FIRST_JOB_ID));
}
use of com.birbit.android.jobqueue.Job in project android-priority-jobqueue by yigit.
the class HitDeadlineAfterException method failAndHitDeadline.
@Test
public void failAndHitDeadline() throws InterruptedException {
final JobManager jobManager = createJobManager();
final AtomicBoolean calledShouldReRun = new AtomicBoolean(false);
final AtomicInteger reason = new AtomicInteger();
final DummyJob job = new DummyJob(new Params(0).overrideDeadlineToCancelInMs(100)) {
@Override
public void onRun() throws Throwable {
super.onRun();
mockTimer.incrementMs(150);
throw new RuntimeException("why not fail");
}
@Override
protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
reason.set(cancelReason);
super.onCancel(cancelReason, throwable);
}
@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
calledShouldReRun.set(true);
return RetryConstraint.RETRY;
}
};
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
jobManager.addJob(job);
}
@Override
public void assertJob(Job job) {
}
});
MatcherAssert.assertThat(reason.get(), CoreMatchers.is(CancelReason.REACHED_DEADLINE));
MatcherAssert.assertThat(calledShouldReRun.get(), CoreMatchers.is(false));
}
use of com.birbit.android.jobqueue.Job 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));
}
use of com.birbit.android.jobqueue.Job in project android-priority-jobqueue by yigit.
the class CancelBeforeRunningTest method testCancelBeforeRunningWithGroups.
@Test
public void testCancelBeforeRunningWithGroups() throws InterruptedException {
JobManager jobManager = createJobManager();
jobManager.stop();
DummyJob nonPersistentJob = new DummyJob(new Params(0).addTags("dummyTag").groupBy("group1"));
DummyJob persistentJob = new DummyJob(new Params(0).addTags("dummyTag").persist().groupBy("group2"));
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));
}
assertThat("there should not be any jobs in the queue", jobManager.count(), is(0));
jobManager.start();
DummyJob nonPersistentJob2 = new DummyJob(new Params(0).addTags("dummyTag").groupBy("group1")) {
@Override
public void onRun() throws Throwable {
super.onRun();
nonPersistentJobLatch.countDown();
}
};
DummyJob persistentJob2 = new PersistentDummyJob(new Params(0).addTags("dummyTag").groupBy("group2"));
jobManager.addJob(persistentJob2);
jobManager.addJob(nonPersistentJob2);
assertThat("a new job in the same group with canceled job should run", nonPersistentJobLatch.await(1, TimeUnit.SECONDS), is(true));
assertThat("a new persistent job in the same group with canceled job should run", persistentJobLatch.await(2, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.Job in project android-priority-jobqueue by yigit.
the class AddInBackgroundTest method addInBackground.
@Test
public void addInBackground() throws Throwable {
long currentThreadId = Thread.currentThread().getId();
final AtomicLong onAddedThreadId = new AtomicLong();
final CountDownLatch addedLatch = new CountDownLatch(2);
final Job dummyJob = new DummyJob(new Params(1).setDelayMs(delayed ? 1000 : 0)) {
@Override
public void onAdded() {
super.onAdded();
onAddedThreadId.set(Thread.currentThread().getId());
addedLatch.countDown();
}
};
JobManager jobManager = createJobManager();
jobManager.stop();
if (useCallback) {
jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {
@Override
public void onAdded() {
addedLatch.countDown();
}
});
} else {
addedLatch.countDown();
jobManager.addJobInBackground(dummyJob);
}
addedLatch.await();
MatcherAssert.assertThat("thread ids should be different. delayed:" + delayed, currentThreadId, CoreMatchers.not(onAddedThreadId.get()));
if (useCallback) {
JobHolder holder = new JobManagerThreadRunnable<JobHolder>(jobManager) {
@Override
public JobHolder onRun() {
return findJobFromQueues(dummyJob.getId());
}
}.run();
MatcherAssert.assertThat("there should be a job in the holder. id:" + dummyJob.getId() + ", delayed:" + delayed, holder, CoreMatchers.notNullValue());
MatcherAssert.assertThat("id callback should have the proper id:", holder.getJob(), CoreMatchers.is(dummyJob));
}
}
Aggregations