Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update SE examples unit tests to start the webserver using Single.await #2845

Closed
romain-grecourt opened this issue Mar 9, 2021 · 2 comments
Assignees
Milestone

Comments

@romain-grecourt
Copy link
Contributor

romain-grecourt commented Mar 9, 2021

SE examples have unit tests starting the server with a blocking loop:

webServer = Main.startServer();

long timeout = 2000; // 2 seconds should be enough to start the server
long now = System.currentTimeMillis();

while (!webServer.isRunning()) {
    //noinspection BusyWait
    Thread.sleep(100);
    if ((System.currentTimeMillis() - now) > timeout) {
        Assertions.fail("Failed to start webserver");
    }
}

Instead, the startServer method should be written more or less that way:

static Single<WebServer> startServer(Config config) {
    // load logging configuration
    LogConfig.configureRuntime();

    // Build server using three ports:
    // default public port, admin port, private port
    WebServer server = WebServer.builder(createRouting())
            .config(config.get("server"))
            // ...
            .build();

    Single<WebServer> webserver = server.start();

    webserver.thenAccept(ws -> {
                System.out.println(
                        "WEB server is up! http://localhost:" + ws.port());
                ws.whenShutdown().thenRun(()
                        -> System.out.println("WEB server is DOWN. Good bye!"));
            })
            .exceptionally(t -> {
                System.err.println("Startup failed: " + t.getMessage());
                t.printStackTrace(System.err);
                return null;
            });

    return webserver;
}

Then the test can do something like this:

WebServer webServer = Main.startServer(config).await();
@romain-grecourt
Copy link
Contributor Author

This needs to be fixed for examples/media/multipart and the SE quickstart.

@romain-grecourt
Copy link
Contributor Author

CC @tomas-langer @barchetta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

No branches or pull requests

3 participants