✓
Passing This code compiles and runs correctly.
Code
// Test: Budget exhaustion with open handles triggers auto-discharge
// Even when exhausted, we clean up resources before returning
~import "$std/runtime"
~import "$std/io"
const std = @import("std");
~pub event open { path: []const u8 }
| opened { handle: []const u8[opened!] }
~proc open {
std.debug.print("opened {s}\n", .{path});
return .{ .opened = .{ .handle = "file_1" } };
}
~pub event close { handle: []const u8[!opened] }
| closed {}
~proc close {
std.debug.print("auto-closing file\n", .{});
return .{ .closed = .{} };
}
~pub event expensive_read { handle: []const u8[opened] }
| read { data: []const u8 }
~proc expensive_read {
return .{ .read = .{ .data = "file contents" } };
}
~std.runtime:register(scope: "test") {
open(10)
close(1)
expensive_read(100) // Very expensive!
}
const TEST_SOURCE = "~open(path: \"x.txt\")\n| opened f |> expensive_read(handle: f.handle)\n| read r |> result { r.data }";
// Open file (10), then try expensive read (100) with only 50 budget
// Should exhaust, but still auto-discharge the open file
~std.runtime:run(source: TEST_SOURCE, scope: "test", budget: 50, auto_discharge: true)
| result _ |> std.io:println(text: "FAIL: should have exhausted")
| exhausted e |> std.io:print.ln("PASS: exhausted with {{ e.used:d }} used, last event: {{ e.last_event:s }}")
| parse_error _ |> std.io:println(text: "PARSE ERROR")
| validation_error _ |> std.io:println(text: "VALIDATION ERROR")
| event_denied _ |> std.io:println(text: "EVENT DENIED")
| dispatch_error _ |> std.io:println(text: "DISPATCH ERROR")
| scope_not_found _ |> std.io:println(text: "SCOPE NOT FOUND")
Expected Output
opened x.txt
auto-closing file
[AUTO-DISCHARGE] Invoked 'close' for handle 'file_1' [main:opened]
PASS: exhausted with 10 used, last event: open
Test Configuration
MUST_RUN