#async #test #safe #safina

dev safina-async-test

Macro for running async tests

1 unstable release

0.1.9 Mar 27, 2021
0.1.8 Mar 26, 2021
0.1.7 Dec 31, 2020

#557 in Asynchronous


Used in 4 crates

Apache-2.0

79KB
842 lines

safina-async-test

crates.io version license: Apache 2.0 unsafe forbidden pipeline status

An async_test macro for running async fn tests.

It is part of safina, a safe async runtime.

Features

  • Runs tests with safina_executor
  • Each test gets its own executor with 2 threads for async tasks and 1 thread for blocking tasks.
  • Also calls safina_timer::start_timer_thread before running the test
  • forbid(unsafe_code)
  • Lightweight dependencies
  • Straightforward implementation

Limitations

Examples

use safina_async_test::async_test;

#[async_test]
async fn test1() {
    async_work().await.unwrap();
}
use safina;
use safina_async_test::async_test;

// Make your test an `async fn`.
#[async_test]
async fn test2() {
    // You can `await`.
    async_work().await.unwrap();

    // You can spawn tasks which will run on
    // the executor.
    // These tasks stop when the test
    // function returns and drops the
    // executor.
    safina::spawn(background_task());

    // You can run blocking code without
    // stalling other async tasks.
    let result = safina::schedule_blocking(
        || blocking_work()
    );
    assert_eq!(3, result.await.unwrap());

    // You can use timer functions.
    safina::sleep_for(
        Duration::from_millis(10)).await;
    safina::with_timeout(
        async_work(),
        Duration::from_millis(100)
    ).await.unwrap();
}

Documentation

https://docs.rs/safina-async-test

Alternatives

Changelog

  • v0.1.9 - Don't require users to also depend on safina-executor and safina-timer crates.
  • v0.1.8
    • Support stable with rust 1.51 and once_cell.
    • Start an Executor for each test
  • v0.1.7 - Update to safina-executor v0.1.4
  • v0.1.6 - Start safina-timer thread
  • v0.1.5 - Use safina-executor v0.1.3 API
  • v0.1.4 - Upgrade to new safina-executor version which removes need for Box::pin.
  • v0.1.3 - Rename safina package to safina-executor.
  • v0.1.2 - Update docs
  • v0.1.1 - First published version

Happy Contributors 🙂

Fixing bugs and adding features is easy and fast. Send us a pull request and we intend to:

  • Always respond within 24 hours
  • Provide clear & concrete feedback
  • Immediately make a new release for your accepted change

TO DO

  • DONE - Implement as declarative macro. UX is bad.
  • DONE - Implement as procedural macro.
  • DONE - Report errors nicely
  • DONE - Publish on crates.io
  • DONE - Automatically start a worker thread and timer thread.
  • DONE - Let users depend only on safina-async-test:
    • DONE - Move proc macro to its own crate.
    • DONE - Make safina_async_test re-export the macro and safina_executor::block_on.
    • DONE - Change the macro to call safina_async_test::block_on.

Release Process

  1. Edit Cargo.toml and bump version number.
  2. Run ./release.sh

License: Apache-2.0

Dependencies

~200KB