Skip to content

Remove need for ENSO_HTTP_TEST_HTTPBIN_URL by launching HTTP Bin server automatically #14556

@JaroslavTulach

Description

@JaroslavTulach

Goal

The goal is to simplify test/*_Tests execution by removing one currently necessary setup step.

  • many tests require adding "HTTP Bin" server
  • this is currently started as a separate process sbt 'http-test-helper/run localhost 8080'
  • and provided via ENSO_HTTP_TEST_HTTPBIN_URL environment variable

This is annoying and clearly unnecessary. Let the tests start the server by themselves!

Approach

  • turn http-test-helper main class into a launch method
diff --git tools/http-test-helper/src/main/java/org/enso/shttp/HTTPTestHelperServer.java tools/http-test-helper/src/main/java/org/enso/shttp/HTTPTestHelperServer.java
index 9f5d1c445c..74c38308e4 100644
--- tools/http-test-helper/src/main/java/org/enso/shttp/HTTPTestHelperServer.java
+++ tools/http-test-helper/src/main/java/org/enso/shttp/HTTPTestHelperServer.java
@@ -1,13 +1,13 @@
 package org.enso.shttp;
 
 import com.sun.net.httpserver.SimpleFileServer;
+import java.io.Closeable;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Arrays;
 import java.util.concurrent.Executor;
-import java.util.concurrent.Semaphore;
 import java.util.stream.Stream;
 import org.enso.shttp.auth.BasicAuthTestHandler;
 import org.enso.shttp.auth.TokenAuthTestHandler;
@@ -23,8 +23,10 @@ import org.enso.shttp.test_helpers.RedirectTestHandler;
 import org.enso.shttp.test_helpers.TestHandler;
 
 public class HTTPTestHelperServer {
+    private HTTPTestHelperServer() {
+    }
 
-  public static void main(String[] args) {
+  public static Closeable launch(String... args) {
     if (args.length < 2) {
       System.err.println("Usage: http-test-helper <host> <port> [additional test options]");
       System.exit(1);
@@ -40,25 +42,8 @@ public class HTTPTestHelperServer {
       e.printStackTrace();
       System.exit(1);
     }
-
-    Semaphore semaphore = new Semaphore(0);
-    Runtime.getRuntime()
-        .addShutdownHook(
-            new Thread() {
-              public void run() {
-                semaphore.release();
-              }
-            });
     server.start();
-    System.out.println("Server started.");
-    try {
-      semaphore.acquire();
-      System.out.println("Shutting down...");
-    } catch (InterruptedException e) {
-      System.err.println("Shutting down abruptly...");
-    } finally {
-      server.stop();
-    }
+    return server;
   }
 
   /**
diff --git tools/http-test-helper/src/main/java/org/enso/shttp/HybridHTTPServer.java tools/http-test-helper/src/main/java/org/enso/shttp/HybridHTTPServer.java
index 456e56770e..a2f3e6b723 100644
--- tools/http-test-helper/src/main/java/org/enso/shttp/HybridHTTPServer.java
+++ tools/http-test-helper/src/main/java/org/enso/shttp/HybridHTTPServer.java
@@ -1,6 +1,7 @@
 package org.enso.shttp;
 
 import com.sun.net.httpserver.*;
+import java.io.Closeable;
 import java.io.IOException;
 import java.net.InetSocketAddress;
 import java.nio.file.Files;
@@ -17,7 +18,7 @@ import org.enso.shttp.cloud_mock.CloudAuthRenew;
 import org.enso.shttp.cloud_mock.CloudRoot;
 import org.enso.shttp.cloud_mock.EventsService.LogEvent;
 
-public class HybridHTTPServer {
+public class HybridHTTPServer implements Closeable {
 
   private final HttpServer server;
   private final HttpsServer sslServer;
@@ -161,6 +162,11 @@ public class HybridHTTPServer {
       sslServer.stop(1);
     }
   }
+  
+  @Override
+  public void close() {
+     stop();
+  }
 
   public void addHandler(String path, HttpHandler handler) {
     server.createContext(path, handler);
  • modify tests that require ENSO_HTTP_TEST_HTTPBIN_URL variable to launch the server (at random port) by itself
  • remove all occurrences of ENSO_HTTP_TEST_HTTPBIN_URL from the code base
  • simplify documentation

Metadata

Metadata

Assignees

No one assigned

    Labels

    -compiler-libsLibraries: New libraries to be implemented

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions