Add Implicit @pool for Each Unit Test Invocation (#2654)

* Add Implicit `@pool` for Each Unit Test Invocation
This commit is contained in:
Zack Puhl
2025-12-17 09:08:02 -05:00
committed by GitHub
parent 00c1210625
commit 8055c340f6
3 changed files with 40 additions and 8 deletions

View File

@@ -27,6 +27,8 @@ struct TestContext
bool is_in_panic; bool is_in_panic;
bool is_quiet_mode; bool is_quiet_mode;
bool is_no_capture; bool is_no_capture;
bool sort;
bool check_leaks;
String current_test_name; String current_test_name;
TestFn setup_fn; TestFn setup_fn;
TestFn teardown_fn; TestFn teardown_fn;
@@ -185,8 +187,6 @@ fn void unmute_output(bool has_error) @local
fn bool run_tests(String[] args, TestUnit[] tests) @private fn bool run_tests(String[] args, TestUnit[] tests) @private
{ {
usz max_name; usz max_name;
bool sort_tests = true;
bool check_leaks = true;
if (!tests.len) if (!tests.len)
{ {
io::printn("There are no test units to run."); io::printn("There are no test units to run.");
@@ -204,6 +204,8 @@ $endif
{ {
.assert_print_backtrace = true, .assert_print_backtrace = true,
.breakpoint_on_assert = false, .breakpoint_on_assert = false,
.sort = true,
.check_leaks = true,
.log_level = LogPriority.ERROR, .log_level = LogPriority.ERROR,
.test_filter = "", .test_filter = "",
.has_ansi_codes = terminal_has_ansi_codes(), .has_ansi_codes = terminal_has_ansi_codes(),
@@ -218,9 +220,9 @@ $endif
case "--test-breakpoint": case "--test-breakpoint":
context.breakpoint_on_assert = true; context.breakpoint_on_assert = true;
case "--test-nosort": case "--test-nosort":
sort_tests = false; context.sort = false;
case "--test-noleak": case "--test-noleak":
check_leaks = false; context.check_leaks = false;
case "--test-nocapture": case "--test-nocapture":
case "--test-show-output": case "--test-show-output":
context.is_no_capture = true; context.is_no_capture = true;
@@ -261,7 +263,7 @@ $endif
test_context = &context; test_context = &context;
log::set_priority_all(test_context.log_level); log::set_priority_all(test_context.log_level);
if (sort_tests) if (context.sort)
{ {
quicksort(tests, &cmp_test_unit); quicksort(tests, &cmp_test_unit);
} }
@@ -321,14 +323,17 @@ $endif
{ {
mute_output(); mute_output();
mem.clear(); mem.clear();
if (check_leaks) allocator::thread_allocator = &mem; if (context.check_leaks) allocator::thread_allocator = &mem;
unit.func(); @pool()
{
unit.func();
};
// track cleanup that may take place in teardown_fn // track cleanup that may take place in teardown_fn
if (context.teardown_fn) if (context.teardown_fn)
{ {
context.teardown_fn(); context.teardown_fn();
} }
if (check_leaks) allocator::thread_allocator = context.stored.allocator; if (context.check_leaks) allocator::thread_allocator = context.stored.allocator;
unmute_output(false); // all good, discard output unmute_output(false); // all good, discard output
if (mem.has_leaks()) if (mem.has_leaks())

View File

@@ -22,6 +22,7 @@
- Hard limit of 127 characters for identifiers. - Hard limit of 127 characters for identifiers.
- `$$LINE` would sometimes yield the incorrect format. - `$$LINE` would sometimes yield the incorrect format.
- Fix error message when a method has the wrong type for the first argument. - Fix error message when a method has the wrong type for the first argument.
- Unit tests allocating too much `tmem` without `@pool` would cause errors in unrelated tests. #2654
### Stdlib changes ### Stdlib changes
- Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads.

View File

@@ -0,0 +1,26 @@
module runtime_test_tests @test;
import std::core::runtime @public;
import std::core::mem::allocator @public;
import std::math::random;
fn void tmem_shouldnt_leak_if_no_pool()
{
for (usz i = 0; i < 256; i++)
{
char[] my_arr = mem::talloc_array(char, 1024);
my_arr[..] = (char[*]){ [0..1023] = 0xA5 }[..];
}
}
fn void ensure_leak_check_works()
{
if (!(runtime::test_context.check_leaks)) return;
char[] my_arr = mem::alloc_array(char, 1024);
test::@check(((TrackingAllocator*)allocator::thread_allocator).has_leaks());
mem::free(my_arr);
test::@check(!((TrackingAllocator*)allocator::thread_allocator).has_leaks());
}