-
-
Save cowwoc/70b0fe1071dd23c9d546 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.concurrent.TimeUnit; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.eclipse.jetty.server.Request; | |
import org.eclipse.jetty.server.Server; | |
import org.eclipse.jetty.server.handler.AbstractHandler; | |
import org.testng.Assert; | |
import org.testng.annotations.BeforeClass; | |
import org.testng.annotations.Test; | |
import com.google.common.io.CharStreams; | |
@Test | |
public class TestJetty { | |
private Server server; | |
@BeforeClass | |
public void setup() throws Exception { | |
this.server = new Server(11111); | |
this.server.setHandler(new TestHandler()); | |
this.server.start(); | |
this.server.setStopTimeout(10 * 1000); | |
} | |
public void testGraceful() throws Exception { | |
new Thread() { | |
public void run() { | |
try { | |
TimeUnit.SECONDS.sleep(1); | |
server.stop(); | |
} catch (Exception e) { | |
} | |
} | |
}.start(); | |
URL url = new URL("http://localhost:11111/"); | |
URLConnection connection = url.openConnection(); | |
connection.connect(); | |
String out = CharStreams.toString(new InputStreamReader(connection.getInputStream())); | |
Assert.assertEquals(out, "OK"); | |
} | |
private static class TestHandler extends AbstractHandler { | |
public void handle(final String s, final Request request, final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse) throws IOException, ServletException { | |
try { | |
TimeUnit.SECONDS.sleep(5); | |
} catch (InterruptedException e) { | |
} | |
httpServletResponse.getWriter().write("OK"); | |
httpServletResponse.setStatus(200); | |
request.setHandled(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment