use of com.birbit.android.jobqueue.scheduling.SchedulerConstraint in project android-priority-jobqueue by yigit.
the class BatchingSchedulerTest method testAddRemoveThenAddAgainOfTheSame.
@Test
public void testAddRemoveThenAddAgainOfTheSame() {
SchedulerConstraint constraint = createConstraint(NetworkUtil.METERED, 0);
bs.request(constraint);
verify(scheduler, times(1)).request(constraint);
MatcherAssert.assertThat(constraint.getDelayInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
bs.onFinished(constraint, false);
SchedulerConstraint constraint2 = createConstraint(NetworkUtil.METERED, 2);
bs.request(constraint2);
verify(scheduler, times(1)).request(constraint2);
MatcherAssert.assertThat(constraint2.getDelayInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
}
use of com.birbit.android.jobqueue.scheduling.SchedulerConstraint in project android-priority-jobqueue by yigit.
the class BatchingSchedulerTest method testFirstWithDeadline.
@Test
public void testFirstWithDeadline() {
SchedulerConstraint constraint = createConstraint(NetworkUtil.METERED, 0, 10L);
bs.request(constraint);
SchedulerConstraint constraint2 = createConstraint(NetworkUtil.METERED, 0);
bs.request(constraint2);
verify(scheduler, times(1)).request(constraint);
verify(scheduler, times(1)).request(constraint2);
MatcherAssert.assertThat(constraint.getDelayInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
MatcherAssert.assertThat(constraint.getOverrideDeadlineInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
MatcherAssert.assertThat(constraint2.getDelayInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
MatcherAssert.assertThat(constraint2.getOverrideDeadlineInMs(), CoreMatchers.nullValue());
}
use of com.birbit.android.jobqueue.scheduling.SchedulerConstraint 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));
}
}
Aggregations