您如何在 tokio::run 期货中编写测试断言?
How do you write test assertions inside of tokio::run futures?
你如何测试你的未来 运行 在 Tokio 运行 时间?
fn fut_dns() -> impl Future<Item = (), Error = ()> {
let f = dns::lookup("www.google.de", "127.0.0.1:53");
f.then(|result| match result {
Ok(smtptls) => {
println!("{:?}", smtptls);
assert_eq!(smtptls.version, "TLSRPTv1");
assert!(smtptls.rua.len() > 0);
assert_eq!(smtptls.rua[0], "mailto://...");
ok(())
}
Err(e) => {
println!("error: {:?}", e);
err(())
}
})
}
#[test]
fn smtp_log_test() {
tokio::run(fut_dns());
assert!(true);
}
未来的 运行 和未来的线程在 assert
上发生恐慌。您可以在控制台中看到恐慌,但 test
无法识别 tokio::run
.
的线程
没有回答这个问题,因为它只是说:测试异步代码的一种简单方法可能是为每个测试使用专用的运行时间
我这样做!
我的问题与测试如何检测未来是否有效有关。未来需要一个开始的运行时间环境。
尽管 future 断言或调用 err(),但测试成功。
那我该怎么办?
不要在未来写你的断言。
如, create a Runtime
to execute your future. As described in 所述,计算您的值,然后退出异步世界:
fn run_one<F>(f: F) -> Result<F::Item, F::Error>
where
F: IntoFuture,
F::Future: Send + 'static,
F::Item: Send + 'static,
F::Error: Send + 'static,
{
let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime");
runtime.block_on(f.into_future())
}
#[test]
fn smtp_log_test() {
let smtptls = run_one(dns::lookup("www.google.de", "127.0.0.1:53")).unwrap();
assert_eq!(smtptls.version, "TLSRPTv1");
assert!(smtptls.rua.len() > 0);
assert_eq!(smtptls.rua[0], "mailto://...");
}
你如何测试你的未来 运行 在 Tokio 运行 时间?
fn fut_dns() -> impl Future<Item = (), Error = ()> {
let f = dns::lookup("www.google.de", "127.0.0.1:53");
f.then(|result| match result {
Ok(smtptls) => {
println!("{:?}", smtptls);
assert_eq!(smtptls.version, "TLSRPTv1");
assert!(smtptls.rua.len() > 0);
assert_eq!(smtptls.rua[0], "mailto://...");
ok(())
}
Err(e) => {
println!("error: {:?}", e);
err(())
}
})
}
#[test]
fn smtp_log_test() {
tokio::run(fut_dns());
assert!(true);
}
未来的 运行 和未来的线程在 assert
上发生恐慌。您可以在控制台中看到恐慌,但 test
无法识别 tokio::run
.
我这样做!
我的问题与测试如何检测未来是否有效有关。未来需要一个开始的运行时间环境。
尽管 future 断言或调用 err(),但测试成功。
那我该怎么办?
不要在未来写你的断言。
如Runtime
to execute your future. As described in
fn run_one<F>(f: F) -> Result<F::Item, F::Error>
where
F: IntoFuture,
F::Future: Send + 'static,
F::Item: Send + 'static,
F::Error: Send + 'static,
{
let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime");
runtime.block_on(f.into_future())
}
#[test]
fn smtp_log_test() {
let smtptls = run_one(dns::lookup("www.google.de", "127.0.0.1:53")).unwrap();
assert_eq!(smtptls.version, "TLSRPTv1");
assert!(smtptls.rua.len() > 0);
assert_eq!(smtptls.rua[0], "mailto://...");
}