This document will explain how you can use Splitlog to block execution of your application until a given Message shows up in the watched log. This is especially useful inside tests for web applications, where the progress of some operation is only signalled via the server log.
Expectations are a mechanism through which Splitlog allows you to block any thread until a particular condition is met. There are several kinds of expectations, but they all share some common properties:
The most obvious use case for expectations is to use them on Follower instances, in order to block for a particular kind of Message. Let’s say we want to wait until an ERROR shows up in the server log. First, let’s create the condition:
MidDeliveryMessageCondition<LogWatch> condition = new MidDeliveryMessageCondition<LogWatch>() { @Override public boolean accept(Message message, MessageDeliveryStatus status, LogWatch source) { return (message.getSeverity() == MessageSeverity.ERROR); } }
There are a few noteworthy things in this example:
The next step is to actually trigger the expectation:
Follower f = ...; // acquire follower instance here Future<Message> expectation = f.expect(condition); // this is your expectation
And now, here’s how you block:
try { Message result = expectation.get(); // this will block until condition is made true by incoming Messages // "result" holds your message now System.out.printn("An error has been caught: " + result); } catch (InterruptedException e) { System.out.println("The thread has been interrupted."); } catch (ExecutionException e) { System.out.println("An exception has been thrown in the user code."); }
Please note that this particular example requires a properly configured TailSplitter that will understand the severity of log messages. Please refer to the section on Splitting for more information.
For some general information on Metrics, please see the chapter about them. Here, we’ll focus merely on how to set expectations on top of them. You’ll see nothing much will change from the previous example. First, the condition changes a bit:
MessageMetricCondition<Integer, LogWatch> condition = new MessageMetricCondition<Integer, LogWatch>() { @Override public boolean accept(final MessageMetric<Integer, LogWatch> evaluate) { return evaluate.getValue() == 65; } }
Couple things to note:
The rest of the code remains mostly identical to the previous example:
Follower f = ...; // acquire follower instance here MessageMetric<Integer, LogWatch> m = f.startMeasuring(...); // acquire metric here Future<Message> expectation = m.expect(condition); // this is your expectation try { Message result = expectation.get(); // this will block until condition is made true by incoming Messages // "result" holds your message now System.out.printn("An error has been caught: " + result); } catch (Exception e) { // handle exceptions }
——————————————-
Think you spotted a mistake in this document? A typo? Factual inaccuracy? Make an edit. Think of the legions of grateful users!