Search in sources :

Example 1 with DelegatingInputStream

use of com.facebook.buck.testutil.integration.DelegatingInputStream in project buck by facebook.

the class DaemonIntegrationTest method whenClientDisconnectionDetectedThenTestIsInterrupted.

/**
   * 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 whenClientDisconnectionDetectedThenTestIsInterrupted() throws InterruptedException, IOException {
    // Sub process interruption not supported on Windows.
    assumeTrue(Platform.detect() != Platform.WINDOWS);
    // Stream timeout > test timeout.
    final long timeoutMillis = 2000;
    // Disconnect before test timeout.
    final long disconnectMillis = 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(disconnectMillis));
                }
                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 2 with DelegatingInputStream

use of com.facebook.buck.testutil.integration.DelegatingInputStream 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)

Aggregations

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 TestContext (com.facebook.buck.testutil.integration.TestContext)2 CapturingPrintStream (com.facebook.buck.util.CapturingPrintStream)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Test (org.junit.Test)2