-
Notifications
You must be signed in to change notification settings - Fork 340
Provide test macro like #[tokio::test]
or #[runtime::test]
#70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
While the #[async_std::test]
async fn foo() {
// ...
} #[test]
fn foo() {
task::block_on(async {
// ...
})
} That said, I wouldn't be against adding a |
Yes, the |
@95th compile times would likely spike significantly if we included such a macro. Perhaps it would make sense to create as a separate package (perhaps under the org) that's not necessarily part of the same top-level crate. In my opinion the main benefit of such macros is when abstracting over different systems; in particular between different execution styles. This isn't quite in scope for the |
While I do see the boilerplaty-ness of fn test() {
// future construction
let x = task::spawn(fut1);
let y = task::spawn(fut2);
task::block_on(async {
x.await;
y.await;
})
} The amount of boiler becomes less and less and it avoids running into annoying situations like this being a block_on in a block_on if we had such a test annotation. I agree with @yoshuawuyts that removing the line at the cost of compiling a proc macro seems excessive to me. |
@skade The pattern you described above is still looks better better with macro IMHO:
Agreed. |
For us, the only benefit of the But I think #[bench]
fn benchmark(b: &mut Bencher) {
b.iter(|| {
task::block_on(async {
// ...
})
})
} The reason is that We can then try: #[bench]
fn benchmark(b: &mut Bencher) {
task::block_on(async {
b.iter(|| {
// ...
})
})
} Still, that is not yet there because a future polled outside the runtime interacting with the reactor and the executor will be slower than one that is spawned onto the executor. So then the proper way of benchmarking tasks becomes: #[bench]
fn benchmark(b: &mut Bencher) {
task::block_on(task::spawn(async {
b.iter(|| {
// ...
})
}))
} Except one has to really think about this whenever benchmarking stuff in order to not get misleading numbers. So the summary is that an attribute is not only syntax sugar; it also provides the optimal benchmarking setup. |
async-attributes now exists, which adds support for all the features discussed in this issue (including @stjepang's correct bench code). You can get it from crates.io. Thanks all! |
As as the title says, async_std should provide macro like
#[async_std::test]
similar to#[tokio::test]
or#[runtime::test]
for writing tests.The text was updated successfully, but these errors were encountered: