Search in sources :

Example 61 with CeTask

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

the class CeTaskLoggingWorkerExecutionListenerTest method onStart_calls_initForTask_with_method_argument.

@Test
public void onStart_calls_initForTask_with_method_argument() {
    CeTask ceTask = Mockito.mock(CeTask.class);
    underTest.onStart(ceTask);
    verify(ceTaskLogging).initForTask(ceTask);
    verifyNoMoreInteractions(ceTaskLogging);
}
Also used : CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 62 with CeTask

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

the class CeWorkerImplTest method log_error_when_task_was_successful_but_ending_state_can_not_be_persisted_to_db.

@Test
public void log_error_when_task_was_successful_but_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);
    doThrow(new RuntimeException("Simulate queue#remove failing")).when(queue).remove(ceTask, CeActivityDto.Status.SUCCESS, null, null);
    underTest.call();
    assertThat(logTester.logs(LoggerLevel.ERROR)).containsOnly("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db");
}
Also used : CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 63 with CeTask

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

the class CeWorkerImplTest method fail_to_process_task_without_listeners.

@Test
public void fail_to_process_task_without_listeners() throws Exception {
    CeTask task = createCeTask(null);
    when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(task));
    taskProcessorRepository.setProcessorForTask(task.getType(), taskProcessor);
    Throwable error = makeTaskProcessorFail(task);
    assertThat(underTestNoListener.call()).isEqualTo(TASK_PROCESSED);
    verifyWorkerUuid();
    inOrder.verify(taskProcessor).process(task);
    inOrder.verify(queue).remove(task, CeActivityDto.Status.FAILED, null, error);
    inOrder.verifyNoMoreInteractions();
}
Also used : CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 64 with CeTask

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

the class CeWorkerImplTest method log_error_as_suppressed_when_task_failed_with_MessageException_and_ending_state_can_not_be_persisted_to_db.

@Test
public void log_error_as_suppressed_when_task_failed_with_MessageException_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);
    MessageException ex = makeTaskProcessorFail(ceTask, MessageException.of("simulate MessageException thrown by TaskProcessor#process"));
    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(1);
    assertThat(logAndArguments.get(0).getFormattedMsg()).isEqualTo("Failed to finalize task with uuid '" + ceTask.getUuid() + "' and persist its state to db");
    Object arg1 = logAndArguments.get(0).getArgs().get()[0];
    assertThat(arg1).isSameAs(runtimeException);
    assertThat(((Exception) arg1).getSuppressed()).containsOnly(ex);
}
Also used : MessageException(org.sonar.api.utils.MessageException) 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 65 with CeTask

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

the class CeWorkerImplTest method getCurrentTask_returns_empty_unless_a_thread_is_currently_executing_a_task.

@Test
public void getCurrentTask_returns_empty_unless_a_thread_is_currently_executing_a_task() throws InterruptedException {
    CountDownLatch inCallLatch = new CountDownLatch(1);
    CountDownLatch assertionsDoneLatch = new CountDownLatch(1);
    String taskType = randomAlphabetic(12);
    CeTask ceTask = mock(CeTask.class);
    when(ceTask.getType()).thenReturn(taskType);
    when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
    taskProcessorRepository.setProcessorForTask(taskType, new SimpleCeTaskProcessor() {

        @CheckForNull
        @Override
        public CeTaskResult process(CeTask task) {
            inCallLatch.countDown();
            try {
                assertionsDoneLatch.await(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return null;
        }
    });
    Thread t = callCallInNewThread(underTest);
    try {
        t.start();
        inCallLatch.await(10, TimeUnit.SECONDS);
        assertThat(underTest.getCurrentTask()).contains(ceTask);
    } finally {
        assertionsDoneLatch.countDown();
        t.join();
    }
    assertThat(underTest.getCurrentTask()).isEmpty();
}
Also used : CeTaskResult(org.sonar.ce.task.CeTaskResult) CheckForNull(javax.annotation.CheckForNull) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) 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