Crate async_metronome

Source
Expand description

§Unit testing framework for async Rust

This crate implements a async unit testing framework, which is based on the MutithreadedTC developed by William Pugh and Nathaniel Ayewah at the University of Maryland.

§Example

use async_metronome::{assert_tick, await_tick};

#[async_metronome::test]
async fn test_send_receive() {
        let (mut sender, mut receiver) = mpsc::channel::<usize>(1);

        let sender = async move {
            assert_tick!(0);
            sender.send(42).await.unwrap();
            sender.send(17).await.unwrap();
            assert_tick!(1);
        };

        let receiver = async move {
            assert_tick!(0);
            await_tick!(1);
            receiver.next().await;
            receiver.next().await;
        };
        let sender = async_metronome::spawn(sender);
        let receiver = async_metronome::spawn(receiver);

        sender.await;
        receiver.await;
}

§Explanation

async-metronome has an internal clock. The clock only advances to the next tick when all tasks are in a pending state.

The clock starts at tick 0. In this example, the macro await_tick!(1) makes the receiver block until the clock reaches tick 1 before resuming. Thread 1 is allowed to run freely in tick 0, until it blocks on the call to sender.send(17). At this point, all threads are blocked, and the clock can advance to the next tick.

In tick 1, the statement receiver.next(42) in the receiver is executed, and this frees up sender. The final statement in sender asserts that the clock is in tick 1, in effect asserting that the task blocked on the call to sender.send(17).

Macros§

assert_tick
Asserts current tick counter value.
await_tick
Awaits for the tick counter reach the specified value.

Structs§

JoinHandle
A future that awaits the result of a task.
Options
Options.
OptionsBuilder
Builder for Options.

Enums§

OptionsBuilderError
Error type for OptionsBuilder

Functions§

is_context
Checks if context is set.
run
Same as run_opt but with default options.
run_opt
Runs the test case and blocks until it complets or panics.
spawn
Spawns a task

Attribute Macros§

test
Marks async test function.