Search in sources :

Example 1 with WaitingAnswer

use of org.eclipse.che.commons.test.mockito.answer.WaitingAnswer in project che by eclipse.

the class ConcurrentCompositeLineConsumerTest method shouldIgnoreWriteToSubConsumersAfterCloseWasCalled.

@Test
public void shouldIgnoreWriteToSubConsumersAfterCloseWasCalled() throws Exception {
    // given
    WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
    doAnswer(waitingAnswer).when(lineConsumer2).close();
    executor.execute(() -> concurrentCompositeLineConsumer.close());
    waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
    // when
    concurrentCompositeLineConsumer.writeLine("Test line");
    waitingAnswer.completeAnswer();
    // then
    awaitFinalization();
    for (LineConsumer consumer : subConsumers) {
        verify(consumer, never()).writeLine(anyString());
    }
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) WaitingAnswer(org.eclipse.che.commons.test.mockito.answer.WaitingAnswer) Test(org.testng.annotations.Test)

Example 2 with WaitingAnswer

use of org.eclipse.che.commons.test.mockito.answer.WaitingAnswer in project che by eclipse.

the class ConcurrentCompositeLineConsumerTest method shouldBeAbleToWriteIntoSubConsumersSimultaneously.

@Test
public void shouldBeAbleToWriteIntoSubConsumersSimultaneously() throws Exception {
    // given
    final String message1 = "Message 1";
    final String message2 = "Message 2";
    WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
    doAnswer(waitingAnswer).when(lineConsumer2).writeLine(eq(message1));
    executor.execute(() -> concurrentCompositeLineConsumer.writeLine(message1));
    waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
    // when
    concurrentCompositeLineConsumer.writeLine(message2);
    waitingAnswer.completeAnswer();
    // then
    awaitFinalization();
    for (LineConsumer consumer : subConsumers) {
        verify(consumer).writeLine(eq(message1));
        verify(consumer).writeLine(eq(message2));
    }
}
Also used : LineConsumer(org.eclipse.che.api.core.util.LineConsumer) WaitingAnswer(org.eclipse.che.commons.test.mockito.answer.WaitingAnswer) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 3 with WaitingAnswer

use of org.eclipse.che.commons.test.mockito.answer.WaitingAnswer in project che by eclipse.

the class ConcurrentFileLineConsumerTest method shouldBeAbleToWriteIntoFileSimultaneously.

@Test
public void shouldBeAbleToWriteIntoFileSimultaneously() throws Exception {
    // given
    final String message1 = "Message 1";
    final String message2 = "Message 2";
    WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
    doAnswer(waitingAnswer).when(writer).write(eq(message1));
    executor.execute(() -> writeIntoConsumer(message1));
    waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
    // when
    writeIntoConsumer(message2);
    waitingAnswer.completeAnswer();
    // then
    awaitFinalization();
    verify(writer).write(eq(message1));
    verify(writer).write(eq(message2));
}
Also used : WaitingAnswer(org.eclipse.che.commons.test.mockito.answer.WaitingAnswer) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test)

Example 4 with WaitingAnswer

use of org.eclipse.che.commons.test.mockito.answer.WaitingAnswer in project che by eclipse.

the class WorkspaceRuntimeIntegrationTest method environmentEngineShouldDestroyAllMachinesBeforeRemovalOfEnvironmentRecord.

