Search in sources :

Example 36 with JobManager

use of com.birbit.android.jobqueue.JobManager 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 37 with JobManager

use of com.birbit.android.jobqueue.JobManager 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 38 with JobManager

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

Example 39 with JobManager

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

the class SingleIdTest method testSingleId.

private void testSingleId(boolean persistent) throws Throwable {
    JobManager jobManager = createJobManager();
    jobManager.stop();
    String singleId = "forks";
    DummyJob dummyJob1 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId));
    String jobId1 = addJob(jobManager, dummyJob1);
    DummyJob dummyJob2 = new DummyJob(new Params(0).setPersistent(persistent));
    String jobId2 = addJob(jobManager, dummyJob2);
    assertThat("should add job if doesn't have singleId", jobManager.countReadyJobs(), is(2));
    DummyJob dummyJob3 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId("otherId"));
    String jobId3 = addJob(jobManager, dummyJob3);
    assertThat("should add job if different singleId", jobManager.countReadyJobs(), is(3));
    DummyJob dummyJob4 = new DummyJob(new Params(0).setPersistent(persistent).setSingleId(singleId));
    addJob(jobManager, dummyJob4);
    assertThat("should not add job with same singleId", jobManager.countReadyJobs(), is(3));
    assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId1));
    assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId2));
    assertThat("should return the same id", nextJob(jobManager).getId(), is(jobId3));
    assertThat("should return the same id", nextJob(jobManager), is(nullValue()));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager)

Example 40 with JobManager

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

the class SlowOnAddedTest method testNonPersistent.

@Test
public void testNonPersistent() throws InterruptedException {
    JobManager jobManager = createJobManager();
    CountDownLatch runLatch = new CountDownLatch(1);
    MyDummyJob job = new MyDummyJob(new Params(2), runLatch);
    for (int i = 0; i < 50; i++) {
        jobManager.addJob(new DummyJob(new Params(1)));
    }
    jobManager.addJob(job);
    runLatch.await();
    assertThat("on added should be called before on run", job.onAddedCntWhenRun, equalTo(1));
}
Also used : DummyJob(com.birbit.android.jobqueue.test.jobs.DummyJob) Params(com.birbit.android.jobqueue.Params) JobManager(com.birbit.android.jobqueue.JobManager) CountDownLatch(java.util.concurrent.CountDownLatch) RetryConstraint(com.birbit.android.jobqueue.RetryConstraint) Test(org.junit.Test)

Aggregations

JobManager (com.birbit.android.jobqueue.JobManager)58 Params (com.birbit.android.jobqueue.Params)47 Test (org.junit.Test)40 DummyJob (com.birbit.android.jobqueue.test.jobs.DummyJob)35 Job (com.birbit.android.jobqueue.Job)26 CountDownLatch (java.util.concurrent.CountDownLatch)26 Configuration (com.birbit.android.jobqueue.config.Configuration)22 CancelResult (com.birbit.android.jobqueue.CancelResult)9 TargetApi (android.annotation.TargetApi)8 RetryConstraint (com.birbit.android.jobqueue.RetryConstraint)8 JobManagerCallbackAdapter (com.birbit.android.jobqueue.callback.JobManagerCallbackAdapter)8 JobHolder (com.birbit.android.jobqueue.JobHolder)6 ArrayList (java.util.ArrayList)6 JobManagerCallback (com.birbit.android.jobqueue.callback.JobManagerCallback)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Semaphore (java.util.concurrent.Semaphore)3 SuppressLint (android.annotation.SuppressLint)2 NonNull (android.support.annotation.NonNull)2 Constraint (com.birbit.android.jobqueue.Constraint)2 TagConstraint (com.birbit.android.jobqueue.TagConstraint)2