Testing
Actors are tested with Aleph’s own test framework, under aleph test. A spec talks to real actors in a real actor
system: there is no mock of the runtime to keep in step.
Layout
my-service/
├── aleph.toml
├── src/ # the code
└── test/
├── Main.ai # lists the suites
└── CounterSpec.ai # one suite per module
aleph test builds src/ and test/ together and runs test/Main.ai. Its exit code is the number of failed
specs, so it fails a CI job by itself.
Writing a suite
A suite is a name and a list of tests; a test is a name and a function returning an assertion:
-- test/CounterSpec.ai
import Prelude
import Data.Maybe
import Data.Either
import Test
import Test.Assert
import Actor.Types
import Actor.Class
import Actor.System as Sys
import Counter
countAfter :: Int -> Address Counter -> Int
countAfter n counter = case n of
0 -> case ask counter (\r -> Current r) of
Right c -> c
Left f -> 0 - 1
_ -> tell counter Increment >> countAfter (n - 1) counter
counterSuite :: Suite
counterSuite = suite "counter"
[ Test "three increments count three" (\_ -> assertEqualInt 3 (countAfter 3 (counterOf (Sys.actorSystem ()))))
, Test "a fresh counter is zero" (\_ -> assertEqualInt 0 (countAfter 0 (counterOf (Sys.actorSystem ()))))
]
-- test/Main.ai
import Test
import CounterSpec
main = reportSummary (runSuites [counterSuite])
> counter
ok three increments count three
ok a fresh counter is zero
summary: passed=2 failed=0
Assertions include assertTrue, assertEqualInt, assertEqualString and assertEqualIntList; a failure prints
what was expected and what came.
Patterns
One system per test
Give each test its own Sys.actorSystem (). Tests then share no actors, and a route like account/alice is new in
each.
Asking and matching
Most specs ask and compare the answer. Turn an Either Fault r into a plain value the assertion compares, as
countAfter does, or compare the fault’s kind:
faultKind :: Either Fault Int -> String
faultKind answer = case answer of
Right n -> "answered"
Left (Fault kind detail) -> kind
Test "an overdraft is refused" (\_ -> assertEqualString "InsufficientFunds" (faultKind (ask alice (\r -> Withdraw 500 r))))
Probes
A probe is an actor that hands every message it receives to the test, in order. Give its address to the code under test, then expect:
import Actor.Probe as Probe
import Actor.Timer as Tm
data Ping = Ping
pinged :: ActorSystem -> Bool
pinged system = case Probe.probe system of
p -> case Tm.scheduleOnce 50 (Probe.probeAddress p) Ping of
_ -> case Probe.expectMsg p 1000 of
Just Ping -> True
Nothing -> False
| Function | Meaning |
|---|---|
Probe.expectMsg p ms |
The next message within ms, or Nothing |
Probe.expectMsgs p n ms |
The next n messages |
Probe.expectNoMsg p ms |
True when nothing arrives within ms |
A wait that times out leaves its listener on the probe, which takes the next message: expect nothing only last.
Death watch in a test
Sys.watching target (Probe.probeAddress p) Terminated tells the probe Terminated when target stops.
Values without actors
Breakers, CRDTs, the failure detector and routing steps are values over a given clock. Test them as functions, passing the time in; no actor, no waiting.
Running tests
aleph test # every suite
ALEPH_TEST_FILTER=counter aleph test
On Linux, the Dockerfiles have a test stage that runs the same specs inside the build:
docker build -f MLambda.Actor.Demo/Dockerfile --target test ../..
When a run stops early
A deadlock (every fiber waiting) ends the run with a message and loses the report. Run suites one at a time to find the test, and look for an actor asking another that is waiting on it.