Skip to content

Commit

Permalink
fix async stest
Browse files Browse the repository at this point in the history
  • Loading branch information
LemonHX committed Mar 24, 2022
1 parent 13b0226 commit 77760f1
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions commons/stest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

//! The stest lib enhances the rust test framework with some useful functions.
use actix::System;
use anyhow::{format_err, Result};
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use futures::stream::StreamExt;
use futures::Future;
use std::sync::mpsc::{Receiver, Sender};
use std::time::Duration;
pub use stest_macro::test;
pub use tokio::{runtime::Runtime, task::LocalSet};

pub mod actix_export {
pub use actix_rt::*;
Expand Down Expand Up @@ -59,13 +59,16 @@ where
F: Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let t = tokio::task::spawn(f).await;
let _ = tx.unbounded_send(t.map_err(Into::<anyhow::Error>::into));
let res = System::current().arbiter().spawn(async move {
tx.unbounded_send(Ok(f.await)).unwrap();
});
if !res {
panic!("stest default system is died!")
}
}

pub async fn wait_result<T>(mut rx: UnboundedReceiver<Result<T>>) -> T {
let result = rx.next().await;
actix_rt::System::current().stop();
match result {
Some(Ok(t)) => t,
Some(Err(e)) => panic!("test fail: {:?}", e),
Expand Down

0 comments on commit 77760f1

Please sign in to comment.