Search in sources :

Example 1 with SchedulerConstraint

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

the class JobManagerThread method scheduleWakeUpFor.

private void scheduleWakeUpFor(JobHolder holder, long now) {
    if (scheduler == null) {
        return;
    }
    int requiredNetwork = holder.requiredNetworkType;
    long delayUntilNs = holder.getDelayUntilNs();
    long deadlineNs = holder.getDeadlineNs();
    long delay = delayUntilNs > now ? TimeUnit.NANOSECONDS.toMillis(delayUntilNs - now) : 0;
    Long deadline = deadlineNs != Params.FOREVER ? TimeUnit.NANOSECONDS.toMillis(deadlineNs - now) : null;
    boolean hasLargeDelay = delayUntilNs > now && delay >= JobManager.MIN_DELAY_TO_USE_SCHEDULER_IN_MS;
    boolean hasLargeDeadline = deadline != null && deadline >= JobManager.MIN_DELAY_TO_USE_SCHEDULER_IN_MS;
    if (requiredNetwork == NetworkUtil.DISCONNECTED && !hasLargeDelay && !hasLargeDeadline) {
        return;
    }
    SchedulerConstraint constraint = new SchedulerConstraint(UUID.randomUUID().toString());
    constraint.setNetworkStatus(requiredNetwork);
    constraint.setDelayInMs(delay);
    constraint.setOverrideDeadlineInMs(deadline);
    scheduler.request(constraint);
    shouldCancelAllScheduledWhenEmpty = true;
}
Also used : SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint)

Example 2 with SchedulerConstraint

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

the class JobManagerThread method handleSchedulerStop.

private void handleSchedulerStop(SchedulerConstraint constraint) {
    final List<SchedulerConstraint> pendingCallbacks = this.pendingSchedulerCallbacks;
    if (pendingCallbacks != null) {
        for (int i = pendingCallbacks.size() - 1; i >= 0; i--) {
            SchedulerConstraint pendingConstraint = pendingCallbacks.get(i);
            if (pendingConstraint.getUuid().equals(constraint.getUuid())) {
                pendingCallbacks.remove(i);
            }
        }
    }
    if (scheduler == null) {
        //nothing to do
        return;
    }
    final boolean hasMatchingJobs = hasJobsWithSchedulerConstraint(constraint);
    if (hasMatchingJobs) {
        // reschedule
        scheduler.request(constraint);
    }
}
Also used : SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint)

Example 3 with SchedulerConstraint

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

the class BatchingSchedulerTest method testAddOne.

@Test
public void testAddOne() {
    SchedulerConstraint constraint = new SchedulerConstraint("abc");
    constraint.setDelayInMs(0);
    constraint.setNetworkStatus(NetworkUtil.DISCONNECTED);
    bs.request(constraint);
    verify(scheduler, times(1)).request(constraint);
    MatcherAssert.assertThat(constraint.getDelayInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
}
Also used : SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) Test(org.junit.Test)

Example 4 with SchedulerConstraint

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

the class BatchingSchedulerTest method testAddTwoWithDifferentNetwork.

@Test
public void testAddTwoWithDifferentNetwork() {
    SchedulerConstraint constraint = createConstraint(NetworkUtil.METERED, 0);
    bs.request(constraint);
    SchedulerConstraint constraint2 = createConstraint(NetworkUtil.UNMETERED, 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(constraint2.getDelayInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
}
Also used : SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) Test(org.junit.Test)

Example 5 with SchedulerConstraint

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

the class BatchingSchedulerTest method testAddTwoOfTheSameWithEnoughTimeDifference.

@Test
public void testAddTwoOfTheSameWithEnoughTimeDifference() {
    SchedulerConstraint constraint = createConstraint(NetworkUtil.METERED, 0);
    bs.request(constraint);
    timer.incrementMs(DEFAULT_BATCHING_PERIOD_IN_MS);
    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(constraint2.getDelayInMs(), CoreMatchers.is(DEFAULT_BATCHING_PERIOD_IN_MS));
}
Also used : SchedulerConstraint(com.birbit.android.jobqueue.scheduling.SchedulerConstraint) 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