use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class WorkerShellStepTest method testStdErrIsPrintedAsErrorIfJobFails.
@Test
public void testStdErrIsPrintedAsErrorIfJobFails() throws IOException, InterruptedException {
String stderr = "my stderr";
ExecutionContext context = createExecutionContextWith(1, "", stderr);
WorkerShellStep step = createWorkerShellStep(createJobParams(ImmutableList.of(startupCommand), startupArgs, ImmutableMap.of(), "myJobArgs"), null, null);
FakeBuckEventListener listener = new FakeBuckEventListener();
context.getBuckEventBus().register(listener);
int exitCode = step.execute(context).getExitCode();
assertThat(exitCode, Matchers.equalTo(1));
// assert that the job's stderr was written to the console as error, not as warning
BuckEvent firstEvent = listener.getEvents().get(0);
assertTrue(firstEvent instanceof ConsoleEvent);
assertThat(((ConsoleEvent) firstEvent).getLevel(), Matchers.is(Level.SEVERE));
assertThat(((ConsoleEvent) firstEvent).getMessage(), Matchers.is(stderr));
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class WorkerShellStepTest method createExecutionContextWith.
private ExecutionContext createExecutionContextWith(final ImmutableMap<String, WorkerJobResult> jobArgs, final int poolCapacity) {
WorkerProcessPool workerProcessPool = new WorkerProcessPool(poolCapacity, Hashing.sha1().hashString(fakeWorkerStartupCommand, Charsets.UTF_8)) {
@Override
protected WorkerProcess startWorkerProcess() throws IOException {
return new FakeWorkerProcess(jobArgs);
}
};
ConcurrentHashMap<String, WorkerProcessPool> workerProcessMap = new ConcurrentHashMap<>();
workerProcessMap.put(fakeWorkerStartupCommand, workerProcessPool);
WorkerProcessPool persistentWorkerProcessPool = new WorkerProcessPool(poolCapacity, Hashing.sha1().hashString(fakePersistentWorkerStartupCommand, Charsets.UTF_8)) {
@Override
protected WorkerProcess startWorkerProcess() throws IOException {
return new FakeWorkerProcess(jobArgs);
}
};
ConcurrentHashMap<String, WorkerProcessPool> persistentWorkerProcessMap = new ConcurrentHashMap<>();
persistentWorkerProcessMap.put(persistentWorkerKey, persistentWorkerProcessPool);
ExecutionContext context = TestExecutionContext.newBuilder().setPlatform(Platform.LINUX).setWorkerProcessPools(workerProcessMap).setPersistentWorkerPools(persistentWorkerProcessMap).setConsole(new TestConsole(Verbosity.ALL)).setBuckEventBus(BuckEventBusFactory.newInstance()).build();
return context;
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class WorkerShellStepTest method testMultipleWorkerProcesses.
@Test
public void testMultipleWorkerProcesses() throws IOException, InterruptedException {
String jobArgsA = "jobArgsA";
String jobArgsB = "jobArgsB";
final ImmutableMap<String, WorkerJobResult> jobResults = ImmutableMap.of(jobArgsA, WorkerJobResult.of(0, Optional.of("stdout A"), Optional.of("stderr A")), jobArgsB, WorkerJobResult.of(0, Optional.of("stdout B"), Optional.of("stderr B")));
class WorkerShellStepWithFakeProcesses extends WorkerShellStep {
WorkerShellStepWithFakeProcesses(WorkerJobParams jobParams) {
super(Optional.ofNullable(jobParams), Optional.empty(), Optional.empty(), new WorkerProcessPoolFactory(new FakeProjectFilesystem()) {
@Override
WorkerProcess createWorkerProcess(ProcessExecutorParams processParams, ExecutionContext context, Path tmpDir) throws IOException {
try {
sleep(5);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
return new FakeWorkerProcess(jobResults);
}
});
}
}
ExecutionContext context = TestExecutionContext.newBuilder().setPlatform(Platform.LINUX).setConsole(new TestConsole(Verbosity.ALL)).setBuckEventBus(BuckEventBusFactory.newInstance()).build();
WorkerJobParams jobParamsA = createJobParams(ImmutableList.of(startupCommand), startupArgs, ImmutableMap.of(), jobArgsA, 2);
WorkerShellStep stepA = new WorkerShellStepWithFakeProcesses(jobParamsA);
WorkerShellStep stepB = new WorkerShellStepWithFakeProcesses(jobParamsA.withJobArgs(jobArgsB));
Thread[] threads = { new ConcurrentExecution(stepA, context), new ConcurrentExecution(stepB, context) };
for (Thread t : threads) {
t.start();
}
for (Thread t : threads) {
t.join();
}
Collection<WorkerProcessPool> pools = context.getWorkerProcessPools().values();
assertThat(pools.size(), Matchers.equalTo(1));
WorkerProcessPool pool = pools.iterator().next();
assertThat(pool.getCapacity(), Matchers.equalTo(2));
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class SymlinkFileStepTest method testReplaceMalformedSymlink.
@Test
public void testReplaceMalformedSymlink() throws IOException, InterruptedException {
assumeTrue(Platform.detect() != Platform.WINDOWS);
// Run `ln -s /path/that/does/not/exist dummy` in /tmp.
ProcessExecutorParams params = ProcessExecutorParams.builder().setCommand(ImmutableList.of("ln", "-s", "/path/that/does/not/exist", "my_symlink")).setDirectory(tmpDir.getRoot().toPath()).build();
ProcessExecutor executor = new DefaultProcessExecutor(Console.createNullConsole());
executor.launchAndExecute(params);
// Verify that the symlink points to a non-existent file.
Path symlink = Paths.get(tmpDir.getRoot().getAbsolutePath(), "my_symlink");
assertFalse("exists() should reflect the existence of what the symlink points to", symlink.toFile().exists());
assertTrue("even though exists() is false, isSymbolicLink should be true", java.nio.file.Files.isSymbolicLink(symlink));
// Create an ExecutionContext to return the ProjectFilesystem.
ProjectFilesystem projectFilesystem = new ProjectFilesystem(tmpDir.getRoot().toPath());
ExecutionContext executionContext = TestExecutionContext.newInstance();
tmpDir.newFile("dummy");
SymlinkFileStep symlinkStep = new SymlinkFileStep(projectFilesystem, /* source */
Paths.get("dummy"), /* target */
Paths.get("my_symlink"), /* useAbsolutePaths*/
true);
int exitCode = symlinkStep.execute(executionContext).getExitCode();
assertEquals(0, exitCode);
assertTrue(java.nio.file.Files.isSymbolicLink(symlink));
assertTrue(symlink.toFile().exists());
}
use of com.facebook.buck.step.ExecutionContext in project buck by facebook.
the class TouchStepTest method testFileGetsCreated.
@Test
public void testFileGetsCreated() throws IOException, InterruptedException {
Path path = Paths.get("somefile");
assertFalse(path.toFile().exists());
ProjectFilesystem projectFilesystem = new FakeProjectFilesystem(new IncrementingFakeClock(TimeUnit.MILLISECONDS.toNanos(1)), tmp.getRoot(), ImmutableSet.of());
TouchStep touchStep = new TouchStep(projectFilesystem, path);
ExecutionContext executionContext = TestExecutionContext.newInstance();
touchStep.execute(executionContext);
assertTrue(projectFilesystem.exists(path));
}
Aggregations