use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class AddInBackgroundTest method addInBackground.
public void addInBackground(boolean delayed, boolean useCallback) throws InterruptedException {
long currentThreadId = Thread.currentThread().getId();
final AtomicLong onAddedThreadId = new AtomicLong();
final CountDownLatch addedLatch = new CountDownLatch(2);
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();
final AtomicLong jobId = new AtomicLong(0);
if (useCallback) {
jobManager.addJobInBackground(dummyJob, new AsyncAddCallback() {
@Override
public void onAdded(long id) {
jobId.set(id);
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) {
JobQueue queue = getNonPersistentQueue(jobManager);
JobHolder holder = queue.findJobById(jobId.longValue());
MatcherAssert.assertThat("there should be a job in the holder. id:" + jobId.longValue() + ", delayed:" + delayed + ", use cb:" + useCallback, holder, CoreMatchers.notNullValue());
MatcherAssert.assertThat("id callback should have the proper id:", holder.getBaseJob(), CoreMatchers.is((BaseJob) dummyJob));
}
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class CountTest method testCount.
@Test
public void testCount() throws Exception {
JobManager jobManager = createJobManager();
jobManager.stop();
for (int i = 0; i < 10; i++) {
jobManager.addJob(new DummyJob(new Params(0).persist()));
MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 1));
jobManager.addJob(new DummyJob(new Params(0).persist()));
MatcherAssert.assertThat((int) jobManager.count(), equalTo(i * 2 + 2));
}
jobManager.start();
Thread.sleep(2000);
MatcherAssert.assertThat((int) jobManager.count(), equalTo(0));
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class DelayTest method testDelay.
public void testDelay(boolean persist) throws Exception {
JobManager jobManager = createJobManager();
jobManager.stop();
DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(1000).setPersistent(persist));
DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
long jobId = jobManager.addJob(delayedJob);
long nonDelayedJobId = jobManager.addJob(nonDelayedJob);
Invoker<JobHolder> nextJobMethod = getNextJobMethod(jobManager);
Invoker<Void> removeJobMethod = getRemoveJobMethod(jobManager);
JobHolder receivedJob = nextJobMethod.invoke();
MatcherAssert.assertThat("non-delayed job should be served", receivedJob, notNullValue());
MatcherAssert.assertThat("non-delayed job should id should match", receivedJob.getId(), equalTo(nonDelayedJobId));
removeJobMethod.invoke(receivedJob);
MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
Thread.sleep(500);
MatcherAssert.assertThat("delayed job should not be served", nextJobMethod.invoke(), nullValue());
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
Thread.sleep(2000);
MatcherAssert.assertThat("job count should still be 1", jobManager.count(), equalTo(1));
receivedJob = nextJobMethod.invoke();
MatcherAssert.assertThat("now should be able to receive the delayed job.", receivedJob, notNullValue());
if (receivedJob != null) {
MatcherAssert.assertThat("received job should be the delayed job", receivedJob.getId(), equalTo(jobId));
}
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class DelayedRunTest method testDelayedRun.
public void testDelayedRun(boolean persist, boolean tryToStop) throws Exception {
JobManager jobManager = createJobManager();
DummyJob delayedJob = new DummyJob(new Params(10).delayInMs(2000).setPersistent(persist));
DummyJob nonDelayedJob = new DummyJob(new Params(0).setPersistent(persist));
jobManager.addJob(delayedJob);
jobManager.addJob(nonDelayedJob);
Thread.sleep(500);
MatcherAssert.assertThat("there should be 1 delayed job waiting to be run", jobManager.count(), equalTo(1));
if (tryToStop) {
// see issue #11
jobManager.stop();
Thread.sleep(3000);
MatcherAssert.assertThat("there should still be 1 delayed job waiting to be run when job manager is stopped", jobManager.count(), equalTo(1));
jobManager.start();
}
Thread.sleep(3000);
MatcherAssert.assertThat("all jobs should be completed", jobManager.count(), equalTo(0));
}
use of com.path.android.jobqueue.test.jobs.DummyJob in project android-priority-jobqueue by path.
the class GroupingTest method testGroupingRaceCondition.
@Test
public void testGroupingRaceCondition() throws Exception {
DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
JobManager jobManager = createJobManager(new Configuration.Builder(Robolectric.application).minConsumerCount(5).maxConsumerCount(10).networkUtil(dummyNetworkUtil));
dummyNetworkUtil.setHasNetwork(false, 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);
jobManager.addJobInBackground(new DummyJob(new Params(10).requireNetwork().groupBy(GROUP_ID)) {
@Override
public void onAdded() {
super.onAdded();
onAddedCalled.countDown();
try {
// wait until all other jobs are added
remainingJobsOnAddedCalled.await();
// wait a bit after all are added,
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
@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.setHasNetwork(true, true);
// wait until all jobs are completed
while (firstRunJob.get() == NOT_SET_JOB_ID) {
Thread.sleep(100);
}
MatcherAssert.assertThat("highest priority job should run if it is added before others", firstRunJob.get(), is(FIRST_JOB_ID));
}
Aggregations