* method.
*/
private void handleNextTest(Map<String, String> params, HttpExchange exchange) {
// Read the test results returned by the browser, if any
long browserId = parseLong(params.get("browserId"), -1);
LongPollingBrowser browser = browsers.get(browserId);
if (browser == null) {
browser = selfAssignedBrowser(browserId);
}
MultiTestMethod completedMethod = browser.getMethodUnderExecution();
if (completedMethod != null) {
// We only have a method under execution, if the HTTP request that is being
// handled is not the first one the server has received
if (config.isDebugEnabled()) {
System.out.println("Server received test results for method " + completedMethod.toString() + " from browser " + browserId);
}
// notify JUnit of the result of this test. When the last browser notifies
// the MultiTestMethod, the JUnit thread will become unblocked and the test result
// will be reported
TestResult result = browser.buildResult(params, exchange);
completedMethod.notifyExecutionResult(result);
} else {
if (config.isDebugEnabled()) {
System.out.println("Server received request for the first test from browser " + browserId);
}
}
// Wait for the JUnit thread to send us the next test. We block this thread
// until we have a new test to send to the browser or the server is shutdown,
// whichever comes first. Basically, we are not sending the HTTP response to the
// browser until we have received a new test
MultiTestMethod nextMethod = browser.awaitNextTest();
if (nextMethod != null) {
if (config.isDebugEnabled()) {
System.out.println("Server is sending test for method " + nextMethod.toString() + " to browser " + browserId);
}
try {
browser.sendTestFixture(nextMethod, exchange);
}
catch (Exception e) {
// we failed to send the fixture. This means that the browser will not request the next test,
// it is therefore essentially dead.
browser.markAsDead(e, exchange.getRequestHeaders().getFirst("User-Agent"));
throw new RuntimeException(e);
}
} else {
try {
browser.sendNoMoreTestFixture(exchange);
}
catch (IOException ioe) {
// sending a 500 error has basically the same effect as sending a proper response. The browser may
// not cleanup properly, but hey, this is disaster recovery
throw new RuntimeException(ioe);