Skip to content

Conversation

@codemob-dev
Copy link
Contributor

Fixes #1286

@Argmaster
Copy link
Collaborator

Argmaster commented Nov 20, 2025

I discourage this change. Sharing allocators, or in more general terms, resources across tests may result in absurd interactions which are very hard to track down.
Instead allocators should be explicitly initialized with a helper function at the start of the test and defer deinited. Since we have our runner, we can hardcore that inside runner so it does init deinit before and after running the test.

Copy link
Member

@IntegratedQuantum IntegratedQuantum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also reroute the stack allocator to the testing_allocator, the testing code should run without any additional setup, and most importantly it should do leak tests without you having to remember to call deinitThreadLocals.

globalArenaAllocator = undefined;
if(globalGpa.deinit() == .leak) {
std.log.err("Memory leak", .{});
if(!builtin.is_test) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function shouldn't be called during testing.

pub var globalGpa = std.heap.GeneralPurposeAllocator(.{.thread_safe = true}){};
pub var handledGpa = ErrorHandlingAllocator.init(globalGpa.allocator());
pub var globalGpa: std.heap.GeneralPurposeAllocator(.{.thread_safe = true}) = if(builtin.is_test) undefined else .init;
pub var handledGpa = ErrorHandlingAllocator.init(if(builtin.is_test) std.testing.allocator else globalGpa.allocator());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion this should be done at the declaration of the interface. Instead I'd propose to have a globally accessible testing allocator that is std.testing.allocator wrapped in the error handling allocator.

@IntegratedQuantum IntegratedQuantum moved this from Easy to Review to In review in PRs to review Nov 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In review

Development

Successfully merging this pull request may close these issues.

Allocators in testing

3 participants