java-express icon indicating copy to clipboard operation
java-express copied to clipboard

testability - synchronous version of Express.listen()

Open szczebel opened this issue 4 years ago • 0 comments

Express.listen() spawns a separate thread to instantiate and start underlying HttpServer. This introduces a race condition if an automated test is trying to execute an http call to the express app right after call to listen().

Workaround:

public void start(int port) {
        System.out.println("Application starting...");
        CountDownLatch latch = new CountDownLatch(1);

        express = new Express();
        //configuration of routes omitted

        express.listen(latch::countDown, port);
        latch.await();
        System.out.println("Application started @ http://localhost:" + getPort()); 
}

I'd prefer not to have to use CountDownLatch.

szczebel avatar Dec 10 '20 23:12 szczebel