RESTServer extends Thread { public boolean blockingStart() { start(); try { return isWorking(System.currentTimeMillis(), 2000); } catch (InterruptedException e) { error = e; Thread.currentThread().interrupt(); return false; } } @SuppressWarnings("rawtypes")  RESTServer(int port, HttpRESTHandler requestHandler); @SuppressWarnings("rawtypes")  RESTServer(CoreConfiguration conf, HttpRESTHandler requestHandler); boolean isExecuting(); boolean hasError(); Exception getError(); int getPort(); boolean blockingStart(); boolean isWorking(long startTime, long timeout); @Override void run(); void shutdown(); static RESTServer getNewStartedRESTServer(int port, HttpRESTHandler<?> restHandler);  }
@Test public void testBlockingStart() throws IOException, InterruptedException { CoreConfiguration conf = ConfigurationFactory.getConfiguration("jUnit-RESTServerTest"); MongoConnector dbc = new MongoConnector(conf); int port = conf.getRestPort(); RESTServer server1 = new RESTServer(conf, new HttpRESTHandler<MongoType>(dbc)); if(!server1.blockingStart()) { server1 = RESTServer.getNewStartedRESTServer(port, new HttpRESTHandler<MongoType>(dbc)); } System.out.println("Started server 1 on port "+port); RESTServer server2 = new RESTServer(conf, new HttpRESTHandler<MongoType>(dbc)); if(server2.blockingStart()) { System.out.println("We are failing on port "+server2.getPort()); Thread.sleep(1000); System.out.println("1 alive: "+server1.isAlive()); System.out.println("2 alive: "+server2.isAlive()); System.out.println("1 hasError: "+server1.hasError()); System.out.println("2 hasError: "+server2.hasError()); System.out.println("1 getError: "+server1.getError()); System.out.println("2 getError: "+server2.getError()); System.out.println("1 isExecuting: "+server1.isExecuting()); System.out.println("2 isExecuting: "+server2.isExecuting()); fail("blockingStart() returned true when port should already be taken"); } server2 = RESTServer.getNewStartedRESTServer(port, new HttpRESTHandler<MongoType>(new MongoConnector(conf))); System.out.println("Restarted on port "+server2.getPort()); server2 = RESTServer.getNewStartedRESTServer(port, new HttpRESTHandler<MongoType>(new MongoConnector(conf))); System.out.println("Restarted on port "+server2.getPort()); server1.shutdown(); server2.shutdown(); }