Search in sources :

Example 66 with CeTask

use of org.sonar.ce.task.CeTask in project sonarqube by SonarSource.

the class CeWorkerImplTest method log_error_when_task_failed_and_ending_state_can_not_be_persisted_to_db.

@Test
public void log_error_when_task_failed_and_ending_state_can_not_be_persisted_to_db() throws Exception {
    CeTask ceTask = createCeTask(submitter);
    when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
    taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
    IllegalStateException ex = makeTaskProcessorFail(ceTask);
    RuntimeException runtimeException = new RuntimeException("Simulate queue#remove failing");
    doThrow(runtimeException).when(queue).remove(ceTask, CeActivityDto.Status.FAILED, null, ex);
    underTest.call();
    List<String> logs = logTester.logs(LoggerLevel.INFO);
    assertThat(logs).hasSize(2);
    assertThat(logs.get(0)).contains(" | submitter=" + submitter.getLogin());
    assertThat(logs.get(1)).contains(String.format(" | submitter=%s | status=FAILED | time=", submitter.getLogin()));
    List<LogAndArguments> logAndArguments = logTester.getLogs(LoggerLevel.ERROR);
    assertThat(logAndArguments).hasSize(2);
    LogAndArguments executionErrorLog = logAndArguments.get(0);
    assertThat(executionErrorLog.getFormattedMsg()).isEqualTo("Failed to execute task " + ceTask.getUuid());
    assertThat(executionErrorLog.getArgs().get()).containsOnly(ceTask.getUuid(), ex);
    LogAndArguments finalizingErrorLog = logAndArguments.get(1);
    assertThat(finalizingErrorLog.getFormattedMsg()).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db");
    Object arg1 = finalizingErrorLog.getArgs().get()[0];
    assertThat(arg1).isSameAs(runtimeException);
    assertThat(((Exception) arg1).getSuppressed()).containsOnly(ex);
}
Also used : LogAndArguments(org.sonar.api.utils.log.LogAndArguments) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CeTask(org.sonar.ce.task.CeTask) MessageException(org.sonar.api.utils.MessageException) Test(org.junit.Test)

Example 67 with CeTask

use of org.sonar.ce.task.CeTask in project sonarqube by SonarSource.

the class CeWorkerImplTest method call_sets_and_restores_thread_name_with_information_of_worker_when_an_error_occurs.

@Test
public void call_sets_and_restores_thread_name_with_information_of_worker_when_an_error_occurs() throws Exception {
    String threadName = randomAlphabetic(3);
    CeTask ceTask = createCeTask(submitter);
    when(queue.peek(anyString(), anyBoolean())).thenAnswer(invocation -> {
        assertThat(Thread.currentThread().getName()).isEqualTo("Worker " + randomOrdinal + " (UUID=" + workerUuid + ") on " + threadName);
        return Optional.of(ceTask);
    });
    taskProcessorRepository.setProcessorForTask(CeTaskTypes.REPORT, taskProcessor);
    makeTaskProcessorFail(ceTask);
    Thread newThread = createThreadNameVerifyingThread(threadName);
    newThread.start();
    newThread.join();
}
Also used : ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 68 with CeTask

use of org.sonar.ce.task.CeTask in project sonarqube by SonarSource.

the class SimpleCeTaskInterrupterTest method onStart_has_no_effect.

@Test
public void onStart_has_no_effect() {
    CeTask ceTask = mock(CeTask.class);
    underTest.onStart(ceTask);
    verifyZeroInteractions(ceTask);
}
Also used : CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 69 with CeTask

use of org.sonar.ce.task.CeTask in project sonarqube by SonarSource.

the class SimpleCeTaskInterrupterTest method onEnd_has_no_effect.

@Test
public void onEnd_has_no_effect() {
    CeTask ceTask = mock(CeTask.class);
    underTest.onEnd(ceTask);
    verifyZeroInteractions(ceTask);
}
Also used : CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 70 with CeTask

use of org.sonar.ce.task.CeTask in project sonarqube by SonarSource.

the class CeWorkerImplTest method peek_and_process_task.

@Test
public void peek_and_process_task() throws Exception {
    CeTask task = createCeTask(null);
    taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
    when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
    assertThat(underTest.call()).isEqualTo(TASK_PROCESSED);
    verifyWorkerUuid();
    inOrder.verify(executionListener1).onStart(task);
    inOrder.verify(executionListener2).onStart(task);
    inOrder.verify(taskProcessor).process(task);
    inOrder.verify(queue).remove(task, CeActivityDto.Status.SUCCESS, null, null);
    inOrder.verify(executionListener1).onEnd(eq(task), eq(CeActivityDto.Status.SUCCESS), any(), isNull(), isNull());
    inOrder.verify(executionListener2).onEnd(eq(task), eq(CeActivityDto.Status.SUCCESS), any(), isNull(), isNull());
}
Also used : CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Aggregations

CeTask (org.sonar.ce.task.CeTask)85 Test (org.junit.Test)75 CeQueueDto (org.sonar.db.ce.CeQueueDto)17 CeActivityDto (org.sonar.db.ce.CeActivityDto)16 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)12 ComponentDto (org.sonar.db.component.ComponentDto)12 UserDto (org.sonar.db.user.UserDto)10 MessageException (org.sonar.api.utils.MessageException)7 Optional (java.util.Optional)5 Random (java.util.Random)5 CheckForNull (javax.annotation.CheckForNull)5 System2 (org.sonar.api.utils.System2)5 DbSession (org.sonar.db.DbSession)5 List (java.util.List)4 Map (java.util.Map)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Nullable (javax.annotation.Nullable)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatThrownBy (org.assertj.core.api.Assertions.assertThatThrownBy)4 Rule (org.junit.Rule)4