Search in sources :

Example 71 with Params

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

the class NetworkJobWithConnectivityListenerTest method testNetworkJobWithConnectivityListener.

@Test
public void testNetworkJobWithConnectivityListener() throws Exception {
    final DummyNetworkUtilWithConnectivityEventSupport dummyNetworkUtil = new DummyNetworkUtilWithConnectivityEventSupport();
    final JobManager jobManager = createJobManager(new Configuration.Builder(RuntimeEnvironment.application).networkUtil(dummyNetworkUtil).timer(mockTimer));
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.DISCONNECTED, true);
    final DummyJob dummyJob = new DummyJob(new Params(0).requireNetwork());
    jobManager.addJob(dummyJob);
    // no job to run so consumers should finish
    jobManager.waitUntilConsumersAreFinished();
    MatcherAssert.assertThat("count should be 1 as no jobs should be consumed w/o network", jobManager.count(), equalTo(1));
    // JobManager may wake up as idle right here and see the new network value. sleep to avoid it
    // count will trigger the queue and will result in another IDLE call. We need to wait until
    // it is handled.
    //noinspection SLEEP_IN_CODE
    Thread.sleep(2000);
    dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, false);
    //noinspection SLEEP_IN_CODE
    //wait a little bit more to let consumer run
    Thread.sleep(5000);
    MatcherAssert.assertThat("even though network is recovered, job manager should not consume any job because it " + "does not know (we did not inform)", jobManager.count(), equalTo(1));
    waitUntilAJobIsDone(jobManager, new WaitUntilCallback() {

        @Override
        public void run() {
            dummyNetworkUtil.setNetworkStatus(NetworkUtil.METERED, true);
        }

        @Override
        public void assertJob(Job job) {
            MatcherAssert.assertThat("should be the added job", job, CoreMatchers.is((Job) dummyJob));
        }
    });
    MatcherAssert.assertThat("job manager should consume network job after it is informed that network is recovered", jobManager.count(), equalTo(0));
}
Also used : Configuration(com.birbit.android.jobqueue.config.Configuration) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Job(com.birbit.android.jobqueue.Job) Test(org.junit.Test)

Example 72 with Params

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

the class RunFailingJobTest method runFailingJob.

@Test
public void runFailingJob() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    JobManager jobManager = createJobManager();
    jobManager.addJob(new Job(new Params(0).requireNetwork()) {

        @Override
        public void onAdded() {
        }

        @Override
        public void onRun() throws Throwable {
            throw new RuntimeException();
        }

        @Override
        protected void onCancel(@CancelReason int cancelReason, @Nullable Throwable throwable) {
            latch.countDown();
        }

        @Override
        protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
            return RetryConstraint.CANCEL;
        }
    });
    latch.await(10, TimeUnit.SECONDS);
    MatcherAssert.assertThat((int) latch.getCount(), equalTo(0));
}
Also used : RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Job(com.birbit.android.jobqueue.Job) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Test(org.junit.Test)

Example 73 with Params

use of com.birbit.android.jobqueue.Params 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));
}
Also used : Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 74 with Params

use of com.birbit.android.jobqueue.Params 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)

Example 75 with Params

use of com.birbit.android.jobqueue.Params 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));
    }
}
Also used : JobHolder(com.birbit.android.jobqueue.JobHolder) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) Job(com.birbit.android.jobqueue.Job) DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Test(org.junit.Test)

Aggregations

Params (com.birbit.android.jobqueue.Params)95 Test (org.junit.Test)75 JobManager (com.birbit.android.jobqueue.JobManager)47 JobHolder (com.birbit.android.jobqueue.JobHolder)46 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)41 JobQueue (com.birbit.android.jobqueue.JobQueue)40 TestConstraint (com.birbit.android.jobqueue.TestConstraint)37 Job (com.birbit.android.jobqueue.Job)31 CountDownLatch (java.util.concurrent.CountDownLatch)31 Configuration (com.birbit.android.jobqueue.config.Configuration)17 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)12 TargetApi (android.annotation.TargetApi)9 CancelResult (com.birbit.android.jobqueue.CancelResult)9 Constraint (com.birbit.android.jobqueue.Constraint)8 TagConstraint (com.birbit.android.jobqueue.TagConstraint)8 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)6 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)4 SqliteJobQueue (com.birbit.android.jobqueue.persistentQueue.sqlite.SqliteJobQueue)3