Search in sources :

Example 16 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherRemoteIntegrationTest method testStartUsingDisableDefaultServerSkipsPortCheck.

@Test
public void testStartUsingDisableDefaultServerSkipsPortCheck() throws Throwable {
    // make serverPort in use
    this.socket = SocketCreatorFactory.createNonDefaultInstance(false, false, null, null, System.getProperties()).createServerSocket(this.serverPort, 50, null, -1);
    assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
    // build and start the server
    final List<String> jvmArguments = getJvmArguments();
    jvmArguments.add("-D" + AbstractCacheServer.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY + "=" + this.serverPort);
    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(ServerLauncher.class.getName());
    command.add(ServerLauncher.Command.START.getName());
    command.add(getUniqueName());
    command.add("--disable-default-server");
    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 server to start
    int pid = 0;
    this.launcher = new ServerLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        waitForServerToStart();
        // validate log file was created
        final String logFileName = getUniqueName() + ".log";
        assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
        final ServerState status = this.launcher.status();
        final String portString = status.getPort();
        assertEquals("Port should be \"\" instead of " + portString, "", portString);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the server
    try {
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        waitForPidToStop(pid);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // verify port is still in use
    this.errorCollector.checkThat(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET), is(equalTo(false)));
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) ArrayList(java.util.ArrayList) File(java.io.File) Builder(org.apache.geode.distributed.ServerLauncher.Builder) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 17 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherRemoteIntegrationTest method testStartUsingServerPortUsedInsteadOfDefaultCacheXml.

/**
   * Confirms part of fix for #47664
   */
