Search in sources :

Example 36 with CeTask

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

the class CeWorkerImplTest method isExecutedBy_returns_false_unless_a_thread_is_currently_executing_a_task.

@Test
public void isExecutedBy_returns_false_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.isExecutedBy(Thread.currentThread())).isFalse();
        assertThat(underTest.isExecutedBy(new Thread())).isFalse();
        assertThat(underTest.isExecutedBy(t)).isTrue();
    } finally {
        assertionsDoneLatch.countDown();
        t.join();
    }
    assertThat(underTest.isExecutedBy(Thread.currentThread())).isFalse();
    assertThat(underTest.isExecutedBy(new Thread())).isFalse();
    assertThat(underTest.isExecutedBy(t)).isFalse();
}
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)

Example 37 with CeTask

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

the class CeWorkerImplTest method peek_and_process_task_without_listeners.

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

Example 38 with CeTask

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

the class CeWorkerImplTest method display_submitterLogin_in_logs_when_set_in_case_of_error.

@Test
public void display_submitterLogin_in_logs_when_set_in_case_of_error() throws Exception {
    UserDto userDto = insertRandomUser();
    CeTask ceTask = createCeTask(toTaskSubmitter(userDto));
    when(queue.peek(anyString(), anyBoolean())).thenReturn(Optional.of(ceTask));
    taskProcessorRepository.setProcessorForTask(ceTask.getType(), taskProcessor);
    makeTaskProcessorFail(ceTask);
    underTest.call();
    verifyWorkerUuid();
    List<String> logs = logTester.logs(LoggerLevel.INFO);
    assertThat(logs).hasSize(2);
    assertThat(logs.get(0)).contains(String.format("submitter=%s", userDto.getLogin()));
    assertThat(logs.get(1)).contains(String.format("submitter=%s | status=FAILED | time=", userDto.getLogin()));
    logs = logTester.logs(LoggerLevel.ERROR);
    assertThat(logs).hasSize(1);
    assertThat(logs.get(0)).isEqualTo("Failed to execute task " + ceTask.getUuid());
}
Also used : UserDto(org.sonar.db.user.UserDto) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 39 with CeTask

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

the class InternalCeQueueImplTest method peek_populates_name_and_key_for_existing_component_and_main_component.

@Test
public void peek_populates_name_and_key_for_existing_component_and_main_component() {
    ComponentDto project = db.components().insertPrivateProject();
    ComponentDto branch = db.components().insertProjectBranch(project);
    CeTask task = submit(CeTaskTypes.REPORT, branch);
    Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, true);
    assertThat(peek).isPresent();
    assertThat(peek.get().getUuid()).isEqualTo(task.getUuid());
    assertThat(peek.get().getType()).isEqualTo(CeTaskTypes.REPORT);
    assertThat(peek.get().getComponent()).contains(new CeTask.Component(branch.uuid(), branch.getDbKey(), branch.name()));
    assertThat(peek.get().getMainComponent()).contains(new CeTask.Component(project.uuid(), project.getDbKey(), project.name()));
    // no more pending tasks
    peek = underTest.peek(WORKER_UUID_2, true);
    assertThat(peek).isEmpty();
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) CeTask(org.sonar.ce.task.CeTask) Test(org.junit.Test)

Example 40 with CeTask

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

the class InternalCeQueueImplTest method remove_does_not_set_analysisUuid_in_CeActivity_when_CeTaskResult_has_no_analysis_uuid.

@Test
public void remove_does_not_set_analysisUuid_in_CeActivity_when_CeTaskResult_has_no_analysis_uuid() {
    CeTask task = submit(CeTaskTypes.REPORT, newProjectDto("PROJECT_1"));
    Optional<CeTask> peek = underTest.peek(WORKER_UUID_1, true);
    underTest.remove(peek.get(), CeActivityDto.Status.SUCCESS, newTaskResult(null), null);
    // available in history
    Optional<CeActivityDto> history = db.getDbClient().ceActivityDao().selectByUuid(db.getSession(), task.getUuid());
    assertThat(history).isPresent();
    assertThat(history.get().getAnalysisUuid()).isNull();
}
Also used : CeActivityDto(org.sonar.db.ce.CeActivityDto) 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