-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobservability_test.go
More file actions
233 lines (207 loc) · 6.92 KB
/
Copy pathobservability_test.go
File metadata and controls
233 lines (207 loc) · 6.92 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package luart
// Unit tests for observability.go — Metrics/Logger injection.
// Convention: basic behavior + exception cases (CONTRIBUTING.md).
import (
"bytes"
"context"
"log/slog"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
lua "github.com/htcom-code/lua-pure/lua"
)
// spyMetrics counts each event. Safe for concurrent use.
type spyMetrics struct {
compile, build, reuse, evict, drop int64
}
func (s *spyMetrics) OnCompile(string) { atomic.AddInt64(&s.compile, 1) }
func (s *spyMetrics) OnBuild(string) { atomic.AddInt64(&s.build, 1) }
func (s *spyMetrics) OnReuse(string) { atomic.AddInt64(&s.reuse, 1) }
func (s *spyMetrics) OnEvict(string) { atomic.AddInt64(&s.evict, 1) }
func (s *spyMetrics) OnDrop(string) { atomic.AddInt64(&s.drop, 1) }
// spyLogger records messages. Safe for concurrent use.
type spyLogger struct {
mu sync.Mutex
info []string
errs []string
}
func (l *spyLogger) Info(msg string, _ ...any) {
l.mu.Lock()
l.info = append(l.info, msg)
l.mu.Unlock()
}
func (l *spyLogger) Error(msg string, _ ...any) {
l.mu.Lock()
l.errs = append(l.errs, msg)
l.mu.Unlock()
}
func (l *spyLogger) has(list []string, sub string) bool {
l.mu.Lock()
defer l.mu.Unlock()
for _, s := range list {
if strings.Contains(s, sub) {
return true
}
}
return false
}
// TestMetricsEvents verifies compile/build/reuse/drop events fire on the
// expected lifecycle transitions.
// Since: 2026-06-07
func TestMetricsEvents(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "1.0.0")
m := &spyMetrics{}
rt := New(loader, Config{MaxStates: 8, IdleTTL: time.Hour, JanitorInterval: time.Hour, Metrics: m})
t.Cleanup(rt.Close)
ctx := context.Background()
rt.Run(ctx, "greet", "run", lua.MkString("a")) // first: compile + build
rt.Run(ctx, "greet", "run", lua.MkString("b")) // second: reuse idle
if got := atomic.LoadInt64(&m.compile); got != 1 {
t.Fatalf("compile=%d, want 1", got)
}
if got := atomic.LoadInt64(&m.build); got != 1 {
t.Fatalf("build=%d, want 1", got)
}
if got := atomic.LoadInt64(&m.reuse); got != 1 {
t.Fatalf("reuse=%d, want 1", got)
}
loader.Set("greet", greetV2, "v2", "2.0.0")
rt.Notify("greet", "v2", "2.0.0")
if got := atomic.LoadInt64(&m.drop); got != 1 {
t.Fatalf("drop=%d, want 1", got)
}
}
// TestMetricsEviction verifies an LRU eviction at the cap emits OnEvict.
// Since: 2026-06-07
func TestMetricsEviction(t *testing.T) {
loader := NewMapLoader()
loader.Set("a", doubleSrc, "v1", "")
loader.Set("b", doubleSrc, "v1", "")
m := &spyMetrics{}
rt := New(loader, Config{MaxStates: 1, IdleTTL: time.Hour, JanitorInterval: time.Hour, Metrics: m})
t.Cleanup(rt.Close)
ctx := context.Background()
if _, err := rt.Run(ctx, "a", "run", lua.Int(1)); err != nil { // build a, then a idle
t.Fatal(err)
}
if _, err := rt.Run(ctx, "b", "run", lua.Int(2)); err != nil { // cap=1 → evict a, build b
t.Fatal(err)
}
if got := atomic.LoadInt64(&m.evict); got < 1 {
t.Fatalf("evict=%d, want >=1", got)
}
}
// TestLoggerEvents verifies pool-loaded and pool-dropped are logged at info, and
// a load failure is logged at error.
// Since: 2026-06-07
func TestLoggerEvents(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "1.0.0")
lg := &spyLogger{}
rt := New(loader, Config{MaxStates: 8, IdleTTL: time.Hour, JanitorInterval: time.Hour, Logger: lg})
t.Cleanup(rt.Close)
ctx := context.Background()
rt.Run(ctx, "greet", "run", lua.MkString("a"))
if !lg.has(lg.info, "pool loaded") {
t.Fatal("expected a 'pool loaded' info log")
}
loader.Set("greet", greetV2, "v2", "2.0.0")
rt.Notify("greet", "v2", "2.0.0")
if !lg.has(lg.info, "pool dropped") {
t.Fatal("expected a 'pool dropped' info log")
}
// Failure path: unknown key → load error logged.
if _, err := rt.Run(ctx, "ghost", "run"); err == nil {
t.Fatal("expected error for unknown script")
}
if !lg.has(lg.errs, "load failed") {
t.Fatal("expected a 'load failed' error log")
}
}
// TestNoopDefaults verifies that without Metrics/Logger the Runtime works and
// does not panic (defaults are no-ops).
// Since: 2026-06-07
func TestNoopDefaults(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := newTestManager(t, loader, 4) // no Metrics/Logger configured
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
}
// TestSlogLoggerAdapter verifies NewSlogLogger routes events into a *slog.Logger.
// Since: 2026-06-07
func TestSlogLoggerAdapter(t *testing.T) {
var buf bytes.Buffer
lg := NewSlogLogger(slog.New(slog.NewTextHandler(&buf, nil)))
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "1.0.0")
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour, Logger: lg})
t.Cleanup(rt.Close)
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
if !strings.Contains(buf.String(), "pool loaded") {
t.Fatalf("slog output missing 'pool loaded': %q", buf.String())
}
}
// spyTrace counts trace stages. Safe for concurrent use.
type spyTrace struct {
mu sync.Mutex
counts map[string]int
}
func (s *spyTrace) hook(stage, _ string, _ time.Duration) {
s.mu.Lock()
s.counts[stage]++
s.mu.Unlock()
}
func (s *spyTrace) count(stage string) int {
s.mu.Lock()
defer s.mu.Unlock()
return s.counts[stage]
}
// TestTraceStages verifies that the first Run traces load/compile/build/acquire/
// execute/release, and a second Run (idle reuse) traces acquire/execute/release
// again without re-tracing load/compile/build.
// Since: 2026-06-07
func TestTraceStages(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
st := &spyTrace{counts: map[string]int{}}
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour, Trace: st.hook})
t.Cleanup(rt.Close)
ctx := context.Background()
if _, err := rt.Run(ctx, "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
for _, stage := range []string{"load", "compile", "build", "acquire", "execute", "release"} {
if st.count(stage) < 1 {
t.Fatalf("stage %q was not traced on first run", stage)
}
}
if _, err := rt.Run(ctx, "greet", "run", lua.MkString("b")); err != nil { // reuse idle
t.Fatal(err)
}
if got := st.count("build"); got != 1 {
t.Fatalf("build should stay 1 on reuse, got %d", got)
}
if got := st.count("load"); got != 1 {
t.Fatalf("load should stay 1 on reuse, got %d", got)
}
if st.count("acquire") < 2 || st.count("execute") < 2 || st.count("release") < 2 {
t.Fatalf("acquire/execute/release should fire each run: %+v", st.counts)
}
}
// TestTraceOff verifies a nil Trace adds no behavior change (no panic).
// Since: 2026-06-07
func TestTraceOff(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := newTestManager(t, loader, 4) // Trace unset
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
}