use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class RunManyNonPersistentTest method runManyNonPersistentJobs.
@Test
public void runManyNonPersistentJobs() throws Exception {
JobManager jobManager = createJobManager();
jobManager.stop();
int limit = 2;
final CountDownLatch latch = new CountDownLatch(limit);
for (int i = 0; i < limit; i++) {
jobManager.addJob(new DummyLatchJob(new Params(i), latch));
}
jobManager.start();
latch.await(10, TimeUnit.SECONDS);
MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class SchedulerSimpleTestCase method testScheduleWhenJobAdded.
@Test
public void testScheduleWhenJobAdded() throws InterruptedException {
Scheduler scheduler = Mockito.mock(Scheduler.class);
ArgumentCaptor<SchedulerConstraint> captor = ArgumentCaptor.forClass(SchedulerConstraint.class);
DummyNetworkUtilWithConnectivityEventSupport networkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
Configuration.Builder builder = new Configuration.Builder(RuntimeEnvironment.application).timer(mockTimer).networkUtil(networkUtil).inTestMode().scheduler(scheduler, false);
if (requireUnmeteredNetwork) {
networkUtil.setNetworkStatus(NetworkUtil.UNMETERED);
} else if (requireNetwork) {
networkUtil.setNetworkStatus(NetworkUtil.METERED);
} else {
networkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED);
}
final JobManager jobManager = createJobManager(builder);
Params params = new Params(1);
params.setPersistent(persistent);
params.setRequiresNetwork(requireNetwork);
params.setRequiresUnmeteredNetwork(requireUnmeteredNetwork);
params.setDelayMs(delayInMs);
if (deadline != null) {
params.overrideDeadlineToRunInMs(deadline);
}
final SchedulerJob job = new SchedulerJob(params);
final CountDownLatch cancelLatch = new CountDownLatch(1);
Mockito.doAnswer(new Answer() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
cancelLatch.countDown();
return null;
}
}).when(scheduler).cancelAll();
waitUntilJobsAreDone(jobManager, Collections.singletonList(job), new Runnable() {
@Override
public void run() {
jobManager.addJob(job);
mockTimer.incrementMs(delayInMs);
}
});
if (persistent && (requireNetwork || requireUnmeteredNetwork || delayInMs >= JobManager.MIN_DELAY_TO_USE_SCHEDULER_IN_MS || (deadline != null && deadline >= JobManager.MIN_DELAY_TO_USE_SCHEDULER_IN_MS))) {
Mockito.verify(scheduler).request(captor.capture());
SchedulerConstraint constraint = captor.getValue();
MatcherAssert.assertThat(constraint.getNetworkStatus(), CoreMatchers.is(requireUnmeteredNetwork ? NetworkUtil.UNMETERED : requireNetwork ? NetworkUtil.METERED : NetworkUtil.DISCONNECTED));
MatcherAssert.assertThat(constraint.getDelayInMs(), CoreMatchers.is(delayInMs));
// wait until cancel is called because it is called when JQ is idle.
// for more clear reporting, let mockito handle the check
MatcherAssert.assertThat(constraint.getOverrideDeadlineInMs(), CoreMatchers.is(deadline));
cancelLatch.await(30, TimeUnit.SECONDS);
Mockito.verify(scheduler).cancelAll();
} else {
Mockito.verify(scheduler, Mockito.never()).request(Mockito.any(SchedulerConstraint.class));
}
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class SessionIdTest method testSessionId.
@Test
public void testSessionId() throws Throwable {
JobManager jobManager = createJobManager();
//we know job manager uses this value :/
Long sessionId = mockTimer.nanoTime();
jobManager.stop();
Job[] jobs = new Job[] { new DummyJob(new Params(0)), new DummyJob(new Params(0).persist()) };
for (Job job : jobs) {
jobManager.addJob(job);
}
for (int i = 0; i < jobs.length; i++) {
JobHolder jobHolder = nextJob(jobManager);
MatcherAssert.assertThat("session id should be correct for job " + i, jobHolder.getRunningSessionId(), equalTo(sessionId));
}
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class SingleIdTest method testSingleId.
private void testSingleId(boolean persistent) throws Throwable {
JobManager jobManager = createJobManager();
jobManager.stop();
String singleId = "forks";
DummyJob dummyJob1 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId));
String jobId1 = addJob(jobManager, dummyJob1);
DummyJob dummyJob2 = new DummyJob(new Params(0).setPersistent(persistent));
String jobId2 = addJob(jobManager, dummyJob2);
assertThat("should add job if doesn't have singleId", jobManager.countReadyJobs(), is(2));
DummyJob dummyJob3 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId("otherId"));
String jobId3 = addJob(jobManager, dummyJob3);
assertThat("should add job if different singleId", jobManager.countReadyJobs(), is(3));
DummyJob dummyJob4 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId));
addJob(jobManager, dummyJob4);
assertThat("should not add job with same singleId", jobManager.countReadyJobs(), is(3));
assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId1));
assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId2));
assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId3));
assertThat("should return the same id", nextJob(jobManager), is(nullValue()));
}
use of com.birbit.android.jobqueue.JobManager in project android-priority-jobqueue by yigit.
the class SlowOnAddedTest method testNonPersistent.
@Test
public void testNonPersistent() throws InterruptedException {
JobManager jobManager = createJobManager();
CountDownLatch runLatch = new CountDownLatch(1);
MyDummyJob job = new MyDummyJob(new Params(2), runLatch);
for (int i = 0; i < 50; i++) {
jobManager.addJob(new DummyJob(new Params(1)));
}
jobManager.addJob(job);
runLatch.await();
assertThat("on added should be called before on run", job.onAddedCntWhenRun, equalTo(1));
}
Aggregations