Search in sources :

Example 31 with BindException

use of java.net.BindException in project geode by apache.

the class LocatorLauncherLocalIntegrationTest method testStartUsingPortInUseFails.

@Test
public void testStartUsingPortInUseFails() throws Throwable {
    // Test makes no sense in this case
    if (this.locatorPort == 0) {
        return;
    }
    // generate one free port and then use it instead of default
    this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(this.locatorPort, 50, null, -1);
    this.launcher = new Builder().setMemberName(getUniqueName()).setPort(this.locatorPort).setRedirectOutput(true).setWorkingDirectory(this.workingDirectory).set(CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory).set(LOG_LEVEL, "config").build();
    RuntimeException expected = null;
    try {
        this.launcher.start();
        fail("LocatorLauncher start should have thrown RuntimeException caused by BindException");
    } catch (RuntimeException e) {
        expected = e;
        assertNotNull(expected.getMessage());
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertNotNull(expected);
        final Throwable cause = expected.getCause();
        assertNotNull(cause);
        assertTrue(cause instanceof BindException);
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertFalse("Pid file should not exist: " + this.pidFile, this.pidFile.exists());
        // creation of log file seems to be random -- look into why sometime
        final String logFileName = getUniqueName() + ".log";
        assertFalse("Log file should not exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // just in case the launcher started...
    LocatorState status = null;
    try {
        status = this.launcher.stop();
    } catch (Throwable t) {
    // ignore
    }
    try {
        waitForFileToDelete(this.pidFile);
        assertEquals(getExpectedStopStatusForNotRunning(), status.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) BindException(java.net.BindException) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 32 with BindException

use of java.net.BindException in project geode by apache.

the class LocatorLauncherLocalIntegrationTest method testStartWithDefaultPortInUseFails.

/*
   * assertTrue(getUniqueName() + " is broken if PID == Integer.MAX_VALUE",
   * ProcessUtils.identifyPid() != Integer.MAX_VALUE);
   * 
   * // create existing pid file this.pidFile = new File(ProcessType.LOCATOR.getPidFileName());
   * final int realPid = Host.getHost(0).getVM(3).invoke(() -> ProcessUtils.identifyPid());
   * assertFalse(realPid == ProcessUtils.identifyPid()); writePid(this.pidFile, realPid);
   * 
   * // build and start the locator final Builder builder = new Builder() .setForce(true)
   * .setMemberName(getUniqueName()) .setPort(this.locatorPort) .setRedirectOutput(true)
   * 
   * assertTrue(builder.getForce()); this.launcher = builder.build();
   * assertTrue(this.launcher.isForcing()); this.launcher.start();
   * 
   * // collect and throw the FIRST failure Throwable failure = null;
   * 
   * try { waitForLocatorToStart(this.launcher);
   * 
   * // validate the pid file and its contents assertTrue(this.pidFile.exists()); final int pid =
   * readPid(this.pidFile); assertTrue(pid > 0); assertTrue(ProcessUtils.isProcessAlive(pid));
   * assertIndexDetailsEquals(getPid(), pid);
   * 
   * // validate log file was created final String logFileName = getUniqueName()+".log";
   * assertTrue("Log file should exist: " + logFileName, new File(logFileName).exists());
   * 
   * } catch (Throwable e) { logger.error(e); if (failure == null) { failure = e; } }
   * 
   * try { assertIndexDetailsEquals(Status.STOPPED, this.launcher.stop().getStatus());
   * waitForFileToDelete(this.pidFile); } catch (Throwable e) { logger.error(e); if (failure ==
   * null) { failure = e; } }
   * 
   * if (failure != null) { throw failure; } } // testStartUsingForceOverwritesExistingPidFile
   */
@Test
public void testStartWithDefaultPortInUseFails() throws Throwable {
    // Test makes no sense in this case
    if (this.locatorPort == 0) {
        return;
    }
    this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(this.locatorPort, 50, null, -1);
    assertTrue(this.socket.isBound());
    assertFalse(this.socket.isClosed());
    assertFalse(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
    assertNotNull(System.getProperty(DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY));
    assertEquals(this.locatorPort, Integer.valueOf(System.getProperty(DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY)).intValue());
    assertFalse(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
    this.launcher = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(this.workingDirectory).set(CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory).set(LOG_LEVEL, "config").build();
    assertEquals(this.locatorPort, this.launcher.getPort().intValue());
    RuntimeException expected = null;
    try {
        this.launcher.start();
        // why did it not fail like it's supposed to?
        final String property = System.getProperty(DistributionLocator.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY);
        assertNotNull(property);
        assertEquals(this.locatorPort, Integer.valueOf(property).intValue());
        assertFalse(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET));
        assertEquals(this.locatorPort, this.launcher.getPort().intValue());
        assertEquals(this.locatorPort, this.socket.getLocalPort());
        assertTrue(this.socket.isBound());
        assertFalse(this.socket.isClosed());
        fail("LocatorLauncher start should have thrown RuntimeException caused by BindException");
    } catch (RuntimeException e) {
        expected = e;
        assertNotNull(expected.getMessage());
    // BindException text varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertNotNull(expected);
        final Throwable cause = expected.getCause();
        assertNotNull(cause);
        assertTrue(cause instanceof BindException);
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertFalse("Pid file should not exist: " + this.pidFile, this.pidFile.exists());
        // creation of log file seems to be random -- look into why sometime
        final String logFileName = getUniqueName() + ".log";
        assertFalse("Log file should not exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // just in case the launcher started...
    LocatorState status = null;
    try {
        status = this.launcher.stop();
    } catch (Throwable t) {
    // ignore
    }
    try {
        waitForFileToDelete(this.pidFile);
        assertEquals(getExpectedStopStatusForNotRunning(), status.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) BindException(java.net.BindException) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 33 with BindException

use of java.net.BindException in project geode by apache.

the class ServerLauncherLocalIntegrationTest method testStartUsingServerPortInUseFails.

/*
                          * // create existing pid file final int realPid =
                          * Host.getHost(0).getVM(3).invoke(() -> ProcessUtils.identifyPid());
                          * assertFalse("Remote pid shouldn't be the same as local pid " + realPid,
                          * realPid == ProcessUtils.identifyPid());
                          * 
                          * this.pidFile = new File(ProcessType.SERVER.getPidFileName());
                          * writePid(this.pidFile, realPid);
                          * 
                          * // build and start the server final Builder builder = new Builder()
                          * .setDisableDefaultServer(true) .setMemberName(getUniqueName())
                          * .setRedirectOutput(true) .set(logLevel, "config")
                          * .set(DistributionConfig.ConfigurationProperties.MCAST_PORT, "0");
                          * 
                          * assertFalse(builder.getForce()); this.launcher = builder.build();
                          * assertFalse(this.launcher.isForcing());
                          * 
                          * // collect and throw the FIRST failure Throwable failure = null;
                          * RuntimeException expected = null;
                          * 
                          * try { this.launcher.start();
                          * fail("ServerLauncher start should have thrown RuntimeException caused by FileAlreadyExistsException"
                          * ); } catch (RuntimeException e) { expected = e;
                          * assertNotNull(expected.getMessage()); assertTrue(expected.getMessage().
                          * contains("A PID file already exists and a Server may be running in")); }
                          * catch (Throwable e) { logger.error(e); if (failure == null) { failure =
                          * e; } }
                          * 
                          * // just in case the launcher started... ServerState status = null; try {
                          * status = this.launcher.stop(); } catch (Throwable t) { // ignore }
                          * 
                          * try { assertNotNull(expected); final Throwable cause =
                          * expected.getCause(); assertNotNull(cause); assertTrue(cause instanceof
                          * FileAlreadyExistsException);
                          * assertTrue(cause.getMessage().contains("Pid file already exists: "));
                          * assertTrue(cause.getMessage().contains("vf.gf.server.pid for process " +
                          * realPid)); } catch (Throwable e) { logger.error(e); if (failure == null)
                          * { failure = e; } }
                          * 
                          * try { delete(this.pidFile); final Status theStatus = status.getStatus();
                          * assertFalse(theStatus == Status.STARTING); assertFalse(theStatus ==
                          * Status.ONLINE); } catch (Throwable e) { logger.error(e); if (failure ==
                          * null) { failure = e; } }
                          * 
                          * if (failure != null) { throw failure; } } //
                          * testStartWithExistingPidFileFails
                          */
/**
   * Confirms fix for #47665.
   */
@Test
public void testStartUsingServerPortInUseFails() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // generate one free port and then use TEST_OVERRIDE_DEFAULT_PORT_PROPERTY
    final int freeTCPPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(freeTCPPort, 50, null, -1);
    // build and start the server
    final Builder builder = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setServerPort(freeTCPPort).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    this.launcher = builder.build();
    RuntimeException expected = null;
    try {
        this.launcher.start();
        fail("ServerLauncher start should have thrown RuntimeException caused by BindException");
    } catch (RuntimeException e) {
        expected = e;
        assertNotNull(expected.getMessage());
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertNotNull(expected);
        final Throwable cause = expected.getCause();
        assertNotNull(cause);
        assertTrue(cause instanceof BindException);
    // BindException string varies by platform
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertFalse("Pid file should not exist: " + this.pidFile, this.pidFile.exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // just in case the launcher started...
    ServerState status = null;
    try {
        status = this.launcher.stop();
    } catch (Throwable t) {
    // ignore
    }
    try {
        waitForFileToDelete(this.pidFile);
        assertEquals(getExpectedStopStatusForNotRunning(), status.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) BindException(java.net.BindException) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 34 with BindException

use of java.net.BindException in project aries by apache.

the class TransactionControlRunningTest method testTwoRequiredsNestedNoRollbackForInnerException.

@Test
public void testTwoRequiredsNestedNoRollbackForInnerException() {
    AtomicReference<TransactionStatus> finalStatusOuter = new AtomicReference<>();
    AtomicReference<TransactionStatus> finalStatusInner = new AtomicReference<>();
    Exception userEx = new BindException("Bang!");
    try {
        txControl.required(() -> {
            assertTrue(txControl.activeTransaction());
            Object key = txControl.getCurrentContext().getTransactionKey();
            txControl.getCurrentContext().postCompletion(finalStatusOuter::set);
            try {
                txControl.build().noRollbackFor(BindException.class).requiresNew(() -> {
                    assertFalse(key.equals(txControl.getCurrentContext().getTransactionKey()));
                    txControl.getCurrentContext().postCompletion(finalStatusInner::set);
                    throw userEx;
                });
                fail("Should not be reached!");
            } catch (ScopedWorkException swe) {
                throw swe.as(BindException.class);
            }
            return null;
        });
        fail("Should not be reached!");
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(ROLLED_BACK, finalStatusOuter.get());
    assertEquals(COMMITTED, finalStatusInner.get());
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) BindException(java.net.BindException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Example 35 with BindException

use of java.net.BindException in project aries by apache.

the class TransactionControlRunningTest method testRequiredNoRollbackException.

@Test
public void testRequiredNoRollbackException() {
    AtomicReference<TransactionStatus> finalStatus = new AtomicReference<>();
    Exception userEx = new BindException("Bang!");
    try {
        txControl.build().noRollbackFor(BindException.class).required(() -> {
            assertTrue(txControl.activeTransaction());
            txControl.getCurrentContext().postCompletion(finalStatus::set);
            throw userEx;
        });
        fail("Should not be reached");
    } catch (ScopedWorkException swe) {
        assertSame(userEx, swe.getCause());
    }
    assertEquals(COMMITTED, finalStatus.get());
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionStatus(org.osgi.service.transaction.control.TransactionStatus) BindException(java.net.BindException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) BindException(java.net.BindException) Test(org.junit.Test)

Aggregations

BindException (java.net.BindException)103 IOException (java.io.IOException)34 InetSocketAddress (java.net.InetSocketAddress)25 ServerSocket (java.net.ServerSocket)22 Test (org.junit.Test)22 File (java.io.File)12 SocketException (java.net.SocketException)10 Configuration (org.apache.hadoop.conf.Configuration)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 InterruptedIOException (java.io.InterruptedIOException)5 MiniDFSNNTopology (org.apache.hadoop.hdfs.MiniDFSNNTopology)5 InetAddress (java.net.InetAddress)4 UnknownHostException (java.net.UnknownHostException)4 RemoteException (java.rmi.RemoteException)4 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)4 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)4 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)3 Socket (java.net.Socket)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3