use of com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter 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.callback.JobManagerCallbackAdapter 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));
}
Aggregations