-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnative_alloc_telemetry_test.go
More file actions
48 lines (42 loc) · 1.59 KB
/
native_alloc_telemetry_test.go
File metadata and controls
48 lines (42 loc) · 1.59 KB
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
package purejson
import "testing"
// TestUntrackedFreeCountStaysZero guards the public NativeAllocStats.UntrackedFreeCount
// counter: a healthy parse/close cycle must never increment it. The counter is incremented
// in native code when remove_allocation receives a pointer that is not in the live
// allocations map (double free, stray free, bookkeeping bug). Making this assertion explicit
// means any future change that introduces such a path fails this test instead of silently
// bleeding into production telemetry.
func TestUntrackedFreeCountStaysZero(t *testing.T) {
library, err := activeLibrary()
if err != nil {
t.Fatalf("activeLibrary(): %v", err)
}
if err := wrapStatus(library.bindings.NativeAllocStatsReset()); err != nil {
t.Fatalf("NativeAllocStatsReset(): %v", err)
}
for i := 0; i < 16; i++ {
parser := mustNewParser(t)
doc, err := parser.Parse([]byte(`{"a":1,"b":[true,false,null],"c":"hello"}`))
if err != nil {
_ = parser.Close()
t.Fatalf("Parse() error = %v", err)
}
if err := doc.Close(); err != nil {
_ = parser.Close()
t.Fatalf("doc.Close() error = %v", err)
}
if err := parser.Close(); err != nil {
t.Fatalf("parser.Close() error = %v", err)
}
}
stats, rc := library.bindings.NativeAllocStatsSnapshot()
if err := wrapStatus(rc); err != nil {
t.Fatalf("NativeAllocStatsSnapshot(): %v", err)
}
if stats.UntrackedFreeCount != 0 {
t.Fatalf("UntrackedFreeCount = %d, want 0 (indicates stray free in native telemetry)", stats.UntrackedFreeCount)
}
if stats.AllocCount == 0 {
t.Fatalf("AllocCount = 0, want > 0 (telemetry path not exercised)")
}
}