Fixed a bug that HttpServer in unit test does not start correctly. (#2361)

Because the http client in unit test try to connect to the server when
server is not ready yet.
This commit is contained in:
Mingyu Chen
2019-12-03 20:34:16 +08:00
committed by GitHub
parent 086bb82fd2
commit c8cff85c94
2 changed files with 15 additions and 2 deletions

View File

@ -75,6 +75,7 @@ import org.apache.logging.log4j.Logger;
import java.io.File;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
@ -94,6 +95,8 @@ public class HttpServer {
private Thread serverThread;
private AtomicBoolean isStarted = new AtomicBoolean(false);
public HttpServer(int port) {
this.port = port;
controller = new ActionController();
@ -202,8 +205,11 @@ public class HttpServer {
.channel(NioServerSocketChannel.class)
.childHandler(new PaloHttpServerInitializer());
Channel ch = serverBootstrap.bind(port).sync().channel();
isStarted.set(true);
LOG.info("HttpServer started with port {}", port);
// block until server is closed
ch.closeFuture().sync();
LOG.info("HttpServer started with port: {}", port);
} catch (Exception e) {
LOG.error("Fail to start FE query http server[port: " + port + "] ", e);
System.exit(-1);
@ -220,6 +226,7 @@ public class HttpServer {
Future future = serverBootstrap.config().group().shutdownGracefully(0, 1, TimeUnit.SECONDS).syncUninterruptibly();
try {
future.get();
isStarted.set(false);
LOG.info("HttpServer was closed completely");
} catch (Throwable e) {
LOG.warn("Exception happened when close HttpServer", e);
@ -228,6 +235,10 @@ public class HttpServer {
}
}
public boolean isStarted() {
return isStarted.get();
}
public static void main(String[] args) throws Exception {
HttpServer httpServer = new HttpServer(8080);
httpServer.setup();

View File

@ -255,7 +255,9 @@ abstract public class DorisHttpTestCase {
httpServer.setup();
httpServer.start();
// must ensure the http server started before any unit test
Thread.sleep(5000);
while (!httpServer.isStarted()) {
Thread.sleep(500);
}
doSetUp();
}