Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle abort in test unit

I want to test one of my function that should panic, but my GitHub action abort() cause even if my code is asking for "only" 2**31 bytes (In my real code the limit is libc::c_int::MAX) that my PC has, GitHub action don't have such memory :p.

#[test]
#[should_panic]
fn reserve_with_max() {
    Vec::with_capacity(usize::MAX);
}

But this fails with:

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

And this stops the testing and report an error, even if this what I expected (either panic or abort).

I didn't find much about this problem:

  • https://github.com/rust-lang/rust/issues/67650
  • https://doc.rust-lang.org/std/alloc/fn.set_alloc_error_hook.html (but nightly)

I expect there should be a #[should_abort], how could I handle this ?

For now my obvious solution is to ignore the test:

#[test]
#[should_panic]
#[ignore = "Ask too much memory"]
like image 405
Stargateur Avatar asked Oct 17 '25 02:10

Stargateur


1 Answers

There is an RFC 2116 that would allow to configure rust to make out of memory panic instead of abort():

[profile.dev]
oom = "panic"

[profile.release]
oom = "panic"

Cons:

  • Not implemented even on nightly #43596
  • It's will work for your use case cause it's an oom abort but it's not a general solution for handle abort() in test.
like image 167
Stargateur Avatar answered Oct 19 '25 17:10

Stargateur