Search in sources :

Example 6 with Builder

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

the class ServerLauncherLocalIntegrationTest method testBuilderSetProperties.

@Test
public void testBuilderSetProperties() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    this.launcher = new Builder().setDisableDefaultServer(true).setForce(true).setMemberName(getUniqueName()).setWorkingDirectory(rootFolder).set(DISABLE_AUTO_RECONNECT, "true").set(LOG_LEVEL, "config").set(MCAST_PORT, "0").build();
    assertNotNull(this.launcher);
    try {
        assertEquals(Status.ONLINE, this.launcher.start().getStatus());
        waitForServerToStart(this.launcher);
        final Cache cache = this.launcher.getCache();
        assertNotNull(cache);
        final DistributedSystem distributedSystem = cache.getDistributedSystem();
        assertNotNull(distributedSystem);
        assertEquals("true", distributedSystem.getProperties().getProperty(DISABLE_AUTO_RECONNECT));
        assertEquals("config", distributedSystem.getProperties().getProperty(LOG_LEVEL));
        assertEquals("0", distributedSystem.getProperties().getProperty(MCAST_PORT));
        assertEquals(getUniqueName(), distributedSystem.getProperties().getProperty(NAME));
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        assertNull(this.launcher.getCache());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 7 with Builder

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

the class ServerLauncherLocalIntegrationTest method testStartUsingServerPortInUseFails.

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

Example 8 with Builder

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

the class ServerLauncherLocalIntegrationTest method testStartUsingDisableDefaultServerLeavesPortFree.

/**
   * Confirms fix for #47778.
   */
@Test
public void testStartUsingDisableDefaultServerLeavesPortFree() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // build and start the server
    assertTrue(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
    // build and start the server
    final Builder builder = new Builder().setDisableDefaultServer(true).setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    this.launcher = builder.build();
    // wait for server to start
    try {
        // if start succeeds without throwing exception then #47778 is fixed
        this.launcher.start();
        waitForServerToStart(this.launcher);
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertTrue(ProcessUtils.isProcessAlive(pid));
        assertEquals(getPid(), pid);
        // verify server did not a port
        assertTrue(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
        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());
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 9 with Builder

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

the class ServerLauncherLocalIntegrationTest method testStartDeletesStaleControlFiles.

@Test
public void testStartDeletesStaleControlFiles() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // create existing control files
    this.stopRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getStopRequestFileName());
    this.stopRequestFile.createNewFile();
    assertTrue(this.stopRequestFile.exists());
    this.statusRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getStatusRequestFileName());
    this.statusRequestFile.createNewFile();
    assertTrue(this.statusRequestFile.exists());
    this.statusFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getStatusFileName());
    this.statusFile.createNewFile();
    assertTrue(this.statusFile.exists());
    // build and start the server
    final Builder builder = new Builder().setDisableDefaultServer(true).setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    assertFalse(builder.getForce());
    this.launcher = builder.build();
    assertFalse(this.launcher.isForcing());
    this.launcher.start();
    try {
        waitForServerToStart(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.SERVER.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(this.stopRequestFile.exists());
        assertFalse(this.statusRequestFile.exists());
        assertFalse(this.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);
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 10 with Builder

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

the class ServerLauncherRemoteFileIntegrationTest method testStopUsingPid.

@Override
@Test
public /**
   * Override because FileProcessController cannot request stop with PID
   */
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(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()).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 server to start
    int pid = 0;
    ServerLauncher pidLauncher = null;
    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());
        // 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 server
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        waitForPidToStop(pid);
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    } finally {
        // TODO: delete
        new File(ProcessType.SERVER.getStopRequestFileName()).delete();
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ArrayList(java.util.ArrayList) Builder(org.apache.geode.distributed.ServerLauncher.Builder) AttachAPINotFoundException(org.apache.geode.lang.AttachAPINotFoundException) ProcessStreamReader(org.apache.geode.internal.process.ProcessStreamReader) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

Builder (org.apache.geode.distributed.ServerLauncher.Builder)56 Test (org.junit.Test)56 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)31 UnitTest (org.apache.geode.test.junit.categories.UnitTest)29 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)27 File (java.io.File)18 Cache (org.apache.geode.cache.Cache)14 Expectations (org.jmock.Expectations)12 ServerState (org.apache.geode.distributed.ServerLauncher.ServerState)11 ArrayList (java.util.ArrayList)4 CacheServer (org.apache.geode.cache.server.CacheServer)4 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)3 FileNotFoundException (java.io.FileNotFoundException)2 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 BindException (java.net.BindException)2 Properties (java.util.Properties)2 CacheCreation (org.apache.geode.internal.cache.xmlcache.CacheCreation)2 RegionAttributesCreation (org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation)2 ProcessStreamReader (org.apache.geode.internal.process.ProcessStreamReader)2