@Test
 public void shouldReturnCreatedJobUri() {
   // given:
   JobRunnable jobRunnable = mock(JobRunnable.class);
   when(jobRunnable.getJobDefinition()).thenReturn(someJobDefinition("BAR"));
   JobRepository jobRepository = mock(JobRepository.class);
   when(jobRepository.findRunningJobByType(anyString())).thenReturn(Optional.<JobInfo>empty());
   JobService jobService =
       new JobService(
           jobRepository,
           asList(jobRunnable),
           mock(GaugeService.class),
           executorService,
           applicationEventPublisher);
   // when:
   Optional<URI> jobUri = jobService.startAsyncJob("BAR");
   // then:
   assertThat(jobUri.get().toString(), startsWith("/internal/jobs/"));
 }
  @Test
  public void shouldReportRuntime() {
    // given:
    JobRunnable jobRunnable = mock(JobRunnable.class);
    when(jobRunnable.getJobDefinition()).thenReturn(someJobDefinition("BAR"));

    GaugeService mock = mock(GaugeService.class);
    JobService jobService =
        new JobService(
            mock(JobRepository.class),
            asList(jobRunnable),
            mock,
            executorService,
            applicationEventPublisher);
    // when:
    jobService.startAsyncJob("BAR");
    // then:
    verify(mock).submit(eq("gauge.jobs.runtime.bar"), anyLong());
  }
  @Test
  public void shouldRunJob() {
    // given:
    JobRunnable jobRunnable = mock(JobRunnable.class);
    when(jobRunnable.getJobDefinition()).thenReturn(someJobDefinition("BAR"));
    InMemJobRepository jobRepository = new InMemJobRepository();
    JobService jobService =
        new JobService(
            jobRepository,
            asList(jobRunnable),
            mock(GaugeService.class),
            executorService,
            applicationEventPublisher);

    // when:
    jobService.startAsyncJob("bar");

    // then:
    verify(executorService).execute(any(Runnable.class));
  }
 @Test
 public void shouldNotRunSameJobsInParallel() {
   // given:
   Clock clock = fixed(Instant.now(), systemDefault());
   JobRunnable jobRunnable = mock(JobRunnable.class);
   when(jobRunnable.getJobDefinition()).thenReturn(someJobDefinition("BAR"));
   InMemJobRepository jobRepository = new InMemJobRepository();
   JobService jobService =
       new JobService(
           jobRepository,
           asList(jobRunnable),
           mock(GaugeService.class),
           executorService,
           applicationEventPublisher);
   URI alreadyRunningJob = URI.create("/internal/jobs/barIsRunning");
   jobRepository.createOrUpdate(JobInfo.newJobInfo(alreadyRunningJob, "BAR", clock));
   // when:
   Optional<URI> jobUri = jobService.startAsyncJob("bar");
   // then:
   assertThat(jobUri.isPresent(), is(false));
 }