package com.tinkerpop.gremlin.server;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * Starts and stops one instance for all tests that extend from this class.
 *
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public abstract class AbstractGremlinServerPerformanceTest {
    private static final Logger logger = LoggerFactory.getLogger(AbstractGremlinServerPerformanceTest.class);

    private static Thread thread;
    private static String host;
    private static String port;

    @BeforeClass
    public static void setUp() throws Exception {
        final InputStream stream = AbstractGremlinServerPerformanceTest.class.getResourceAsStream("gremlin-server-performance.yaml");
        final Settings settings = Settings.read(stream);
        final CompletableFuture<Void> serverReadyFuture = new CompletableFuture<>();

        thread = new Thread(() -> {
            try {
                new GremlinServer(settings, serverReadyFuture).run();
            } catch (InterruptedException ie) {
                logger.info("Shutting down Gremlin Server");
            } catch (Exception ex) {
                logger.error("Could not start Gremlin Server for performance tests", ex);
            }
        });
        thread.start();

        // make sure gremlin server gets off the ground - longer than 30 seconds means that this didn't work somehow
        try {
            serverReadyFuture.get(30000, TimeUnit.MILLISECONDS);
        } catch (Exception ex) {
            logger.error("Server did not start in the expected time or was otherwise interrupted.", ex);
            return;
        }

        host = System.getProperty("host", "localhost");
        port = System.getProperty("port", "8182");
    }

    @AfterClass
    public static void tearDown() throws Exception {
        stopServer();
    }

    public static void stopServer() throws Exception {
        if (!thread.isInterrupted())
            thread.interrupt();

        while (thread.isAlive()) {
            Thread.sleep(250);
        }
    }

    protected static String getHostPort() {
        return host + ":" + port;
    }

    protected static String getWebSocketBaseUri() {
        return "ws://" + getHostPort() + "/gremlin";
    }
}
@@ -224,16 +224,60 @@
 Future;%0A
+import java.util.concurrent.CountDownLatch;%0A
 import j
@@ -724,16 +724,58 @@
  port;%0A%0A
+    private static CountDownLatch latch;%0A%0A
     @Bef
@@ -1106,24 +1106,63 @@
 uture%3C%3E();%0A%0A
+        latch = new CountDownLatch(1);%0A
         thre
@@ -1185,16 +1185,64 @@
 () -%3E %7B%0A
+            GremlinServer gremlinServer = null;%0A
         
@@ -1266,16 +1266,32 @@
         
+ gremlinServer =
  new Gre
@@ -1333,12 +1333,74 @@
 ure)
-.run
+;%0A                gremlinServer.run();%0A                latch.await
 ();%0A
@@ -1627,32 +1627,299 @@
 ce tests%22, ex);%0A
+            %7D finally %7B%0A                try %7B%0A                    if (gremlinServer != null) gremlinServer.stop();%0A                %7D catch (Exception ex) %7B%0A                    logger.error(%22Could not stop Gremlin Server for performance tests%22, ex);%0A                %7D%0A
             %7D%0A  
@@ -2597,65 +2597,23 @@
     
-if (!thread.isInterrupted())%0A            thread.interrupt
+latch.countDown
 ();%0A

Refactor method for shutting down Gremlin Server in Performance Tests.