Skip to main content
The testkit package provides helpers for actor-level testing. It creates a throwaway actor system, probes for message assertions, and supports grain testing.

TestKit

TestKit
  • ActorSystem: Access the underlying actor system.
  • Spawn: Spawn an actor. Fails the test on error.
  • SpawnChild: Spawn a child of an existing actor.
  • Subscribe: Create an event-stream subscriber. Auto-unsubscribes on test cleanup.
  • Kill: Stop an actor by name.
  • NewProbe: Create a test probe.
  • NewGrainProbe: Create a grain test probe.
  • GrainOf[T]: Create a grain identity of kind T (package-level function).
  • Shutdown: Stop the actor system.

Options

  • WithLogging: Enable logging at the given level.
  • WithExtensions: Register extensions with the test system.

Probe

A probe is a test actor that records messages. Use it to assert message delivery instead of sleeps or polling.
Probe

Probe interface

  • ExpectMessage: Assert the next message exactly matches msg. Fails on timeout or mismatch.
  • ExpectMessageWithin: Same with custom timeout.
  • ExpectMessageOfType: Assert the next message has the same type as msg.
  • ExpectMessageOfTypeWithin: Same with custom timeout.
  • ExpectNoMessage: Assert no message arrives within the default timeout.
  • ExpectAnyMessage: Wait for and return the next message.
  • ExpectAnyMessageWithin: Same with custom timeout.
  • ExpectTerminated: Assert a Terminated message for the given actor.
  • ExpectTerminatedWithin: Same with custom timeout.
  • ExpectMessageMatching: Assert the next message satisfies the predicate function.
  • ExpectChildSpawned: Assert a child actor was spawned by the given parent.
  • ExpectChildSpawnedWithin: Same with custom timeout.
  • ClearMessages: Drain all pending messages from the probe’s queue.
  • Send: Tell the named actor (fire-and-forget).
  • SendSync: Ask the named actor; record the response in the probe.
  • Sender: PID of the sender of the last received message.
  • PID: PID of the probe itself (use as sender in tests).
  • Watch: Subscribe to termination of pid.
  • WatchNamed: Subscribe to termination of the named actor.
  • Stop: Shutdown the probe. Call in cleanup.
Default timeout is 3 seconds. Probes work in both standalone and cluster mode.

Asserting child actor creation

When an actor spawns a child during message handling, use ExpectChildSpawned to verify:
Asserting child actor creation

GrainProbe

Similar to Probe but for grains. Use the package-level testkit.GrainOf[T] function (or testkit.NodeGrainOf[T] on a cluster test node) to create identities, then send and assert responses.
  • ExpectResponse: Assert the next response matches.
  • ExpectResponseWithin: Same with custom timeout.
  • ExpectResponseOfType: Assert type match.
  • ExpectNoResponse: Assert no response within timeout.
  • ExpectAnyResponse: Wait for and return the next response.
  • ExpectTerminated: Assert grain termination.
  • Send: Tell the grain.
  • SendSync: Ask the grain; record response.
  • Stop: Shutdown the probe.

MultiNodes

For integration tests, MultiNodes creates an in-process multi-node cluster. See testkit/multi_nodes.go for usage. Requires cluster and remoting configuration.

Usage pattern

Usage pattern