Skip to content

Commit

Permalink
feat: allow to stop the invoker
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Feb 21, 2022
1 parent 6312e64 commit f9ae5af
Showing 1 changed file with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ private static class FunctionClassLoader extends URLClassLoader {
private final String functionSignatureType;
private final ClassLoader functionClassLoader;

private Server server;

public Invoker(
Integer port,
String functionTarget,
Expand Down Expand Up @@ -225,8 +227,52 @@ ClassLoader getFunctionClassLoader() {
return functionClassLoader;
}

/**
* This will start the server and wait (join) for function calls.
* To start the server inside a unit or integration test, use {@link #startTestServer()} instead.
*
* @see #stopServer()
* @throws Exception
*/
public void startServer() throws Exception {
Server server = new Server(port);
startServer(true);
}


/**
* This will start the server and return.
*
* This method is designed to be used for unit or integration testing only.
* For other use cases use {@link #startServer()}.
*
* Inside a test a typical usage will be:
* {@code
* // Create an invoker
* Invoker invoker = new Invoker(
* 8081,
* "org.example.MyHttpFunction",
* "http",
* Thread.currentThread().getContextClassLoader()
* );
*
* // Start the test server
* invoker.startTestServer();
*
* // Test the function
*
* // Stop the test server
* invoker.stopServer();
* }
*
* @see #stopServer()
* @throws Exception
*/
public void startTestServer() throws Exception {
startServer(false);
}

private void startServer(boolean join) throws Exception {
server = new Server(port);

ServletContextHandler servletContextHandler = new ServletContextHandler();
servletContextHandler.setContextPath("/");
Expand Down Expand Up @@ -261,7 +307,21 @@ public void startServer() throws Exception {

server.start();
logServerInfo();
server.join();
if (join) {
server.join();
}
}

/**
* Stop the server.
*
* @see #startServer()
* @see #startTestServer()
*
* @throws Exception
*/
public void stopServer() throws Exception {
server.stop();
}

private Class<?> loadFunctionClass() throws ClassNotFoundException {
Expand Down

0 comments on commit f9ae5af

Please sign in to comment.