【发布时间】:2019-10-11 02:59:38
【问题描述】:
我正在尝试更新我的依赖项。我有一些使用期货预览和期货计时器的代码。 futures-timer 的早期版本包括在未来设置超时的能力,我有一个使用此功能的测试,但它是最近版本的removed。
是否有一种直接的方法来检查未来是否已解决,如果已解决,则测试失败? (另外,“解决”是正确的词吗?)
我尝试过使用f1.poll(),但无法完全弄清楚返回类型以及如何使用它。
我也尝试过使用timeout from async_std,但我遇到了一堆错误:
error[E0008]: cannot bind by-move into a pattern guard
--> /Users/nathan/.cargo/registry/src/github.com-1ecc6299db9ec823/async-std-0.99.9/src/net/driver/mod.rs:207:17
|
207 | Err(err) if err.kind() == io::ErrorKind::WouldBlock => {}
| ^^^ moves value into pattern guard
|
= help: add `#![feature(bind_by_move_pattern_guards)]` to the crate attributes to enable
除非我弄错了,否则就是要求我修改 async-std,对吗?有办法解决吗?
这是我的测试:
#[cfg(test)]
mod tests {
use super::*;
use crate::threadpool::ThreadPool;
use futures_timer::TryFutureExt;
// ....
#[test]
fn test_trigger_future_untriggered() {
let tf = TriggerFuture::default();
let f1 = tf.future();
ThreadPool::default()
.spawn_wait::<_, _, ()>(async {
f1.map(|_| Result::<(), std::io::Error>::Ok(()))
.timeout(std::time::Duration::from_millis(50))
.await
.expect_err("expected timeout");
Ok(())
})
.expect("expected!");
}
}
更新:这是我尝试的另一件事。使用futures-timer v1.0.1,如果我取消注释tf.trigger(),它会按预期失败,但是当我不注释时它会永远运行。这里有什么问题?
#[test]
fn test_trigger_future_untriggered() {
let tf = TriggerFuture::default();
let f1 = tf.future();
// tf.trigger();
ThreadPool::default()
.spawn_wait::<_, _, _>(async {
use futures_timer::Delay;
let delay = Delay::new(std::time::Duration::from_millis(50));
future::select(f1, delay).then(|either| {
match either {
Either::Left((_, b)) => b.map(move |y| (Err("this future shoudn't resolve"), y)).left_future(),
Either::Right((_, a)) => a.map(move |x| (Ok("expected timeout"), x)).right_future(),
}
}).await.0
})
.expect("expected!");
}
这是我要求的 Cargo.toml:
[package]
name = "shared_futures"
version = "0.1.0"
publish = false
edition = "2018"
[dependencies]
futures-preview = { version = "0.3.0-alpha.19", features = [ "async-await", "compat" ] }
crossbeam-channel = "0.3.9"
num_cpus = "1.10.1"
# Note: the timeout feature was removed in futures-timer v0.6
futures-timer = "1.0.1"
[features]
sanitizer_safe = []
[lib]
name = "shared_futures"
crate-type = ["rlib"]
【问题讨论】:
标签: rust