-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevent_pool_test.go
More file actions
113 lines (97 loc) · 2.63 KB
/
event_pool_test.go
File metadata and controls
113 lines (97 loc) · 2.63 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cotlib
import (
"bytes"
"context"
"log/slog"
"runtime/debug"
"strings"
"testing"
)
func TestEventPoolReuseOnInvalidXML(t *testing.T) {
// Disable GC to prevent sync.Pool cleanup
pct := debug.SetGCPercent(-1)
defer debug.SetGCPercent(pct)
// Count the number of events we can get from the pool initially
var initialEvents []*Event
for i := 0; i < 10; i++ {
e := getEvent()
initialEvents = append(initialEvents, e)
}
// Return them all to the pool
for _, e := range initialEvents {
ReleaseEvent(e)
}
// Parse invalid XML to trigger error after pool allocation
invalid := []byte("<event><bad></event>")
if _, err := UnmarshalXMLEvent(context.Background(), invalid); err == nil {
t.Fatal("expected error from invalid XML")
}
// Get events from pool; at least one should be from our initial set
// This tests that events are being returned to the pool after failures
var foundReused bool
for i := 0; i < 20; i++ {
e := getEvent()
for _, initial := range initialEvents {
if e == initial {
foundReused = true
break
}
}
ReleaseEvent(e)
if foundReused {
break
}
}
if !foundReused {
t.Error("event was not returned to pool after failure")
}
}
func TestUnmarshalXMLEventCtxLogsError(t *testing.T) {
var buf bytes.Buffer
handler := slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})
logger := slog.New(handler)
ctx := WithLogger(context.Background(), logger)
if _, err := UnmarshalXMLEventCtx(ctx, []byte("<event><bad></event>")); err == nil {
t.Fatal("expected error from invalid XML")
}
logOutput := buf.String()
if !strings.Contains(logOutput, "level=ERROR") {
t.Errorf("expected error log, got: %s", logOutput)
}
}
func TestNewEventPoolReuseOnValidationError(t *testing.T) {
pct := debug.SetGCPercent(-1)
defer debug.SetGCPercent(pct)
// Count the number of events we can get from the pool initially
var initialEvents []*Event
for i := 0; i < 10; i++ {
e := getEvent()
initialEvents = append(initialEvents, e)
}
// Return them all to the pool
for _, e := range initialEvents {
ReleaseEvent(e)
}
if _, err := NewEvent("test", "a-f-G", 95, 0, 0); err == nil {
t.Fatal("expected validation error")
}
// Get events from pool; at least one should be from our initial set
// This tests that events are being returned to the pool after failures
var foundReused bool
for i := 0; i < 20; i++ {
e := getEvent()
for _, initial := range initialEvents {
if e == initial {
foundReused = true
break
}
}
ReleaseEvent(e)
if foundReused {
break
}
}
if !foundReused {
t.Error("event was not returned to pool after validation failure")
}
}