【发布时间】:2021-06-19 09:20:42
【问题描述】:
我想测试我的一个应该恐慌的函数,但我的 GitHub 操作 abort() 导致即使我的代码要求“仅”2**31 字节(在我的真实代码中,限制是 libc::c_int::MAX)我的电脑有,GitHub action 没有这样的内存:p.
#[test]
#[should_panic]
fn reserve_with_max() {
Vec::with_capacity(usize::MAX);
}
但这失败了:
test tests::bad_fd ... ok
test tests::create_true ... ok
test tests::create_false ... ok
test tests::create_with_one ... ok
memory allocation of 25769803764 bytes failed
这会停止测试并报告错误,即使这是我所期望的(恐慌或中止)。
我没有找到太多关于这个问题的信息:
- https://github.com/rust-lang/rust/issues/67650
- https://doc.rust-lang.org/std/alloc/fn.set_alloc_error_hook.html(但每晚)
我希望应该有一个#[should_abort],我该如何处理?
目前我的明显解决方案是ignore 测试:
#[test]
#[should_panic]
#[ignore = "Ask too much memory"]
【问题讨论】:
-
不应该被认为是 GitHub Actions 实现中的一个 bug 吗?
-
@DenysSéguret 很难说,从技术上讲,我实际上并没有使用这个内存,我只是在我的真实代码中做一个
with_capacity(),这样内存可以保持完全虚拟并且永远不会被操作系统分配.但是没有什么不允许 github 操作报告 oom(当您询问这么多内存时,这是意料之中的)。我想我会在最后删除这个测试,我不喜欢它但我认为这个问题仍然很有趣,我们可以清楚地看到,使用 cargo 测试中止是不容易的。
标签: unit-testing rust abort