【问题标题】:How do I assert that a Rust futures-preview future has not yet resolved我如何断言 Rust 期货预览未来尚未解决
【发布时间】: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


    【解决方案1】:

    您能否发布您的 Cargo.toml 以及属于示例代码的任何使用语句?我无法运行您的代码,但在查看了您的测试示例和错误消息后,我相信有一些 related questions(和此处)可能对您有所帮助。

    您能否尝试更新到 nightly 的最新版本,看看是否能解决问题?该功能似乎是recently stabilized。

    rustup update nightly
    

    这是假设您使用的是 rust 编译器的夜间版本。

    祝你好运。

    【讨论】:

    • 发布了 Cargo.toml。我已经看过你的链接问题,但我认为它不能回答我遇到的问题。在稳定合并之前的大约 4 天,我每晚都在进行,但它被锁定以使团队都保持在同一个版本上。然而,我发现了我最后一个变体的问题,并在下面发布了一个答案,不需要任何额外的库(或更新的 rust 版本)。
    • 您解决了这个问题真是太好了。希望我能提供更多帮助!
    【解决方案2】:

    想通了(在一点帮助下)。我的最后一次尝试很接近,但做得太多了。这是一个实际可行的简化版本:

        #[test]
        fn test_trigger_future_untriggered() {
            let tf = TriggerFuture::default();
            let f1 = tf.future();
    
            tf.trigger();
    
            ThreadPool::default()
                .spawn_wait::<_, _, &str>(async {
                    use futures_timer::Delay;
                    let delay = Delay::new(std::time::Duration::from_millis(50));
                    let res = future::select(f1, delay).await;
                    match res {
                        Either::Left(..) => Err("The TriggerFuture resolved first"),
                        Either::Right(..) => Ok(()),
                    }
                })
                .expect("The Delay should have resolved first");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-08
      • 2019-01-22
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 2015-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多