1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Process-global allocation-ceiling tests (#899).
//!
//! # Why these live in their own test binary
//!
//! [`set_alloc_byte_limit`] configures a **process-global** ceiling: that is its
//! contract, so a test that pins it to a small value necessarily lowers it for
//! every thread in the process.
//!
//! Inside `--lib`, that is unsafe at any level of annotation. `#[serial]`
//! (`serial_test`) only excludes other `#[serial]` tests; the thousands of
//! unannotated tests in the same binary keep running in parallel and are judged
//! against whatever ceiling happens to be installed. On 2026-07-25 this
//! surfaced as a reproducible flake: with a 4096-byte ceiling pinned by
//! `alloc_guard`, an unrelated agent-memory test failed on a legitimate
//! 1_600_000-byte buffer with an `AllocationFailed` naming a limit it never
//! configured. The test that failed varied run to run; only the full suite
//! showed it, and each test passed in isolation.
//!
//! Process-global state needs *process* isolation. Cargo runs every integration
//! test target as its own process, so these tests get a private global here and
//! cannot perturb — or be perturbed by — the main suite. The scoped, thread-local
//! counterpart (`with_alloc_byte_limit`) is exercised in the `--lib` tests, where
//! it is safe by construction.
use serial_test::serial;
use std::alloc::Layout;
use velesdb_core::alloc_guard::{
alloc_byte_limit, set_alloc_byte_limit, AllocGuard, DEFAULT_ALLOC_BYTE_LIMIT,
};
/// The ceiling is configurable process-wide; `0` restores the default.
#[test]
#[serial]
fn test_set_alloc_byte_limit_roundtrip() {
let original = alloc_byte_limit();
set_alloc_byte_limit(8192);
assert_eq!(alloc_byte_limit(), 8192);
assert!(AllocGuard::new(Layout::from_size_align(16384, 8).unwrap()).is_none());
assert!(AllocGuard::new(Layout::from_size_align(4096, 8).unwrap()).is_some());
// `0` means "no override" → back to default.
set_alloc_byte_limit(0);
assert_eq!(alloc_byte_limit(), DEFAULT_ALLOC_BYTE_LIMIT);
// Restore whatever the harness started with.
set_alloc_byte_limit(original);
}
/// The global ceiling applies to **every** thread — that is the whole point of
/// `set_alloc_byte_limit`, and what distinguishes it from the thread-local
/// `with_alloc_byte_limit` scope.
#[test]
#[serial]
fn test_global_limit_applies_to_worker_threads() {
let original = alloc_byte_limit();
set_alloc_byte_limit(8192);
let rejected_on_worker = std::thread::spawn(|| {
AllocGuard::new(Layout::from_size_align(16384, 8).unwrap()).is_none()
})
.join()
.expect("worker thread panicked");
set_alloc_byte_limit(original);
assert!(
rejected_on_worker,
"an operator-configured ceiling must bound allocations on worker threads \
(rayon index builds, tokio blocking pool), not just the caller's thread"
);
}