use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class CancelFailingJobsTest method testCancelWithoutNetworkPersistent.
public void testCancelWithoutNetworkPersistent(boolean async, TagConstraint constraint) throws InterruptedException {
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil));
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
jobManager.addJob(new DummyJob(new Params(1).persist().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(2).persist().groupBy("group").addTags("tag")));
jobManager.addJob(new DummyJob(new Params(3).persist().groupBy("group").addTags("tag")));
final CancelResult[] result = new CancelResult[1];
if (async) {
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
result[0] = cancelResult;
cancelLatch.countDown();
}
}, constraint, "tag");
cancelLatch.await(2, TimeUnit.SECONDS);
} else {
result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
}
assertThat("all jobs should be canceled", result[0].getCancelledJobs().size(), is(3));
assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
final CountDownLatch runLatch = persistentLatches[latchCounter++];
jobManager.addJob(new PersistentDummyJob(new Params(3).persist().groupBy("group").addTags("tag"), latchCounter - 1));
networkUtil.setNetworkStatus(NetworkUtil.METERED, true);
assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class CancelFailingJobsTest method testCancelWithoutNetwork.
public void testCancelWithoutNetwork(boolean async, TagConstraint constraint) throws InterruptedException {
final int jobCount = 30;
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(5).networkUtil(networkUtil).timer(mockTimer));
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
for (int i = 0; i < jobCount; i++) {
jobManager.addJob(new FailingJob(new Params(i).groupBy("group").addTags("tag")));
}
final CancelResult[] result = new CancelResult[1];
if (async) {
final CountDownLatch cancelLatch = new CountDownLatch(1);
jobManager.cancelJobsInBackground(new CancelResult.AsyncCancelCallback() {
@Override
public void onCancelled(CancelResult cancelResult) {
result[0] = cancelResult;
cancelLatch.countDown();
}
}, constraint, "tag");
assertThat(cancelLatch.await(jobCount, TimeUnit.SECONDS), is(true));
} else {
result[0] = jobManager.cancelJobs(TagConstraint.ANY, "tag");
}
assertThat("all jobs should be cancelled", result[0].getCancelledJobs().size(), is(jobCount));
assertThat("no jobs should fail to cancel", result[0].getFailedToCancel().size(), is(0));
final CountDownLatch runLatch = new CountDownLatch(1);
jobManager.addJob(new DummyJob(new Params(1).groupBy("group").addTags("tag")) {
@Override
public void onRun() throws Throwable {
super.onRun();
runLatch.countDown();
}
});
networkUtil.setNetworkStatus(NetworkUtil.UNMETERED, true);
assertThat("new job should run w/o any issues", runLatch.await(2, TimeUnit.SECONDS), is(true));
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob 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));
}
}
use of com.birbit.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by yigit.
the class JobQueueTestBase method createNewJobHolder.
public static JobHolder createNewJobHolder(Params params, Timer timer) {
long delay = getDelayMsField(params).get();
long deadline = getDeadlineMsField(params).get();
boolean cancelOnDeadline = Boolean.TRUE.equals(getCancelOnDeadlineDeadlineField(params).get());
DummyJob job = new DummyJob(params);
//noinspection WrongConstant
return new JobHolder.Builder().priority(params.getPriority()).groupId(params.getGroupId()).job(job).id(job.getId()).persistent(params.isPersistent()).tags(job.getTags()).createdNs(timer.nanoTime()).deadline(deadline > 0 ? timer.nanoTime() + deadline * JobManager.NS_PER_MS : Params.FOREVER, cancelOnDeadline).delayUntilNs(delay > 0 ? timer.nanoTime() + delay * JobManager.NS_PER_MS : JobManager.NOT_DELAYED_JOB_DELAY).requiredNetworkType(getNetworkTypeField(params).get()).runningSessionId(JobManager.NOT_RUNNING_SESSION_ID).build();
}
Aggregations