Search in sources :

Example 1 with VolunteerJob

use of gov.cms.bfd.pipeline.app.volunteer.VolunteerJob in project beneficiary-fhir-data by CMSgov.

the class PipelineManagerIT method runThenStopAndCancelPendingJobs.

/**
 * Verifies that {@link PipelineManager#stop()} works, as expected.
 *
 * @throws Exception Any unhandled {@link Exception}s will cause this test case to fail.
 */
@Test
public void runThenStopAndCancelPendingJobs() throws Exception {
    // Create the pipeline and a slow mock job that we can use.
    PipelineJobRecordStore jobRecordStore = new PipelineJobRecordStore(PipelineTestUtils.get().getPipelineApplicationState().getMetrics());
    try (PipelineManager pipelineManager = new PipelineManager(PipelineTestUtils.get().getPipelineApplicationState().getMetrics(), jobRecordStore)) {
        MockJob mockJob = new MockJob(Optional.empty(), () -> {
            // Add an artificial delay that we'll be able to measure.
            Thread.sleep(500);
            return PipelineJobOutcome.WORK_DONE;
        });
        pipelineManager.registerJob(mockJob);
        /*
       * Once the VolunteerJob is running, submit enough slow mock jobs to fill up the
       * PipelineManager's executor threads/slots.
       */
        Awaitility.await().atMost(1, TimeUnit.SECONDS).until(() -> jobRecordStore.getJobRecords().stream().filter(j -> VolunteerJob.JOB_TYPE.equals(j.getJobType()) && j.isStarted()).findAny().isPresent());
        int openExecutorSlots = pipelineManager.getOpenExecutorSlots();
        for (int i = 0; i < openExecutorSlots; i++) {
            jobRecordStore.submitPendingJob(MockJob.JOB_TYPE, null);
        }
        // Add one extra job that should sit as pending for a bit.
        jobRecordStore.submitPendingJob(MockJob.JOB_TYPE, null);
        // Wait until one of the mock jobs has started.
        Awaitility.await().atMost(1, TimeUnit.SECONDS).until(() -> jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && j.isStarted()).findAny().isPresent());
        // Stop the pipeline and verify that at least one job was cancelled before it started.
        pipelineManager.stop();
        assertTrue(jobRecordStore.getJobRecords().stream().filter(j -> MockJob.JOB_TYPE.equals(j.getJobType()) && !j.isStarted()).findAny().isPresent());
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) NullPipelineJobArguments(gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments) LoggerFactory(org.slf4j.LoggerFactory) SchedulerJob(gov.cms.bfd.pipeline.app.scheduler.SchedulerJob) PipelineJob(gov.cms.bfd.pipeline.sharedutils.PipelineJob) Callable(java.util.concurrent.Callable) VolunteerJob(gov.cms.bfd.pipeline.app.volunteer.VolunteerJob) Disabled(org.junit.jupiter.api.Disabled) PipelineJobSchedule(gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule) PipelineTestUtils(gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils) PipelineJobRecord(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord) BeforeAll(org.junit.jupiter.api.BeforeAll) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) PipelineJobOutcome(gov.cms.bfd.pipeline.sharedutils.PipelineJobOutcome) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) BadCodeMonkeyException(gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException) Set(java.util.Set) PipelineJobType(gov.cms.bfd.pipeline.sharedutils.PipelineJobType) Collectors(java.util.stream.Collectors) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) AfterEach(org.junit.jupiter.api.AfterEach) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Slf4jReporter(com.codahale.metrics.Slf4jReporter) Optional(java.util.Optional) Awaitility(org.awaitility.Awaitility) PipelineJobRecordStore(gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore) Test(org.junit.jupiter.api.Test)

Aggregations

MetricRegistry (com.codahale.metrics.MetricRegistry)1 Slf4jReporter (com.codahale.metrics.Slf4jReporter)1 SchedulerJob (gov.cms.bfd.pipeline.app.scheduler.SchedulerJob)1 VolunteerJob (gov.cms.bfd.pipeline.app.volunteer.VolunteerJob)1 NullPipelineJobArguments (gov.cms.bfd.pipeline.sharedutils.NullPipelineJobArguments)1 PipelineJob (gov.cms.bfd.pipeline.sharedutils.PipelineJob)1 PipelineJobOutcome (gov.cms.bfd.pipeline.sharedutils.PipelineJobOutcome)1 PipelineJobSchedule (gov.cms.bfd.pipeline.sharedutils.PipelineJobSchedule)1 PipelineJobType (gov.cms.bfd.pipeline.sharedutils.PipelineJobType)1 PipelineTestUtils (gov.cms.bfd.pipeline.sharedutils.PipelineTestUtils)1 PipelineJobRecord (gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecord)1 PipelineJobRecordStore (gov.cms.bfd.pipeline.sharedutils.jobs.store.PipelineJobRecordStore)1 BadCodeMonkeyException (gov.cms.bfd.sharedutils.exceptions.BadCodeMonkeyException)1 ChronoUnit (java.time.temporal.ChronoUnit)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Callable (java.util.concurrent.Callable)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Awaitility (org.awaitility.Awaitility)1