http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/566fce96/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalIntegrationTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalIntegrationTest.java new file mode 100755 index 0000000..252b5aa --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalIntegrationTest.java @@ -0,0 +1,827 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.distributed; + +import static org.junit.Assert.*; + +import java.io.File; +import java.lang.management.ManagementFactory; +import java.net.BindException; +import java.net.InetAddress; + +import org.junit.After; +import org.junit.Before; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import com.gemstone.gemfire.distributed.AbstractLauncher.Status; +import com.gemstone.gemfire.distributed.LocatorLauncher.Builder; +import com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState; +import com.gemstone.gemfire.distributed.internal.DistributionConfig; +import com.gemstone.gemfire.distributed.internal.InternalLocator; +import com.gemstone.gemfire.internal.AvailablePort; +import com.gemstone.gemfire.internal.AvailablePortHelper; +import com.gemstone.gemfire.internal.DistributionLocator; +import com.gemstone.gemfire.internal.GemFireVersion; +import com.gemstone.gemfire.internal.SocketCreator; +import com.gemstone.gemfire.internal.process.ProcessControllerFactory; +import com.gemstone.gemfire.internal.process.ProcessType; +import com.gemstone.gemfire.internal.process.ProcessUtils; +import com.gemstone.gemfire.test.junit.categories.IntegrationTest; + +/** + * Tests usage of LocatorLauncher as a local API in existing JVM. + * + * @since 8.0 + */ +@Category(IntegrationTest.class) +public class LocatorLauncherLocalIntegrationTest extends AbstractLocatorLauncherIntegrationTestCase { + + @Before + public final void setUpLocatorLauncherLocalIntegrationTest() throws Exception { + disconnectFromDS(); + System.setProperty(ProcessType.TEST_PREFIX_PROPERTY, getUniqueName()+"-"); + } + + @After + public final void tearDownLocatorLauncherLocalIntegrationTest() throws Exception { + disconnectFromDS(); + } + + @Test + public void testBuilderSetProperties() throws Throwable { + this.launcher = new Builder() + .setForce(true) + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.DISABLE_AUTO_RECONNECT_NAME, "true") + .set(DistributionConfig.LOG_LEVEL_NAME, "config") + .set(DistributionConfig.MCAST_PORT_NAME, "0") + .build(); + + try { + assertEquals(Status.ONLINE, this.launcher.start().getStatus()); + waitForLocatorToStart(this.launcher, true); + + final InternalLocator locator = this.launcher.getLocator(); + assertNotNull(locator); + + final DistributedSystem distributedSystem = locator.getDistributedSystem(); + + assertNotNull(distributedSystem); + assertEquals("true", distributedSystem.getProperties().getProperty(DistributionConfig.DISABLE_AUTO_RECONNECT_NAME)); + assertEquals("0", distributedSystem.getProperties().getProperty(DistributionConfig.MCAST_PORT_NAME)); + assertEquals("config", distributedSystem.getProperties().getProperty(DistributionConfig.LOG_LEVEL_NAME)); + assertEquals(getUniqueName(), distributedSystem.getProperties().getProperty(DistributionConfig.NAME_NAME)); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); + assertNull(this.launcher.getLocator()); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + + @Test + public void testIsAttachAPIFound() throws Exception { + final ProcessControllerFactory factory = new ProcessControllerFactory(); + assertTrue(factory.isAttachAPIFound()); + } + + @Test + public void testStartCreatesPidFile() throws Throwable { + this.launcher = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config") + .build(); + + try { + this.launcher.start(); + waitForLocatorToStart(this.launcher); + assertEquals(Status.ONLINE, this.launcher.status().getStatus()); + + // validate the pid file and its contents + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue(this.pidFile.exists()); + final int pid = readPid(this.pidFile); + assertTrue(pid > 0); + assertTrue(ProcessUtils.isProcessAlive(pid)); + assertEquals(getPid(), pid); + + assertEquals(Status.ONLINE, this.launcher.status().getStatus()); + + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + + @Test + public void testStartDeletesStaleControlFiles() throws Throwable { + // create existing control files + this.stopRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStopRequestFileName()); + this.stopRequestFile.createNewFile(); + assertTrue(this.stopRequestFile.exists()); + + this.statusRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusRequestFileName()); + this.statusRequestFile.createNewFile(); + assertTrue(this.statusRequestFile.exists()); + + this.statusFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusFileName()); + this.statusFile.createNewFile(); + assertTrue(this.statusFile.exists()); + + // build and start the locator + final Builder builder = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + assertFalse(builder.getForce()); + this.launcher = builder.build(); + assertFalse(this.launcher.isForcing()); + this.launcher.start(); + + try { + waitForLocatorToStart(this.launcher); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + // validate the pid file and its contents + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue(this.pidFile.exists()); + final int pid = readPid(this.pidFile); + assertTrue(pid > 0); + assertTrue(ProcessUtils.isProcessAlive(pid)); + assertEquals(getPid(), pid); + + // validate stale control files were deleted + assertFalse(stopRequestFile.exists()); + assertFalse(statusRequestFile.exists()); + assertFalse(statusFile.exists()); + + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + + @Test + public void testStartOverwritesStalePidFile() throws Throwable { + // create existing pid file + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertFalse("Integer.MAX_VALUE shouldn't be the same as local pid " + Integer.MAX_VALUE, Integer.MAX_VALUE == ProcessUtils.identifyPid()); + writePid(this.pidFile, Integer.MAX_VALUE); + + // build and start the locator + final Builder builder = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + assertFalse(builder.getForce()); + this.launcher = builder.build(); + assertFalse(this.launcher.isForcing()); + this.launcher.start(); + + try { + waitForLocatorToStart(this.launcher); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + // validate the pid file and its contents + assertTrue(this.pidFile.exists()); + final int pid = readPid(this.pidFile); + assertTrue(pid > 0); + assertTrue(ProcessUtils.isProcessAlive(pid)); + assertEquals(getPid(), pid); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + + @Test + @Ignore("Need to rewrite this without using dunit.Host") + public void testStartUsingForceOverwritesExistingPidFile() throws Throwable { + }/* + 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) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + 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)); + assertEquals(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 { + assertEquals(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 { + this.socket = SocketCreator.getDefaultInstance().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(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "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); + } + } + + @Test + @Ignore("Need to rewrite this without using dunit.Host") + public void testStartWithExistingPidFileFails() throws Throwable { + }/* + // 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.LOCATOR.getPidFileName()); + writePid(this.pidFile, realPid); + + // build and start the locator + final Builder builder = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + 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("LocatorLauncher start should have thrown RuntimeException caused by FileAlreadyExistsException"); + } catch (RuntimeException e) { + expected = e; + assertNotNull(expected.getMessage()); + assertTrue(expected.getMessage(), expected.getMessage().contains("A PID file already exists and a Locator may be running in")); + assertEquals(RuntimeException.class, expected.getClass()); + } catch (Throwable e) { + logger.error(e); + if (failure == null) { + failure = e; + } + } + + // just in case the launcher started... + LocatorState 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.locator.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 + */ + + @Test + public void testStartUsingPort() throws Throwable { + // generate one free port and then use it instead of default + final int freeTCPPort = AvailablePortHelper.getRandomAvailableTCPPort(); + assertTrue(AvailablePort.isPortAvailable(freeTCPPort, AvailablePort.SOCKET)); + + this.launcher = new Builder() + .setMemberName(getUniqueName()) + .setPort(freeTCPPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config") + .build(); + + int pid = 0; + try { + // if start succeeds without throwing exception then #47778 is fixed + this.launcher.start(); + waitForLocatorToStart(this.launcher); + + // validate the pid file and its contents + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue(pidFile.exists()); + pid = readPid(pidFile); + assertTrue(pid > 0); + assertTrue(ProcessUtils.isProcessAlive(pid)); + assertEquals(getPid(), pid); + + // verify locator did not use default port + assertTrue(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET)); + + final LocatorState status = this.launcher.status(); + final String portString = status.getPort(); + assertEquals("Port should be \"" + freeTCPPort + "\" instead of " + portString, String.valueOf(freeTCPPort), portString); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + // stop the locator + try { + assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + + @Test + public void testStartUsingPortInUseFails() throws Throwable { + // generate one free port and then use it instead of default + final int freeTCPPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); + this.socket = SocketCreator.getDefaultInstance().createServerSocket(freeTCPPort, 50, null, -1); + + this.launcher = new Builder() + .setMemberName(getUniqueName()) + .setPort(freeTCPPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "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); + } + } + + @Test + public void testStatusUsingPid() throws Throwable { + // build and start the locator + final Builder builder = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + assertFalse(builder.getForce()); + this.launcher = builder.build(); + assertFalse(this.launcher.isForcing()); + + LocatorLauncher pidLauncher = null; + try { + this.launcher.start(); + waitForLocatorToStart(this.launcher); + + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists()); + final int pid = readPid(this.pidFile); + assertTrue(pid > 0); + assertEquals(ProcessUtils.identifyPid(), pid); + + pidLauncher = new Builder().setPid(pid).build(); + assertNotNull(pidLauncher); + assertFalse(pidLauncher.isRunning()); + + final LocatorState actualStatus = pidLauncher.status(); + assertNotNull(actualStatus); + assertEquals(Status.ONLINE, actualStatus.getStatus()); + assertEquals(pid, actualStatus.getPid().intValue()); + assertTrue(actualStatus.getUptime() > 0); + // getWorkingDirectory returns user.dir instead of rootFolder because test is starting Locator in this process (to move logFile and pidFile into temp dir) + assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath()); + assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion()); + assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion()); + assertEquals(this.workingDirectory + File.separator + getUniqueName() + ".log", actualStatus.getLogFile()); + assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost()); + assertEquals(getUniqueName(), actualStatus.getMemberName()); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + if (pidLauncher == null) { + try { + assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + } else { + try { + assertEquals(Status.STOPPED, pidLauncher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + } + + @Test + public void testStatusUsingWorkingDirectory() throws Throwable { + final Builder builder = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + assertFalse(builder.getForce()); + this.launcher = builder.build(); + assertFalse(this.launcher.isForcing()); + + LocatorLauncher dirLauncher = null; + try { + this.launcher.start(); + waitForLocatorToStart(this.launcher); + + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists()); + final int pid = readPid(this.pidFile); + assertTrue(pid > 0); + assertEquals(ProcessUtils.identifyPid(), pid); + + dirLauncher = new Builder().setWorkingDirectory(this.workingDirectory).build(); + assertNotNull(dirLauncher); + assertFalse(dirLauncher.isRunning()); + + final LocatorState actualStatus = dirLauncher.status(); + assertNotNull(actualStatus); + assertEquals(Status.ONLINE, actualStatus.getStatus()); + assertEquals(pid, actualStatus.getPid().intValue()); + assertTrue(actualStatus.getUptime() > 0); + // getWorkingDirectory returns user.dir instead of rootFolder because test is starting Locator in this process (to move logFile and pidFile into temp dir) + assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath()); + assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion()); + assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion()); + assertEquals(this.workingDirectory + File.separator + getUniqueName() + ".log", actualStatus.getLogFile()); + assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost()); + assertEquals(getUniqueName(), actualStatus.getMemberName()); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + if (dirLauncher == null) { + try { + assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + } else { + try { + assertEquals(Status.STOPPED, dirLauncher.stop().getStatus()); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + } + + @Test + public void testStopUsingPid() throws Throwable { + final Builder builder = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + assertFalse(builder.getForce()); + this.launcher = builder.build(); + assertFalse(this.launcher.isForcing()); + + LocatorLauncher pidLauncher = null; + try { + this.launcher.start(); + waitForLocatorToStart(this.launcher); + + // validate the pid file and its contents + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue(this.pidFile.exists()); + final int pid = readPid(this.pidFile); + assertTrue(pid > 0); + assertEquals(ProcessUtils.identifyPid(), pid); + + pidLauncher = new Builder().setPid(pid).build(); + assertNotNull(pidLauncher); + assertFalse(pidLauncher.isRunning()); + + // stop the locator + final LocatorState locatorState = pidLauncher.stop(); + assertNotNull(locatorState); + assertEquals(Status.STOPPED, locatorState.getStatus()); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + this.launcher.stop(); + } catch (Throwable e) { + // ignore + } + + try { + // verify the PID file was deleted + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + + @Test + public void testStopUsingWorkingDirectory() throws Throwable { + final Builder builder = new Builder() + .setMemberName(getUniqueName()) + .setPort(this.locatorPort) + .setRedirectOutput(true) + .setWorkingDirectory(this.workingDirectory) + .set(DistributionConfig.CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory) + .set(DistributionConfig.LOG_LEVEL_NAME, "config"); + + assertFalse(builder.getForce()); + this.launcher = builder.build(); + assertFalse(this.launcher.isForcing()); + + LocatorLauncher dirLauncher = null; + try { + this.launcher.start(); + waitForLocatorToStart(this.launcher); + + // validate the pid file and its contents + this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists()); + final int pid = readPid(this.pidFile); + assertTrue(pid > 0); + assertEquals(ProcessUtils.identifyPid(), pid); + + dirLauncher = new Builder().setWorkingDirectory(this.workingDirectory).build(); + assertNotNull(dirLauncher); + assertFalse(dirLauncher.isRunning()); + + // stop the locator + final LocatorState locatorState = dirLauncher.stop(); + assertNotNull(locatorState); + assertEquals(Status.STOPPED, locatorState.getStatus()); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + this.launcher.stop(); + } catch (Throwable e) { + // ignore + } + + try { + // verify the PID file was deleted + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } +}
http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/566fce96/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalJUnitTest.java deleted file mode 100755 index bfed4d0..0000000 --- a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherLocalJUnitTest.java +++ /dev/null @@ -1,842 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.gemstone.gemfire.distributed; - -import static org.junit.Assert.*; - -import java.io.File; -import java.lang.management.ManagementFactory; -import java.net.BindException; -import java.net.InetAddress; - -import org.junit.After; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import com.gemstone.gemfire.distributed.AbstractLauncher.Status; -import com.gemstone.gemfire.distributed.LocatorLauncher.Builder; -import com.gemstone.gemfire.distributed.LocatorLauncher.LocatorState; -import com.gemstone.gemfire.distributed.internal.DistributionConfig; -import com.gemstone.gemfire.distributed.internal.InternalLocator; -import com.gemstone.gemfire.internal.AvailablePort; -import com.gemstone.gemfire.internal.AvailablePortHelper; -import com.gemstone.gemfire.internal.DistributionLocator; -import com.gemstone.gemfire.internal.GemFireVersion; -import com.gemstone.gemfire.internal.SocketCreator; -import com.gemstone.gemfire.internal.process.ProcessControllerFactory; -import com.gemstone.gemfire.internal.process.ProcessType; -import com.gemstone.gemfire.internal.process.ProcessUtils; -import com.gemstone.gemfire.test.junit.categories.IntegrationTest; - -/** - * Tests usage of LocatorLauncher as a local API in existing JVM. - * - * @since 8.0 - */ -@Category(IntegrationTest.class) -public class LocatorLauncherLocalJUnitTest extends AbstractLocatorLauncherJUnitTestCase { - - @Before - public final void setUpLocatorLauncherLocalTest() throws Exception { - disconnectFromDS(); - System.setProperty(ProcessType.TEST_PREFIX_PROPERTY, getUniqueName()+"-"); - } - - @After - public final void tearDownLocatorLauncherLocalTest() throws Exception { - disconnectFromDS(); - } - - protected Status getExpectedStopStatusForNotRunning() { - return Status.NOT_RESPONDING; - } - - @Test - public void testBuilderSetProperties() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - this.launcher = new Builder() - .setForce(true) - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.DISABLE_AUTO_RECONNECT_NAME, "true") - .set(DistributionConfig.LOG_LEVEL_NAME, "config") - .set(DistributionConfig.MCAST_PORT_NAME, "0") - .build(); - - try { - assertEquals(Status.ONLINE, this.launcher.start().getStatus()); - waitForLocatorToStart(this.launcher, true); - - final InternalLocator locator = this.launcher.getLocator(); - assertNotNull(locator); - - final DistributedSystem distributedSystem = locator.getDistributedSystem(); - - assertNotNull(distributedSystem); - assertEquals("true", distributedSystem.getProperties().getProperty(DistributionConfig.DISABLE_AUTO_RECONNECT_NAME)); - assertEquals("0", distributedSystem.getProperties().getProperty(DistributionConfig.MCAST_PORT_NAME)); - assertEquals("config", distributedSystem.getProperties().getProperty(DistributionConfig.LOG_LEVEL_NAME)); - assertEquals(getUniqueName(), distributedSystem.getProperties().getProperty(DistributionConfig.NAME_NAME)); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - assertNull(this.launcher.getLocator()); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - - @Test - public void testIsAttachAPIFound() throws Exception { - final ProcessControllerFactory factory = new ProcessControllerFactory(); - assertTrue(factory.isAttachAPIFound()); - } - - @Test - public void testStartCreatesPidFile() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - this.launcher = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config") - .build(); - - try { - this.launcher.start(); - waitForLocatorToStart(this.launcher); - assertEquals(Status.ONLINE, this.launcher.status().getStatus()); - - // validate the pid file and its contents - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue(this.pidFile.exists()); - final int pid = readPid(this.pidFile); - assertTrue(pid > 0); - assertTrue(ProcessUtils.isProcessAlive(pid)); - assertEquals(getPid(), pid); - - assertEquals(Status.ONLINE, this.launcher.status().getStatus()); - - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - - @Test - public void testStartDeletesStaleControlFiles() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - // create existing control files - this.stopRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStopRequestFileName()); - this.stopRequestFile.createNewFile(); - assertTrue(this.stopRequestFile.exists()); - - this.statusRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusRequestFileName()); - this.statusRequestFile.createNewFile(); - assertTrue(this.statusRequestFile.exists()); - - this.statusFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusFileName()); - this.statusFile.createNewFile(); - assertTrue(this.statusFile.exists()); - - // build and start the locator - final Builder builder = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - assertFalse(builder.getForce()); - this.launcher = builder.build(); - assertFalse(this.launcher.isForcing()); - this.launcher.start(); - - try { - waitForLocatorToStart(this.launcher); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - // validate the pid file and its contents - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue(this.pidFile.exists()); - final int pid = readPid(this.pidFile); - assertTrue(pid > 0); - assertTrue(ProcessUtils.isProcessAlive(pid)); - assertEquals(getPid(), pid); - - // validate stale control files were deleted - assertFalse(stopRequestFile.exists()); - assertFalse(statusRequestFile.exists()); - assertFalse(statusFile.exists()); - - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - - @Test - public void testStartOverwritesStalePidFile() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - // create existing pid file - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertFalse("Integer.MAX_VALUE shouldn't be the same as local pid " + Integer.MAX_VALUE, Integer.MAX_VALUE == ProcessUtils.identifyPid()); - writePid(this.pidFile, Integer.MAX_VALUE); - - // build and start the locator - final Builder builder = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - assertFalse(builder.getForce()); - this.launcher = builder.build(); - assertFalse(this.launcher.isForcing()); - this.launcher.start(); - - try { - waitForLocatorToStart(this.launcher); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - // validate the pid file and its contents - assertTrue(this.pidFile.exists()); - final int pid = readPid(this.pidFile); - assertTrue(pid > 0); - assertTrue(ProcessUtils.isProcessAlive(pid)); - assertEquals(getPid(), pid); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - - @Test - @Ignore("Need to rewrite this without using dunit.Host") - public void testStartUsingForceOverwritesExistingPidFile() throws Throwable { - }/* - 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) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - 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)); - assertEquals(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 { - assertEquals(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 { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - this.socket = SocketCreator.getDefaultInstance().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(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "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); - } - } - - @Test - @Ignore("Need to rewrite this without using dunit.Host") - public void testStartWithExistingPidFileFails() throws Throwable { - }/* - // 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.LOCATOR.getPidFileName()); - writePid(this.pidFile, realPid); - - // build and start the locator - final Builder builder = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - 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("LocatorLauncher start should have thrown RuntimeException caused by FileAlreadyExistsException"); - } catch (RuntimeException e) { - expected = e; - assertNotNull(expected.getMessage()); - assertTrue(expected.getMessage(), expected.getMessage().contains("A PID file already exists and a Locator may be running in")); - assertEquals(RuntimeException.class, expected.getClass()); - } catch (Throwable e) { - logger.error(e); - if (failure == null) { - failure = e; - } - } - - // just in case the launcher started... - LocatorState 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.locator.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 - */ - - @Test - public void testStartUsingPort() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - // generate one free port and then use it instead of default - final int freeTCPPort = AvailablePortHelper.getRandomAvailableTCPPort(); - assertTrue(AvailablePort.isPortAvailable(freeTCPPort, AvailablePort.SOCKET)); - - this.launcher = new Builder() - .setMemberName(getUniqueName()) - .setPort(freeTCPPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config") - .build(); - - int pid = 0; - try { - // if start succeeds without throwing exception then #47778 is fixed - this.launcher.start(); - waitForLocatorToStart(this.launcher); - - // validate the pid file and its contents - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue(pidFile.exists()); - pid = readPid(pidFile); - assertTrue(pid > 0); - assertTrue(ProcessUtils.isProcessAlive(pid)); - assertEquals(getPid(), pid); - - // verify locator did not use default port - assertTrue(AvailablePort.isPortAvailable(this.locatorPort, AvailablePort.SOCKET)); - - final LocatorState status = this.launcher.status(); - final String portString = status.getPort(); - assertEquals("Port should be \"" + freeTCPPort + "\" instead of " + portString, String.valueOf(freeTCPPort), portString); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - // stop the locator - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - - @Test - public void testStartUsingPortInUseFails() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - // generate one free port and then use it instead of default - final int freeTCPPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); - this.socket = SocketCreator.getDefaultInstance().createServerSocket(freeTCPPort, 50, null, -1); - - this.launcher = new Builder() - .setMemberName(getUniqueName()) - .setPort(freeTCPPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "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); - } - } - - @Test - public void testStatusUsingPid() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - // build and start the locator - final Builder builder = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - assertFalse(builder.getForce()); - this.launcher = builder.build(); - assertFalse(this.launcher.isForcing()); - - LocatorLauncher pidLauncher = null; - try { - this.launcher.start(); - waitForLocatorToStart(this.launcher); - - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists()); - final int pid = readPid(this.pidFile); - assertTrue(pid > 0); - assertEquals(ProcessUtils.identifyPid(), pid); - - pidLauncher = new Builder().setPid(pid).build(); - assertNotNull(pidLauncher); - assertFalse(pidLauncher.isRunning()); - - final LocatorState actualStatus = pidLauncher.status(); - assertNotNull(actualStatus); - assertEquals(Status.ONLINE, actualStatus.getStatus()); - assertEquals(pid, actualStatus.getPid().intValue()); - assertTrue(actualStatus.getUptime() > 0); - // getWorkingDirectory returns user.dir instead of rootFolder because test is starting Locator in this process (to move logFile and pidFile into temp dir) - assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath()); - assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion()); - assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion()); - assertEquals(rootFolder + File.separator + getUniqueName() + ".log", actualStatus.getLogFile()); - assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost()); - assertEquals(getUniqueName(), actualStatus.getMemberName()); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - if (pidLauncher == null) { - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - } else { - try { - assertEquals(Status.STOPPED, pidLauncher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - } - - @Test - public void testStatusUsingWorkingDirectory() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - final Builder builder = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - assertFalse(builder.getForce()); - this.launcher = builder.build(); - assertFalse(this.launcher.isForcing()); - - LocatorLauncher dirLauncher = null; - try { - this.launcher.start(); - waitForLocatorToStart(this.launcher); - - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists()); - final int pid = readPid(this.pidFile); - assertTrue(pid > 0); - assertEquals(ProcessUtils.identifyPid(), pid); - - dirLauncher = new Builder().setWorkingDirectory(rootFolder).build(); - assertNotNull(dirLauncher); - assertFalse(dirLauncher.isRunning()); - - final LocatorState actualStatus = dirLauncher.status(); - assertNotNull(actualStatus); - assertEquals(Status.ONLINE, actualStatus.getStatus()); - assertEquals(pid, actualStatus.getPid().intValue()); - assertTrue(actualStatus.getUptime() > 0); - // getWorkingDirectory returns user.dir instead of rootFolder because test is starting Locator in this process (to move logFile and pidFile into temp dir) - assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath()); - assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion()); - assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion()); - assertEquals(rootFolder + File.separator + getUniqueName() + ".log", actualStatus.getLogFile()); - assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost()); - assertEquals(getUniqueName(), actualStatus.getMemberName()); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - if (dirLauncher == null) { - try { - assertEquals(Status.STOPPED, this.launcher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - } else { - try { - assertEquals(Status.STOPPED, dirLauncher.stop().getStatus()); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - } - - @Test - public void testStopUsingPid() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - final Builder builder = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - assertFalse(builder.getForce()); - this.launcher = builder.build(); - assertFalse(this.launcher.isForcing()); - - LocatorLauncher pidLauncher = null; - try { - this.launcher.start(); - waitForLocatorToStart(this.launcher); - - // validate the pid file and its contents - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue(this.pidFile.exists()); - final int pid = readPid(this.pidFile); - assertTrue(pid > 0); - assertEquals(ProcessUtils.identifyPid(), pid); - - pidLauncher = new Builder().setPid(pid).build(); - assertNotNull(pidLauncher); - assertFalse(pidLauncher.isRunning()); - - // stop the locator - final LocatorState locatorState = pidLauncher.stop(); - assertNotNull(locatorState); - assertEquals(Status.STOPPED, locatorState.getStatus()); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - this.launcher.stop(); - } catch (Throwable e) { - // ignore - } - - try { - // verify the PID file was deleted - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - - @Test - public void testStopUsingWorkingDirectory() throws Throwable { - String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath(); - - final Builder builder = new Builder() - .setMemberName(getUniqueName()) - .setPort(this.locatorPort) - .setRedirectOutput(true) - .setWorkingDirectory(rootFolder) - .set(DistributionConfig.LOG_LEVEL_NAME, "config"); - - assertFalse(builder.getForce()); - this.launcher = builder.build(); - assertFalse(this.launcher.isForcing()); - - LocatorLauncher dirLauncher = null; - try { - this.launcher.start(); - waitForLocatorToStart(this.launcher); - - // validate the pid file and its contents - this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists()); - final int pid = readPid(this.pidFile); - assertTrue(pid > 0); - assertEquals(ProcessUtils.identifyPid(), pid); - - dirLauncher = new Builder().setWorkingDirectory(rootFolder).build(); - assertNotNull(dirLauncher); - assertFalse(dirLauncher.isRunning()); - - // stop the locator - final LocatorState locatorState = dirLauncher.stop(); - assertNotNull(locatorState); - assertEquals(Status.STOPPED, locatorState.getStatus()); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - this.launcher.stop(); - } catch (Throwable e) { - // ignore - } - - try { - // verify the PID file was deleted - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/566fce96/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileIntegrationTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileIntegrationTest.java new file mode 100755 index 0000000..63f7312 --- /dev/null +++ b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileIntegrationTest.java @@ -0,0 +1,218 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gemstone.gemfire.distributed; + +import static org.junit.Assert.*; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import com.gemstone.gemfire.distributed.AbstractLauncher.Status; +import com.gemstone.gemfire.distributed.LocatorLauncher.Builder; +import com.gemstone.gemfire.internal.process.ProcessControllerFactory; +import com.gemstone.gemfire.internal.process.ProcessStreamReader; +import com.gemstone.gemfire.internal.process.ProcessType; +import com.gemstone.gemfire.internal.process.ProcessUtils; +import com.gemstone.gemfire.lang.AttachAPINotFoundException; +import com.gemstone.gemfire.test.junit.categories.IntegrationTest; + +/** + * Subclass of LocatorLauncherRemoteDUnitTest which forces the code to not find + * the Attach API which is in the JDK tools.jar. As a result LocatorLauncher + * ends up using the FileProcessController implementation. + * + * @since 8.0 + */ +@Category(IntegrationTest.class) +public class LocatorLauncherRemoteFileIntegrationTest extends LocatorLauncherRemoteIntegrationTest { + + @Before + public final void setUpLocatorLauncherRemoteFileIntegrationTest() throws Exception { + System.setProperty(ProcessControllerFactory.PROPERTY_DISABLE_ATTACH_API, "true"); + } + + @After + public final void tearDownLocatorLauncherRemoteFileIntegrationTest() throws Exception { + } + + /** + * Override and assert Attach API is NOT found + */ + @Override + @Test + public void testIsAttachAPIFound() throws Exception { + final ProcessControllerFactory factory = new ProcessControllerFactory(); + assertFalse(factory.isAttachAPIFound()); + } + + /** + * Override because FileProcessController cannot request status with PID + */ + @Override + @Test + public void testStatusUsingPid() throws Throwable { + final List<String> jvmArguments = getJvmArguments(); + + final List<String> command = new ArrayList<String>(); + command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath()); + for (String jvmArgument : jvmArguments) { + command.add(jvmArgument); + } + command.add("-cp"); + command.add(System.getProperty("java.class.path")); + command.add(LocatorLauncher.class.getName()); + command.add(LocatorLauncher.Command.START.getName()); + command.add(getUniqueName()); + command.add("--port=" + this.locatorPort); + command.add("--redirect-output"); + + this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start(); + this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start(); + this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start(); + + // wait for locator to start + int pid = 0; + LocatorLauncher pidLauncher = null; + final LocatorLauncher dirLauncher = new LocatorLauncher.Builder() + .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()) + .build(); + try { + waitForLocatorToStart(dirLauncher); + + // validate the pid file and its contents + final File pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue(pidFile.exists()); + pid = readPid(pidFile); + assertTrue(pid > 0); + assertTrue(ProcessUtils.isProcessAlive(pid)); + + // validate log file was created + final String logFileName = getUniqueName()+".log"; + assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists()); + + // use launcher with pid + pidLauncher = new Builder() + .setPid(pid) + .build(); + + assertNotNull(pidLauncher); + assertFalse(pidLauncher.isRunning()); + + // status with pid only should throw AttachAPINotFoundException + try { + pidLauncher.status(); + fail("FileProcessController should have thrown AttachAPINotFoundException"); + } catch (AttachAPINotFoundException e) { + // passed + } + + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + // stop the locator + try { + assertEquals(Status.STOPPED, dirLauncher.stop().getStatus()); + waitForPidToStop(pid, true); + waitForFileToDelete(this.pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } + + /** + * Override because FileProcessController cannot request stop with PID + */ + @Override + @Test + public void testStopUsingPid() throws Throwable { + final List<String> jvmArguments = getJvmArguments(); + + final List<String> command = new ArrayList<String>(); + command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath()); + for (String jvmArgument : jvmArguments) { + command.add(jvmArgument); + } + command.add("-cp"); + command.add(System.getProperty("java.class.path")); + command.add(LocatorLauncher.class.getName()); + command.add(LocatorLauncher.Command.START.getName()); + command.add(getUniqueName()); + command.add("--port=" + this.locatorPort); + command.add("--redirect-output"); + + this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start(); + this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createLoggingListener("sysout", getUniqueName() + "#sysout")).build().start(); + this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createLoggingListener("syserr", getUniqueName() + "#syserr")).build().start(); + + // wait for locator to start + int pid = 0; + File pidFile = null; + LocatorLauncher pidLauncher = null; + final LocatorLauncher dirLauncher = new LocatorLauncher.Builder() + .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()) + .build(); + try { + waitForLocatorToStart(dirLauncher); + + // validate the pid file and its contents + pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); + assertTrue(pidFile.exists()); + pid = readPid(pidFile); + assertTrue(pid > 0); + assertTrue(ProcessUtils.isProcessAlive(pid)); + + // validate log file was created + final String logFileName = getUniqueName()+".log"; + assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists()); + + // use launcher with pid + pidLauncher = new Builder() + .setPid(pid) + .build(); + + assertNotNull(pidLauncher); + assertFalse(pidLauncher.isRunning()); + + // stop with pid only should throw AttachAPINotFoundException + try { + pidLauncher.stop(); + fail("FileProcessController should have thrown AttachAPINotFoundException"); + } catch (AttachAPINotFoundException e) { + // passed + } + + } catch (Throwable e) { + this.errorCollector.addError(e); + } + + try { + // stop the locator + assertEquals(Status.STOPPED, dirLauncher.stop().getStatus()); + waitForPidToStop(pid); + waitForFileToDelete(pidFile); + } catch (Throwable e) { + this.errorCollector.addError(e); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-geode/blob/566fce96/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileJUnitTest.java b/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileJUnitTest.java deleted file mode 100755 index d3edcae..0000000 --- a/geode-core/src/test/java/com/gemstone/gemfire/distributed/LocatorLauncherRemoteFileJUnitTest.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.gemstone.gemfire.distributed; - -import static org.junit.Assert.*; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import com.gemstone.gemfire.distributed.AbstractLauncher.Status; -import com.gemstone.gemfire.distributed.LocatorLauncher.Builder; -import com.gemstone.gemfire.internal.process.ProcessControllerFactory; -import com.gemstone.gemfire.internal.process.ProcessStreamReader; -import com.gemstone.gemfire.internal.process.ProcessType; -import com.gemstone.gemfire.internal.process.ProcessUtils; -import com.gemstone.gemfire.lang.AttachAPINotFoundException; -import com.gemstone.gemfire.test.junit.categories.IntegrationTest; - -/** - * Subclass of LocatorLauncherRemoteDUnitTest which forces the code to not find - * the Attach API which is in the JDK tools.jar. As a result LocatorLauncher - * ends up using the FileProcessController implementation. - * - * @since 8.0 - */ -@Category(IntegrationTest.class) -public class LocatorLauncherRemoteFileJUnitTest extends LocatorLauncherRemoteJUnitTest { - - @Before - public final void setUpLocatorLauncherRemoteFileTest() throws Exception { - System.setProperty(ProcessControllerFactory.PROPERTY_DISABLE_ATTACH_API, "true"); - } - - @After - public final void tearDownLocatorLauncherRemoteFileTest() throws Exception { - } - - @Override - @Test - /** - * Override and assert Attach API is NOT found - */ - public void testIsAttachAPIFound() throws Exception { - final ProcessControllerFactory factory = new ProcessControllerFactory(); - assertFalse(factory.isAttachAPIFound()); - } - - @Override - @Test - /** - * Override because FileProcessController cannot request status with PID - */ - public void testStatusUsingPid() throws Throwable { - final List<String> jvmArguments = getJvmArguments(); - - final List<String> command = new ArrayList<String>(); - command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath()); - for (String jvmArgument : jvmArguments) { - command.add(jvmArgument); - } - command.add("-cp"); - command.add(System.getProperty("java.class.path")); - command.add(LocatorLauncher.class.getName()); - command.add(LocatorLauncher.Command.START.getName()); - command.add(getUniqueName()); - command.add("--port=" + this.locatorPort); - command.add("--redirect-output"); - - this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start(); - this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start(); - this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start(); - - // wait for locator to start - int pid = 0; - LocatorLauncher pidLauncher = null; - final LocatorLauncher dirLauncher = new LocatorLauncher.Builder() - .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()) - .build(); - try { - waitForLocatorToStart(dirLauncher); - - // validate the pid file and its contents - final File pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue(pidFile.exists()); - pid = readPid(pidFile); - assertTrue(pid > 0); - assertTrue(ProcessUtils.isProcessAlive(pid)); - - // validate log file was created - final String logFileName = getUniqueName()+".log"; - assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists()); - - // use launcher with pid - pidLauncher = new Builder() - .setPid(pid) - .build(); - - assertNotNull(pidLauncher); - assertFalse(pidLauncher.isRunning()); - - // status with pid only should throw AttachAPINotFoundException - try { - pidLauncher.status(); - fail("FileProcessController should have thrown AttachAPINotFoundException"); - } catch (AttachAPINotFoundException e) { - // passed - } - - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - // stop the locator - try { - assertEquals(Status.STOPPED, dirLauncher.stop().getStatus()); - waitForPidToStop(pid, true); - waitForFileToDelete(this.pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } - - @Override - @Test - /** - * Override because FileProcessController cannot request stop with PID - */ - public void testStopUsingPid() throws Throwable { - final List<String> jvmArguments = getJvmArguments(); - - final List<String> command = new ArrayList<String>(); - command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath()); - for (String jvmArgument : jvmArguments) { - command.add(jvmArgument); - } - command.add("-cp"); - command.add(System.getProperty("java.class.path")); - command.add(LocatorLauncher.class.getName()); - command.add(LocatorLauncher.Command.START.getName()); - command.add(getUniqueName()); - command.add("--port=" + this.locatorPort); - command.add("--redirect-output"); - - this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start(); - this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createLoggingListener("sysout", getUniqueName() + "#sysout")).build().start(); - this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createLoggingListener("syserr", getUniqueName() + "#syserr")).build().start(); - - // wait for locator to start - int pid = 0; - File pidFile = null; - LocatorLauncher pidLauncher = null; - final LocatorLauncher dirLauncher = new LocatorLauncher.Builder() - .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()) - .build(); - try { - waitForLocatorToStart(dirLauncher); - - // validate the pid file and its contents - pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName()); - assertTrue(pidFile.exists()); - pid = readPid(pidFile); - assertTrue(pid > 0); - assertTrue(ProcessUtils.isProcessAlive(pid)); - - // validate log file was created - final String logFileName = getUniqueName()+".log"; - assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists()); - - // use launcher with pid - pidLauncher = new Builder() - .setPid(pid) - .build(); - - assertNotNull(pidLauncher); - assertFalse(pidLauncher.isRunning()); - - // stop with pid only should throw AttachAPINotFoundException - try { - pidLauncher.stop(); - fail("FileProcessController should have thrown AttachAPINotFoundException"); - } catch (AttachAPINotFoundException e) { - // passed - } - - } catch (Throwable e) { - this.errorCollector.addError(e); - } - - try { - // stop the locator - assertEquals(Status.STOPPED, dirLauncher.stop().getStatus()); - waitForPidToStop(pid); - waitForFileToDelete(pidFile); - } catch (Throwable e) { - this.errorCollector.addError(e); - } - } -}