// Check for https://github.com/codenvy/codenvy/issues/593
@Test(expectedExceptions = NotFoundException.class, expectedExceptionsMessageRegExp = "Workspace with id '" + WORKSPACE_ID + "' is not running")
public void environmentEngineShouldDestroyAllMachinesBeforeRemovalOfEnvironmentRecord() throws Exception {
    // given
    EnvironmentDto environment = newDto(EnvironmentDto.class);
    environment.withMachines(singletonMap("service1", newDto(ExtendedMachineDto.class).withAgents(singletonList("org.eclipse.che.ws-agent"))));
    WorkspaceConfigDto config = newDto(WorkspaceConfigDto.class).withDefaultEnv(ENV_NAME).withName("ws1").withEnvironments(singletonMap(ENV_NAME, environment));
    WorkspaceDto workspace = newDto(WorkspaceDto.class).withId(WORKSPACE_ID).withNamespace("namespace").withConfig(config);
    Instance instance = mock(Instance.class);
    MachineConfigImpl machineConfig = new MachineConfigImpl();
    machineConfig.setDev(true);
    machineConfig.setName("service1");
    when(instance.getWorkspaceId()).thenReturn(WORKSPACE_ID);
    when(instance.getId()).thenReturn("machineId");
    when(instance.getConfig()).thenReturn(machineConfig);
    CheServicesEnvironmentImpl internalEnv = new CheServicesEnvironmentImpl();
    internalEnv.getServices().put("service1", new CheServiceImpl().withId("machineId"));
    when(environmentParser.parse(any(Environment.class))).thenReturn(internalEnv);
    when(instanceProvider.startService(anyString(), anyString(), anyString(), anyString(), anyBoolean(), anyString(), any(CheServiceImpl.class), any(LineConsumer.class))).thenReturn(instance);
    runtimes.startAsync(workspace, ENV_NAME, false);
    verify(sharedPool).submit(taskCaptor.capture());
    taskCaptor.getValue().call();
    WaitingAnswer<Void> waitingAnswer = new WaitingAnswer<>();
    doAnswer(waitingAnswer).when(instance).destroy();
    // when
    executor.execute(() -> {
        try {
            runtimes.stop(WORKSPACE_ID);
        } catch (Exception e) {
            LOG.error(e.getLocalizedMessage(), e);
        }
    });
    waitingAnswer.waitAnswerCall(1, TimeUnit.SECONDS);
    // then
    // no exception - environment and workspace are still running
    runtimes.getRuntime(WORKSPACE_ID);
    // let instance removal proceed
    waitingAnswer.completeAnswer();
    // verify destroying was called
    verify(instance, timeout(1000)).destroy();
    verify(instanceProvider, timeout(1000)).destroyNetwork(anyString());
    // wait to ensure that removal of runtime is finished
    Thread.sleep(500);
    // runtime is removed - now getting of it should throw an exception
    runtimes.getRuntime(WORKSPACE_ID);
}
Also used : WaitingAnswer(org.eclipse.che.commons.test.mockito.answer.WaitingAnswer) Instance(org.eclipse.che.api.machine.server.spi.Instance) CheServiceImpl(org.eclipse.che.api.environment.server.model.CheServiceImpl) EnvironmentDto(org.eclipse.che.api.workspace.shared.dto.EnvironmentDto) CheServicesEnvironmentImpl(org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl) WorkspaceDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceDto) NotFoundException(org.eclipse.che.api.core.NotFoundException) LineConsumer(org.eclipse.che.api.core.util.LineConsumer) MachineConfigImpl(org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl) Environment(org.eclipse.che.api.core.model.workspace.Environment) WorkspaceConfigDto(org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto) ExtendedMachineDto(org.eclipse.che.api.workspace.shared.dto.ExtendedMachineDto) Test(org.testng.annotations.Test)

Aggregations

WaitingAnswer (org.eclipse.che.commons.test.mockito.answer.WaitingAnswer)4 Test (org.testng.annotations.Test)4 LineConsumer (org.eclipse.che.api.core.util.LineConsumer)3 Matchers.anyString (org.mockito.Matchers.anyString)2 NotFoundException (org.eclipse.che.api.core.NotFoundException)1 Environment (org.eclipse.che.api.core.model.workspace.Environment)1 CheServiceImpl (org.eclipse.che.api.environment.server.model.CheServiceImpl)1 CheServicesEnvironmentImpl (org.eclipse.che.api.environment.server.model.CheServicesEnvironmentImpl)1 MachineConfigImpl (org.eclipse.che.api.machine.server.model.impl.MachineConfigImpl)1 Instance (org.eclipse.che.api.machine.server.spi.Instance)1 EnvironmentDto (org.eclipse.che.api.workspace.shared.dto.EnvironmentDto)1 ExtendedMachineDto (org.eclipse.che.api.workspace.shared.dto.ExtendedMachineDto)1 WorkspaceConfigDto (org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto)1 WorkspaceDto (org.eclipse.che.api.workspace.shared.dto.WorkspaceDto)1