@Test
public void testStartUsingServerPortUsedInsteadOfDefaultCacheXml() throws Throwable {
    // write out cache.xml with one port
    final CacheCreation creation = new CacheCreation();
    final RegionAttributesCreation attrs = new RegionAttributesCreation(creation);
    attrs.setScope(Scope.DISTRIBUTED_ACK);
    attrs.setDataPolicy(DataPolicy.REPLICATE);
    creation.createRegion(getUniqueName(), attrs);
    creation.addCacheServer();
    File cacheXmlFile = new File(this.temporaryFolder.getRoot(), getUniqueName() + ".xml");
    final PrintWriter pw = new PrintWriter(new FileWriter(cacheXmlFile), true);
    CacheXmlGenerator.generate(creation, pw);
    pw.close();
    // launch server and specify a different port
    final List<String> jvmArguments = getJvmArguments();
    jvmArguments.add("-D" + DistributionConfig.GEMFIRE_PREFIX + "" + CACHE_XML_FILE + "=" + cacheXmlFile.getCanonicalPath());
    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(ServerLauncher.class.getName());
    command.add(ServerLauncher.Command.START.getName());
    command.add(getUniqueName());
    command.add("--redirect-output");
    command.add("--server-port=" + this.serverPort);
    final String expectedString = "java.net.BindException";
    final AtomicBoolean outputContainedExpectedString = new AtomicBoolean();
    this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
    this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createExpectedListener("sysout", getUniqueName() + "#sysout", expectedString, outputContainedExpectedString)).build().start();
    this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createExpectedListener("syserr", getUniqueName() + "#syserr", expectedString, outputContainedExpectedString)).build().start();
    // wait for server to start up
    int pid = 0;
    this.launcher = new ServerLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        waitForServerToStart();
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        pid = readPid(this.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());
        // verify server used --server-port instead of default or port in cache.xml
        assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
        final ServerState status = this.launcher.status();
        final String portString = status.getPort();
        int port = Integer.valueOf(portString);
        assertEquals("Port should be " + this.serverPort + " instead of " + port, this.serverPort, port);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the server
    try {
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        waitForPidToStop(pid);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : FileWriter(java.io.FileWriter) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) ArrayList(java.util.ArrayList) Builder(org.apache.geode.distributed.ServerLauncher.Builder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProcessStreamReader(org.apache.geode.internal.process.ProcessStreamReader) RegionAttributesCreation(org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) File(java.io.File) PrintWriter(java.io.PrintWriter) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 18 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherRemoteIntegrationTest method testStatusWithEmptyPidFile.

@Test
public void testStatusWithEmptyPidFile() throws Exception {
    this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
    assertTrue(this.pidFile + " already exists", this.pidFile.createNewFile());
    final ServerLauncher dirLauncher = new ServerLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    final ServerState actualStatus = dirLauncher.status();
    assertThat(actualStatus, is(notNullValue()));
    assertThat(actualStatus.getStatus(), is(equalTo(Status.NOT_RESPONDING)));
    assertThat(actualStatus.getPid(), is(nullValue()));
    assertThat(actualStatus.getUptime().intValue(), is(equalTo(0)));
    assertThat(actualStatus.getWorkingDirectory(), is(equalTo(this.temporaryFolder.getRoot().getCanonicalPath())));
    assertThat(actualStatus.getClasspath(), is(nullValue()));
    assertThat(actualStatus.getGemFireVersion(), is(equalTo(GemFireVersion.getGemFireVersion())));
    assertThat(actualStatus.getJavaVersion(), is(nullValue()));
    assertThat(actualStatus.getLogFile(), is(nullValue()));
    assertThat(actualStatus.getHost(), is(nullValue()));
    assertThat(actualStatus.getMemberName(), is(nullValue()));
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) File(java.io.File) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 19 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherRemoteIntegrationTest method testStartWithDefaultPortInUseFails.

// GEODE-1135: random ports, BindException, fork JVM
@Category(FlakyTest.class)
@Test
public void testStartWithDefaultPortInUseFails() throws Throwable {
    String expectedString = "java.net.BindException";
    AtomicBoolean outputContainedExpectedString = new AtomicBoolean();
    // make serverPort in use
    this.socket = SocketCreatorFactory.createNonDefaultInstance(false, false, null, null, System.getProperties()).createServerSocket(this.serverPort, 50, null, -1);
    assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
    // launch server
    final List<String> jvmArguments = getJvmArguments();
    jvmArguments.add("-D" + AbstractCacheServer.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY + "=" + this.serverPort);
    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(ServerLauncher.class.getName());
    command.add(ServerLauncher.Command.START.getName());
    command.add(getUniqueName());
    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(createExpectedListener("sysout", getUniqueName() + "#sysout", expectedString, outputContainedExpectedString)).build().start();
    this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createExpectedListener("syserr", getUniqueName() + "#syserr", expectedString, outputContainedExpectedString)).build().start();
    // wait for server to start up
    final ServerLauncher dirLauncher = new ServerLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        int code = this.process.waitFor();
        assertEquals("Expected exit code 1 but was " + code, 1, code);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        // check the status
        final ServerState serverState = dirLauncher.status();
        assertNotNull(serverState);
        assertEquals(Status.NOT_RESPONDING, serverState.getStatus());
        // creation of log file seems to be random -- look into why sometime
        final String logFileName = getUniqueName() + ".log";
        assertFalse("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // if the following fails, then the SHORTER_TIMEOUT might be too short for slow machines
    // or this test needs to use MainLauncher in ProcessWrapper
    // validate that output contained BindException
    this.errorCollector.checkThat(outputContainedExpectedString.get(), is(equalTo(true)));
    // just in case the launcher started...
    ServerState status = null;
    try {
        status = dirLauncher.stop();
    } catch (Throwable t) {
    // ignore
    }
    try {
        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) ArrayList(java.util.ArrayList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProcessStreamReader(org.apache.geode.internal.process.ProcessStreamReader) File(java.io.File) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 20 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherLocalIntegrationTest method testStartWithDefaultPortInUseFails.

@Test
public void testStartWithDefaultPortInUseFails() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // generate one free port and then use TEST_OVERRIDE_DEFAULT_PORT_PROPERTY
    this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(this.serverPort, 50, null, -1);
    assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
    // build and start the server
    final Builder builder = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    this.launcher = builder.build();
    RuntimeException expected = null;
    try {
        this.launcher.start();
        // why did it not fail like it's supposed to?
        final String property = System.getProperty(AbstractCacheServer.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY);
        assertNotNull(property);
        assertEquals(this.serverPort, Integer.valueOf(property).intValue());
        assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
        fail("Server port is " + this.launcher.getCache().getCacheServers().get(0).getPort());
        fail("ServerLauncher 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.SERVER.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...
    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)

Aggregations

ServerState (org.apache.geode.distributed.ServerLauncher.ServerState)25 File (java.io.File)24 Test (org.junit.Test)24 Builder (org.apache.geode.distributed.ServerLauncher.Builder)23 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)23 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)14 ArrayList (java.util.ArrayList)10 ProcessStreamReader (org.apache.geode.internal.process.ProcessStreamReader)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 FileWriter (java.io.FileWriter)3 PrintWriter (java.io.PrintWriter)3 CacheCreation (org.apache.geode.internal.cache.xmlcache.CacheCreation)3 RegionAttributesCreation (org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation)3 BindException (java.net.BindException)2 ServerLauncher (org.apache.geode.distributed.ServerLauncher)2 CommandStringBuilder (org.apache.geode.management.internal.cli.util.CommandStringBuilder)2 Category (org.junit.experimental.categories.Category)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1 ClientCache (org.apache.geode.cache.client.ClientCache)1