Example usage for org.apache.hadoop.net ServerSocketUtil getPort

List of usage examples for org.apache.hadoop.net ServerSocketUtil getPort

Introduction

In this page you can find the example usage for org.apache.hadoop.net ServerSocketUtil getPort.

Prototype

public static int getPort(int port, int retries) throws IOException 

Source Link

Document

Port scan & allocate is how most other apps find ports

Usage

From source file:org.apache.drill.test.TestGracefulShutdown.java

License:Apache License

@Test // DRILL-6912
public void gracefulShutdownThreadShouldBeInitializedBeforeClosingDrillbit() throws Exception {
    Drillbit drillbit = null;/*w ww.j a  v  a 2 s .c  om*/
    Drillbit drillbitWithSamePort = null;

    int userPort = ServerSocketUtil.getPort(31170, 300);
    int bitPort = ServerSocketUtil.getPort(31180, 300);
    ClusterFixtureBuilder fixtureBuilder = ClusterFixture.bareBuilder(dirTestWatcher).withLocalZk()
            .configProperty(ExecConstants.INITIAL_USER_PORT, userPort)
            .configProperty(ExecConstants.INITIAL_BIT_PORT, bitPort);
    try (ClusterFixture clusterFixture = fixtureBuilder.build()) {
        drillbit = clusterFixture.drillbit();

        // creating another drillbit instance using same config
        drillbitWithSamePort = new Drillbit(clusterFixture.config(),
                fixtureBuilder.configBuilder().getDefinitions(), clusterFixture.serviceSet());

        try {
            drillbitWithSamePort.run();
            fail("drillbitWithSamePort.run() should throw UserException");
        } catch (UserException e) {
            // it's expected that second drillbit can't be started because port is busy
            assertThat(e.getMessage(), containsString("RESOURCE ERROR: Drillbit could not bind to port"));
        }
    } finally {
        // preconditions
        assertNotNull(drillbit);
        assertNotNull(drillbitWithSamePort);
        assertNotNull("gracefulShutdownThread should be initialized, otherwise NPE will be thrown from close()",
                drillbit.getGracefulShutdownThread());
        // main test case
        assertNotNull("gracefulShutdownThread should be initialized, otherwise NPE will be thrown from close()",
                drillbitWithSamePort.getGracefulShutdownThread());
        drillbit.close();
        drillbitWithSamePort.close();
    }
}