Richard Searle's Blog

Thoughts about software

Archive for August, 2012

Unusual java httpserver race condition

Posted by eggsearle on August 29, 2012

The JDK httpserver provides a simple implementation that can easily be applied to unit tests.

The following example would generate a response containing responseBytes.
It might be expected that the referencing code would only see the response when exchange.close() is called.
In reality, the response can be seen as soon as the write executes!

That does not matter in a real web server since the client cannot be aware of the internal processing of the server.
Unit tests intrinsically couple the client and server together so as to progress through the test steps and validate the state of both parties.

This race condition caused unit tests to randomly fail, which is always hard to diagnose.

private static class MockHandler implements HttpHandler{
   @Override
   public void handle(HttpExchange exchange) throws IOException {
       exchange.getResponseBody().write(responseBytes);
       exchange.close();
   }
}

Posted in Uncategorized | Leave a Comment »

Performance surprising with Scala regexp parser combinators

Posted by eggsearle on August 21, 2012

The various examples generally have the form

def identifier  = """[_\p{L}][_\p{L}\p{Nd}]*""".r

This works fine, but the underlying Java Pattern is recompiled on every reference.

The behavior came to light during an upgrade from Java 6 and Scala 2.7.7 to Java 7 and Scala 2.9.2, when a ~ 10% performance degradation was noted.
Performance analysis indicated an unexpected large number of calls to Pattern.compile.
The Java 7 implementation is evidently somewhat slower.

Changing the def to val resolves the problem, without impact to the semantics.
In this case, the improvement was greater than 30% which  more than compensates for the degradation.

Posted in Scala | Leave a Comment »