知识卡片
异步测试隔离运行时
测试自主管理任务、时间与运行时生命周期,避免一个测试留下的后台任务影响另一个。若生产逻辑依赖多线程或特定 I/O 驱动,需要补充覆盖对应运行时配置的测试。仅返回 Ready 的替身适合测业务分支,难以检出唤醒和取消问题。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- Chapter 11. Testing 用异步替身返回可轮询结果 · 查看原书依据
Now that our function is async, we need to define a runtime inside our test and then block on it. Our tests are not going to all run on one thread, so we can ensure that our tests are atomic by defining the runtime in each individual test:
参考来源
- 位置:《Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)》第11章《Chapter 11. Testing》,用异步替身返回可轮询结果(来源项目 book-source,extracted/Async Rust Unleashing the Power of Fearless Concurrency (Maxwell Flitton, Caroline Morton)/_epub-src/OEBPS/ch11.html#id70)
- 结论依据:书中为测试单独构建运行时,以 mock Future 驱动结果分支。
- 原始内容:Now that our function is async, we need to define a runtime inside our test and then block on it. Our tests are not going to all run on one thread, so we can ensure that our tests are atomic by defining the runtime in each individual test: