Search in sources :

Example 6 with CapturingPrintStream

use of com.facebook.buck.util.CapturingPrintStream in project buck by facebook.

the class DaemonIntegrationTest method whenClientTimeoutDetectedThenTestIsInterrupted.

/**
   * This verifies that a client disconnection will be detected by a Nailgun
   * NGInputStream reading from an empty heartbeat stream and that the generated
   * InterruptedException will interrupt command execution causing it to fail.
   */
@Test
public void whenClientTimeoutDetectedThenTestIsInterrupted() throws InterruptedException, IOException {
    // Sub process interruption not supported on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    final long timeoutMillis = 100;
    final ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "exclusive_execution", tmp);
    workspace.setUp();
    // Start with an input stream that sends heartbeats at a regular rate.
    final DelegatingInputStream inputStream = new DelegatingInputStream(TestContext.createHeartBeatStream(timeoutMillis / 10));
    // Build an NGContext connected to an NGInputStream reading from stream that will timeout.
    try (TestContext context = new TestContext(ImmutableMap.copyOf(System.getenv()), inputStream, timeoutMillis)) {
        ProcessResult result = workspace.runBuckdCommand(context, new CapturingPrintStream() {

            @Override
            public void println(String x) {
                if (x.contains("TESTING //:test")) {
                    // When tests start running, make the heartbeat stream simulate a disconnection.
                    inputStream.setDelegate(TestContext.createDisconnectionStream(2 * timeoutMillis));
                }
                super.println(x);
            }
        }, "test", "//:test");
        result.assertFailure();
        assertThat(result.getStderr(), containsString("InterruptedException"));
    }
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) DelegatingInputStream(com.facebook.buck.testutil.integration.DelegatingInputStream) TestContext(com.facebook.buck.testutil.integration.TestContext) ProcessResult(com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Example 7 with CapturingPrintStream

use of com.facebook.buck.util.CapturingPrintStream in project buck by facebook.

the class DaemonIntegrationTest method createRunnableCommand.

private Runnable createRunnableCommand(final int expectedExitCode, final String... args) {
    return () -> {
        try {
            Main main = new Main(new CapturingPrintStream(), new CapturingPrintStream(), new ByteArrayInputStream("".getBytes("UTF-8")));
            int exitCode = main.runMainWithExitCode(new BuildId(), tmp.getRoot(), Optional.of(new TestContext()), ImmutableMap.copyOf(System.getenv()), CommandMode.TEST, WatchmanWatcher.FreshInstanceAction.NONE, System.nanoTime(), args);
            assertEquals("Unexpected exit code.", expectedExitCode, exitCode);
        } catch (IOException e) {
            fail("Should not throw exception.");
            Throwables.throwIfUnchecked(e);
        } catch (InterruptedException e) {
            fail("Should not throw exception.");
            Thread.currentThread().interrupt();
        }
    };
}
Also used : CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BuildId(com.facebook.buck.model.BuildId) TestContext(com.facebook.buck.testutil.integration.TestContext) IOException(java.io.IOException)

Example 8 with CapturingPrintStream

use of com.facebook.buck.util.CapturingPrintStream in project buck by facebook.

the class UserInputTest method testAsk.

@Test
public void testAsk() throws Exception {
    String cannedAnswer = "answer";
    UserInputFixture fixture = new UserInputFixture(cannedAnswer);
    UserInput input = fixture.getUserInput();
    CapturingPrintStream outputStream = fixture.getOutputStream();
    String questionString = "question";
    String response = input.ask(questionString);
    assertThat(outputStream.getContentsAsString(Charsets.UTF_8), Matchers.containsString(questionString));
    assertThat(response, Matchers.equalTo(cannedAnswer));
}
Also used : CapturingPrintStream(com.facebook.buck.util.CapturingPrintStream) Test(org.junit.Test)

Aggregations

CapturingPrintStream (com.facebook.buck.util.CapturingPrintStream)8 Test (org.junit.Test)4 TestContext (com.facebook.buck.testutil.integration.TestContext)3 BuildId (com.facebook.buck.model.BuildId)2 DelegatingInputStream (com.facebook.buck.testutil.integration.DelegatingInputStream)2 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)2 ProcessResult (com.facebook.buck.testutil.integration.ProjectWorkspace.ProcessResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Main (com.facebook.buck.cli.Main)1 BuildReport (com.facebook.buck.command.BuildReport)1 ExecutionContext (com.facebook.buck.step.ExecutionContext)1 TestConsole (com.facebook.buck.testutil.TestConsole)1 Console (com.facebook.buck.util.Console)1 Verbosity (com.facebook.buck.util.Verbosity)1 InputStream (java.io.InputStream)1 HashMap (java.util.HashMap)1