Search in sources :

Example 86 with ExecutionContext

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));
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) FakeBuckEventListener(com.facebook.buck.event.FakeBuckEventListener) BuckEvent(com.facebook.buck.event.BuckEvent) Test(org.junit.Test)

Example 87 with ExecutionContext

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;
}
Also used : ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TestConsole(com.facebook.buck.testutil.TestConsole)

Example 88 with ExecutionContext

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));
}
Also used : Path(java.nio.file.Path) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) TestConsole(com.facebook.buck.testutil.TestConsole) Test(org.junit.Test)

Example 89 with ExecutionContext

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());
}
Also used : Path(java.nio.file.Path) ProcessExecutorParams(com.facebook.buck.util.ProcessExecutorParams) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ProcessExecutor(com.facebook.buck.util.ProcessExecutor) DefaultProcessExecutor(com.facebook.buck.util.DefaultProcessExecutor) Test(org.junit.Test)

Example 90 with ExecutionContext

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));
}
Also used : Path(java.nio.file.Path) ExecutionContext(com.facebook.buck.step.ExecutionContext) TestExecutionContext(com.facebook.buck.step.TestExecutionContext) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Test(org.junit.Test)

Aggregations

ExecutionContext (com.facebook.buck.step.ExecutionContext)176 TestExecutionContext (com.facebook.buck.step.TestExecutionContext)140 Test (org.junit.Test)128 Path (java.nio.file.Path)79 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)67 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)66 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)55 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)50 BuildRuleResolver (com.facebook.buck.rules.BuildRuleResolver)45 DefaultTargetNodeToBuildRuleTransformer (com.facebook.buck.rules.DefaultTargetNodeToBuildRuleTransformer)44 Step (com.facebook.buck.step.Step)34 ImmutableList (com.google.common.collect.ImmutableList)32 BuildTarget (com.facebook.buck.model.BuildTarget)31 SourcePath (com.facebook.buck.rules.SourcePath)26 TestConsole (com.facebook.buck.testutil.TestConsole)25 FakeProcess (com.facebook.buck.util.FakeProcess)21 ProcessExecutorParams (com.facebook.buck.util.ProcessExecutorParams)20 ImmutableMap (com.google.common.collect.ImmutableMap)20 MakeCleanDirectoryStep (com.facebook.buck.step.fs.MakeCleanDirectoryStep)19 FakeProcessExecutor (com.facebook.buck.util.FakeProcessExecutor)19