Search in sources :

Example 16 with SchedulerConstraint

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));
}
Also used : SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) Test(org.junit.Test)

Example 17 with SchedulerConstraint

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());
}
Also used : SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) Test(org.junit.Test)

Example 18 with SchedulerConstraint

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));
    }
}
Also used : Configuration(com.birbit.android.jobqueue.config.Configuration) Scheduler(com.birbit.android.jobqueue.scheduling.Scheduler) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Aggregations

SchedulerConstraint (com.birbit.android.jobqueue.scheduling.SchedulerConstraint)18 Test (org.junit.Test)14 JobManager (com.birbit.android.jobqueue.JobManager)1 Params (com.birbit.android.jobqueue.Params)1 Configuration (com.birbit.android.jobqueue.config.Configuration)1 Scheduler (com.birbit.android.jobqueue.scheduling.Scheduler)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1