Search in sources :

Example 1 with Scheduler

use of com.birbit.android.jobqueue.scheduling.Scheduler in project android-priority-jobqueue by yigit.

the class BatchingSchedulerTest method testCustomDuration.

@Test
public void testCustomDuration() {
    scheduler = mock(Scheduler.class);
    timer = new MockTimer();
    bs = new BatchingScheduler(scheduler, timer, 123);
    MatcherAssert.assertThat(bs.batchingDurationInMs, CoreMatchers.is(123L));
    MatcherAssert.assertThat(bs.batchingDurationInNs, CoreMatchers.is(123000000L));
}
Also used : MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) BatchingScheduler(com.birbit.android.jobqueue.BatchingScheduler) Scheduler(com.birbit.android.jobqueue.scheduling.Scheduler) BatchingScheduler(com.birbit.android.jobqueue.BatchingScheduler) Test(org.junit.Test)

Example 2 with Scheduler

use of com.birbit.android.jobqueue.scheduling.Scheduler in project android-priority-jobqueue by yigit.

the class BatchingSchedulerTest method init.

@Before
public void init() {
    scheduler = mock(Scheduler.class);
    timer = new MockTimer();
    bs = new BatchingScheduler(scheduler, timer);
    Context context = mock(Context.class);
    when(context.getApplicationContext()).thenReturn(mock(Context.class));
    bs.init(context, mock(Scheduler.Callback.class));
}
Also used : Context(android.content.Context) MockTimer(com.birbit.android.jobqueue.test.timer.MockTimer) BatchingScheduler(com.birbit.android.jobqueue.BatchingScheduler) Scheduler(com.birbit.android.jobqueue.scheduling.Scheduler) BatchingScheduler(com.birbit.android.jobqueue.BatchingScheduler) Before(org.junit.Before)

Example 3 with Scheduler

use of com.birbit.android.jobqueue.scheduling.Scheduler 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

Scheduler (com.birbit.android.jobqueue.scheduling.Scheduler)3 BatchingScheduler (com.birbit.android.jobqueue.BatchingScheduler)2 MockTimer (com.birbit.android.jobqueue.test.timer.MockTimer)2 Test (org.junit.Test)2 Context (android.content.Context)1 JobManager (com.birbit.android.jobqueue.JobManager)1 Params (com.birbit.android.jobqueue.Params)1 Configuration (com.birbit.android.jobqueue.config.Configuration)1 SchedulerConstraint (com.birbit.android.jobqueue.scheduling.SchedulerConstraint)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Answer (org.mockito.stubbing.Answer)1