use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class DelayedRunTest method testDelayWith0Consumers.
@Test
public void testDelayWith0Consumers() throws InterruptedException {
JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).minConsumerCount(0).maxConsumerCount(3).timer(mockTimer));
final CountDownLatch latch = new CountDownLatch(1);
final DummyJob dummyJob = new DummyJob(new Params(0).delayInMs(2000)) {
@Override
public void onRun() throws Throwable {
super.onRun();
latch.countDown();
}
};
jobManager.addJob(dummyJob);
mockTimer.incrementMs(1999);
assertThat("there should not be any ready jobs", jobManager.countReadyJobs(), is(0));
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
mockTimer.incrementMs(1002);
}
@Override
public void assertJob(Job job) {
assertThat("should be the dummy job", job, is((Job) dummyJob));
}
});
assertThat("job should run in 3 seconds", latch.await(3, TimeUnit.NANOSECONDS), is(true));
}
use of com.birbit.android.jobqueue.Params in project android-priority-jobqueue by yigit.
the class DelayedRunTest method delayedRunTest.
public void delayedRunTest(boolean persist, boolean tryToStop) throws Exception {
final JobManager jobManager = createJobManager();
jobManager.addCallback(new JobManagerCallbackAdapter() {
@Override
public void onJobRun(@NonNull Job job, int resultCode) {
super.onJobRun(job, resultCode);
System.out.println("CB job run " + job.getTags().toArray()[0] + ", " + mockTimer.nanoTime());
}
@Override
public void onDone(@NonNull Job job) {
System.out.println("CB job done " + job.getTags().toArray()[0] + ", " + mockTimer.nanoTime());
}
@Override
public void onAfterJobRun(@NonNull Job job, int resultCode) {
System.out.println("CB job after run " + job.getTags().toArray()[0] + ", " + mockTimer.nanoTime());
}
});
final DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(2000).setPersistent(persist).addTags("delayed"));
final DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist).addTags("notDelayed"));
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
jobManager.addJob(delayedJob);
jobManager.addJob(nonDelayedJob);
}
@Override
public void assertJob(Job job) {
assertThat("correct job should run first", (String) job.getTags().toArray()[0], is("notDelayed"));
}
});
MatcherAssert.assertThat("there should be 1 delayed job waiting to be run", jobManager.count(), equalTo(1));
if (tryToStop) {
//see issue #11
jobManager.stopAndWaitUntilConsumersAreFinished();
mockTimer.incrementMs(3000);
MatcherAssert.assertThat("there should still be 1 delayed job waiting to be run when job manager is stopped", jobManager.count(), equalTo(1));
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
jobManager.start();
}
@Override
public void assertJob(Job job) {
assertThat("correct job should run first", (String) job.getTags().toArray()[0], is("delayed"));
}
});
} else {
waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {
@Override
public void run() {
mockTimer.incrementMs(3000);
}
@Override
public void assertJob(Job job) {
assertThat("correct job should run first", (String) job.getTags().toArray()[0], is("delayed"));
}
});
}
MatcherAssert.assertThat("all jobs should be completed", jobManager.count(), equalTo(0));
}
use of com.birbit.android.jobqueue.Params 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.Params 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.Params 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));
}
Aggregations