Skip to content

Verification

actor4j-verification verifies the behavior of actors on the level of a model. Each actor describes its behavior as a state machine with states and transitions. A transition can be triggered by received messages (events) and can send messages to other actors. The module then offers two kinds of checks:

  • Analysis of the model: cycles, unreachable states, dead states (no outgoing transition, the actor hangs there) and traps (cycles the actor can never leave). This works for a single actor and for the overall system, where the state machines of all actors are interconnected via the events they send and receive.
  • Conformance check: while a system runs, the messages of the actors are recorded and compared with their models. This shows where the code no longer matches the model, e.g. after receive() was changed but verify() was not.

The conformance check only covers the flows a test actually runs through. It complements the analysis of the model, it does not replace it.

An actor of Actor4j - Core becomes verifiable by implementing the interface ActorVerification. Its method verify() returns the model as ActorVerificationSM. The analysis (ActorVerifier) needs no actor system and works on the models alone. The conformance check needs a running system: ActorVerificator is an ActorSystem based on the default runtime (actor4j-core-runtime) with its own message dispatcher that records the messages. The graphs are built with JGraphT (jgrapht-core).

<dependency>
<groupId>io.actor4j</groupId>
<artifactId>actor4j-verification</artifactId>
<version>2.5.0</version>
</dependency>

If the actors themselves implement ActorVerification, the dependency is needed at compile time. If the models are only built in tests, <scope>test</scope> is enough.

States are named per actor. Internally, they get the name of the actor as prefix (ping:PING), so the states of different actors stay apart. States are created with the first transition that uses them. state(...) declares a state without transitions.

Builder Meaning
ActorVerificationSM.of(actor) creates the model of the actor
initial("S") initial state
state("S") a state, also without transitions
on(event, ...).from("S").to("T") transition triggered by one or more received events (message tags)
from("S").to("T") transition without received event, e.g. only sending
.send(event, alias, ...) the last transition sends the event to the actors with these names or aliases. Can be repeated.

The ping actor in the following example has the states PING, A, B, C and D. With the event PONG it stays in PING and sends PING to the pong actor. With the event 0 it moves from PING to D, from where it never gets away. The states A, B and C form a cycle that cannot be reached from PING. The pong actor has only the state PONG: with the event PING it stays there and sends PONG back to the ping actor.

public static final int PING = 100;
public static final int PONG = 101;
public class Ping extends Actor implements ActorVerification {
public Ping() {
super("ping");
}
@Override
public void receive(ActorMessage<?> message) {
if (message.tag()==PONG)
tell(null, PING, message.source());
}
@Override
public ActorVerificationSM verify() {
return ActorVerificationSM.of(this)
.initial("PING")
.on(PONG).from("PING").to("PING").send(PING, "pong")
.on(0).from("PING").to("D")
.on(0).from("A").to("B")
.on(0).from("B").to("C")
.on(0).from("C").to("A");
}
}
public class Pong extends Actor implements ActorVerification {
public Pong() {
super("pong");
}
@Override
public void receive(ActorMessage<?> message) {
if (message.tag()==PING)
tell(null, PONG, message.source());
}
@Override
public ActorVerificationSM verify() {
return ActorVerificationSM.of(this)
.initial("PONG")
.on(PING).from("PONG").to("PONG").send(PONG, "ping");
}
}

Give verifiable actors a name. Without a name, the prefix of the states is derived from the id of the actor, which is hard to read. The older methods addInitialStateMarker, addStateMarker, addInTransition and addOutTransition still exist and create the same model.

ActorVerifier collects the models, makes the names unique (actors with the same name become name#1, name#2, …) and interconnects them: a transition that sends an event to an actor is connected with the transitions of that actor that receive this event. The result is a VerificationResult.

VerificationResult result = ActorVerifier.verify(new Ping(), new Pong());
StateMachineAnalysis ping = result.actor("ping"); // starts at the initial state
ping.cycles(); // [ping:PING], [ping:A, ping:B, ping:C]
ping.unreachables(); // [ping:A, ping:B, ping:C]
ping.dead(); // [ping:D]
result.actor("pong").traps(); // [pong:PONG], pong can never leave PONG
StateMachineAnalysis all = result.all().from("ping"); // overall system, starting at ping:PING
all.cycles(); // additionally [pong:PONG] and [ping:PING, pong:PONG]
System.out.println(result.report());
Method of StateMachineAnalysis Result
cycles() all simple cycles
unreachables() states that cannot be reached from the start state
dead() states without outgoing transition (only reachable ones, if a start state is known)
traps() states of cycles without a transition out of the cycle (only reachable ones, if a start state is known)
from(actor), from(actor, state) the same analysis, starting at the initial state or at a state of the actor

The overall system (result.all()) has no start state of its own. For unreachables() it needs one, given by from(...). ActorVerifier.verifyModels(list) analyzes a list of ActorVerificationSM directly, e.g. models built in a test.

VerificationAssert turns the results into assertions for tests. They throw an AssertionError and work with any test framework:

VerificationAssert.assertCycle(result.all(), "ping:PING", "pong:PONG");
VerificationAssert.assertNoUnreachableStates(result.actor("pong"));
VerificationAssert.assertNoDeadStates(result.actor("pong"));
VerificationAssert.assertNoDeadStates(result.all().from("ping")); // fails: dead states: [ping:D]

Further assertions are assertNoCycles and assertConforms (see below).

ActorVerificator runs the actors as an ordinary actor system and records every message that is sent or received by an actor implementing ActorVerification. Afterwards, conformance() compares the recorded messages with the models:

  • a sender with a model must have a transition that sends this event to the receiver (by its name or by the alias used),
  • a receiver with a model must have a transition that is triggered by this event.

Internal messages (negative tags such as stop or restart) are ignored. If an actor also implements ActorVerificationState, it reports its current state (String state()). Then the sending transition must also start or end in this state.

CountDownLatch done = new CountDownLatch(1);
ActorVerificator verificator = ActorVerificator.create();
ActorId ping = verificator.addActor(() -> new Ping() {
protected int rounds;
@Override
public void receive(ActorMessage<?> message) {
if (message.tag()==PONG) {
if (++rounds<=5)
tell(null, PING, message.source());
else
done.countDown();
}
}
});
ActorId pong = verificator.addActor(() -> new Pong());
verificator.start();
verificator.send(ActorMessage.create(null, PONG, pong, ping)); // as if pong had sent it
done.await(2, TimeUnit.SECONDS);
verificator.shutdownWithActors(true);
ConformanceResult result = verificator.conformance(Map.of(PING, "PING", PONG, "PONG"));
VerificationAssert.assertConforms(result);

The map with event names is optional and only makes the messages readable. If pong answered with an event that is not in the models, e.g. OTHER, the result would contain:

pong sent OTHER to ping, not in the model
ping received OTHER from pong, not in the model

ConformanceResult offers conforms(), violations() and observed() (number of checked messages). clearObserved() discards the recorded messages, e.g. between two test phases.

ActorVerificator also collects the models of all actors added to it: verify(consumer) passes each model, verifyAll(consumer, consumerAll) additionally passes the interconnected graph of the overall system.

Further examples can be found in the tests of the module under actor4j-verification.