use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ConcurrentCompositeLineConsumerTest method shouldNotWriteIntoSubConsumersAfterClosingCompositeConsumer.
@Test
public void shouldNotWriteIntoSubConsumersAfterClosingCompositeConsumer() throws Exception {
// given
final String message = "Test line";
// when
concurrentCompositeLineConsumer.close();
concurrentCompositeLineConsumer.writeLine(message);
// then
for (LineConsumer subConsumer : subConsumers) {
verify(subConsumer, never()).writeLine(anyString());
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class ConcurrentCompositeLineConsumerTest method closeOperationShouldWaitUntilAllCurrentOperationsWillBeFinished.
@Test
public void closeOperationShouldWaitUntilAllCurrentOperationsWillBeFinished() throws Exception {
// given
final String message1 = "Message 1";
final String message2 = "Message 2";
WaitingAnswer<Void> waitingAnswer1 = waitOnWrite(lineConsumer2, message1);
WaitingAnswer<Void> waitingAnswer2 = waitOnWrite(lineConsumer2, message2);
// when
executor.execute(concurrentCompositeLineConsumer::close);
waitingAnswer1.completeAnswer();
waitingAnswer2.completeAnswer();
// then
awaitFinalization();
assertFalse(concurrentCompositeLineConsumer.isOpen());
for (LineConsumer consumer : subConsumers) {
verify(consumer).writeLine(eq(message1));
verify(consumer).writeLine(eq(message2));
verify(consumer, last()).close();
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class MachineProviderImpl method startService.
@Override
public Instance startService(String namespace, String workspaceId, String envName, String machineName, boolean isDev, String networkName, CheServiceImpl service, LineConsumer machineLogger) throws ServerException {
// copy to not affect/be affected by changes in origin
service = new CheServiceImpl(service);
ProgressLineFormatterImpl progressLineFormatter = new ProgressLineFormatterImpl();
ProgressMonitor progressMonitor = currentProgressStatus -> {
try {
machineLogger.writeLine(progressLineFormatter.format(currentProgressStatus));
} catch (IOException e) {
LOG.error(e.getLocalizedMessage(), e);
}
};
String container = null;
try {
String image = prepareImage(machineName, service, progressMonitor);
container = createContainer(workspaceId, machineName, isDev, image, networkName, service);
connectContainerToAdditionalNetworks(container, service);
docker.startContainer(StartContainerParams.create(container));
readContainerLogsInSeparateThread(container, workspaceId, service.getId(), machineLogger);
DockerNode node = dockerMachineFactory.createNode(workspaceId, container);
dockerInstanceStopDetector.startDetection(container, service.getId(), workspaceId);
final String userId = EnvironmentContext.getCurrent().getSubject().getUserId();
MachineImpl machine = new MachineImpl(MachineConfigImpl.builder().setDev(isDev).setName(machineName).setType("docker").setLimits(new MachineLimitsImpl((int) Size.parseSizeToMegabytes(service.getMemLimit() + "b"))).setSource(new MachineSourceImpl(service.getBuild() != null ? "context" : "image").setLocation(service.getBuild() != null ? service.getBuild().getContext() : service.getImage())).build(), service.getId(), workspaceId, envName, userId, MachineStatus.RUNNING, null);
return dockerMachineFactory.createInstance(machine, container, image, node, machineLogger);
} catch (SourceNotFoundException e) {
throw e;
} catch (RuntimeException | ServerException | NotFoundException | IOException e) {
cleanUpContainer(container);
throw new ServerException(e.getLocalizedMessage(), e);
}
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class SshMachineExecAgentLauncher method start.
protected SshMachineProcess start(SshMachineInstance machine, Agent agent) throws ServerException {
Command command = new CommandImpl(agent.getId(), agent.getScript(), "agent");
SshMachineProcess process = machine.createProcess(command, null);
LineConsumer lineConsumer = new AbstractLineConsumer() {
@Override
public void writeLine(String line) throws IOException {
machine.getLogger().writeLine(line);
}
};
CountDownLatch countDownLatch = new CountDownLatch(1);
executor.execute(ThreadLocalPropagateContext.wrap(() -> {
try {
countDownLatch.countDown();
process.start(lineConsumer);
} catch (ConflictException | MachineException e) {
try {
machine.getLogger().writeLine(format("[ERROR] %s", e.getMessage()));
} catch (IOException ignored) {
}
} finally {
try {
lineConsumer.close();
} catch (IOException ignored) {
}
}
}));
try {
// ensure that code inside of task submitted to executor is called before end of this method
countDownLatch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
return process;
}
use of org.eclipse.che.api.core.util.LineConsumer in project che by eclipse.
the class CloneTest method testLineConsumerOutputWhenCloning.
@Test(dataProvider = "GitConnectionFactory", dataProviderClass = GitConnectionFactoryProvider.class)
public void testLineConsumerOutputWhenCloning(GitConnectionFactory connectionFactory) throws ServerException, IOException, UnauthorizedException, URISyntaxException {
//given
GitConnection remoteConnection = connectToGitRepositoryWithContent(connectionFactory, remoteRepo);
GitConnection localConnection = connectionFactory.getConnection(localRepo.getAbsolutePath());
LineConsumerFactory lineConsumerFactory = mock(LineConsumerFactory.class);
LineConsumer lineConsumer = mock(LineConsumer.class);
when(lineConsumerFactory.newLineConsumer()).thenReturn(lineConsumer);
localConnection.setOutputLineConsumerFactory(lineConsumerFactory);
//when
localConnection.clone(CloneParams.create(remoteConnection.getWorkingDir().getAbsolutePath()));
//then
verify(lineConsumer, atLeastOnce()).writeLine(anyString());
}
Aggregations