Skip to main content

on_call

on_call(hook) runs an async closure each time a subscription's handler is called, handing it the subscription's assertions as they stood at that call.

The hook runs once per delivery, or once per batch, after the call settled. It gets the same SubscriberAssertions a test reads, with the new call already in the record, and without the calls recorded after it, so assert_called and settled work inside it. The publish that caused the call returns only after the hook finished. A hook that never finishes therefore hangs that publish; the test's timeout is the guard. A hook still running when the test shuts the harness down, or drops it, is stopped where it waits. A failed assertion inside the hook fails the publish, settle or advance that drove the call, and otherwise the test's shutdown or the end of the test. A hook applies from the moment it is registered, and several hooks on one subscription run in the order they were registered. It runs the same way under start_live.

Example​

#[tokio::test]
async fn every_order_is_counted_as_it_arrives() -> Result<(), Box<dyn Error>> {
let tb = TestApp::start(app()).await?;
let calls = Arc::new(AtomicUsize::new(0));
let seen = Arc::clone(&calls);
tb.broker::<MemoryBroker>()
.subscriber("orders")
.on_call(async move |orders| {
let count = seen.fetch_add(1, Ordering::SeqCst) + 1;
orders.assert_called(count).settled(HandlerOutcome::ack());
});

for id in 1..=3 {
tb.broker::<MemoryBroker>()
.message(&Order { id })
.publish()
.await?;
}

assert_eq!(calls.load(Ordering::SeqCst), 3);
Ok(())
}