-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager_test.go
More file actions
1263 lines (1148 loc) · 42.3 KB
/
Copy pathmanager_test.go
File metadata and controls
1263 lines (1148 loc) · 42.3 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package luart
// Unit tests for manager.go — Runtime (lazy load, TTL, drop hot reload, LRU,
// memory sizing) and helpers.
// Convention: basic behavior + exception cases + benchmarks (CONTRIBUTING.md).
import (
"context"
"errors"
"fmt"
"sync"
"testing"
"time"
lua "github.com/htcom-code/lua-pure/lua"
)
// ── test fixtures (manager-only) ──
const (
greetV1 = `function run(name) return "v1:" .. name end`
greetV2 = `function run(name) return "v2:" .. name end`
doubleSrc = `function run(n) return tostring(n * 2) end`
// spinSrc has a runaway loop (spin) plus a fast function (ok) sharing one
// pooled State — used to test ExecTimeout abort and clean pooled reuse.
spinSrc = `function spin() while true do end end
function ok(name) return "ok:" .. name end`
)
// newTestManager builds a Runtime with long TTL/interval so the janitor does not
// interfere during tests.
// Since: 2026-06-07
func newTestManager(t *testing.T, loader SourceLoader, maxStates int) *Runtime {
t.Helper()
rt := New(loader, Config{
MaxStates: maxStates,
IdleTTL: time.Hour,
JanitorInterval: time.Hour,
})
t.Cleanup(rt.Close)
return rt
}
// ─────────────────────────────────────────────────────────────────────────────
// Basic behavior
// ─────────────────────────────────────────────────────────────────────────────
// TestLazyLoadFromDB verifies that running the same key many times loads and
// compiles the source only once.
// Since: 2026-06-07
func TestLazyLoadFromDB(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := newTestManager(t, loader, 8)
ctx := context.Background()
for i := 0; i < 10; i++ {
out, err := rt.Run(ctx, "greet", "run", lua.MkString("x"))
if err != nil {
t.Fatalf("iter %d: %v", i, err)
}
if out[0].Str() != "v1:x" {
t.Fatalf("iter %d: got %q", i, out[0].Str())
}
}
if got := loader.Loads(); got != 1 {
t.Fatalf("expected 1 lazy load, got %d", got)
}
if got := rt.cc.CompileCount(); got != 1 {
t.Fatalf("expected 1 compile, got %d", got)
}
}
// TestConcurrentLazyLoadOnce verifies that many goroutines requesting the same
// key for the first time concurrently still load the source only once.
// Since: 2026-06-07
func TestConcurrentLazyLoadOnce(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := newTestManager(t, loader, 16)
var wg sync.WaitGroup
for i := 0; i < 64; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("y")); err != nil {
t.Errorf("run: %v", err)
}
}()
}
wg.Wait()
if got := loader.Loads(); got != 1 {
t.Fatalf("expected exactly 1 load under concurrency, got %d", got)
}
}
// TestIdleEviction verifies the janitor evicts idle pools past their TTL so live
// state count returns to zero.
// Since: 2026-06-07
func TestIdleEviction(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := New(loader, Config{
MaxStates: 8,
IdleTTL: 50 * time.Millisecond,
JanitorInterval: 20 * time.Millisecond,
})
t.Cleanup(rt.Close)
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
// Confirm the pool was actually created via a single load (an immediate
// live>0 assertion would race the fast janitor and be flaky).
if loader.Loads() != 1 {
t.Fatalf("expected the pool to have been created (1 load), got %d", loader.Loads())
}
deadline := time.Now().Add(2 * time.Second)
for {
s := rt.Stats()
if s.Pools == 0 && s.LiveStates == 0 {
break
}
if time.Now().After(deadline) {
t.Fatalf("idle pool not evicted: pools=%d live=%d", s.Pools, s.LiveStates)
}
time.Sleep(10 * time.Millisecond)
}
}
// TestMaxPoolSizeWithLRU verifies many-key requests are served without exceeding
// the global cap (LRU eviction) and that eviction does not trigger recompiles.
// Since: 2026-06-07
func TestMaxPoolSizeWithLRU(t *testing.T) {
loader := NewMapLoader()
keys := []string{"k0", "k1", "k2", "k3", "k4"}
for _, k := range keys {
loader.Set(k, doubleSrc, "v1", "")
}
const maxStates = 2
rt := newTestManager(t, loader, maxStates)
var wg sync.WaitGroup
for i := 0; i < 300; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
k := keys[i%len(keys)]
out, err := rt.Run(context.Background(), k, "run", lua.Int(int64(i)))
if err != nil {
t.Errorf("%s #%d: %v", k, i, err)
return
}
if want := fmt.Sprintf("%d", i*2); out[0].Str() != want {
t.Errorf("%s #%d: want %s got %s", k, i, want, out[0].Str())
}
}(i)
}
wg.Wait()
if live := rt.Stats().LiveStates; live > maxStates {
t.Fatalf("live states %d exceeded cap %d", live, maxStates)
}
if got := rt.cc.CompileCount(); got != int64(len(keys)) {
t.Fatalf("expected %d compiles (one per key), got %d — eviction must not recompile", len(keys), got)
}
}
// TestMemoryBudgetSizing verifies MaxStates is derived as MemoryBudgetBytes /
// measured perState and scales roughly with the budget.
// Since: 2026-06-07
func TestMemoryBudgetSizing(t *testing.T) {
loader := NewMapLoader()
const ratio = 32
small := New(loader, Config{MemoryBudgetBytes: 2 << 20, IdleTTL: time.Hour, JanitorInterval: time.Hour})
defer small.Close()
big := New(loader, Config{MemoryBudgetBytes: 2 * ratio << 20, IdleTTL: time.Hour, JanitorInterval: time.Hour})
defer big.Close()
sm, bg := small.Stats().MaxStates, big.Stats().MaxStates
if sm < 1 {
t.Fatalf("small budget should yield >=1 state, got %d", sm)
}
if bg < sm*4 {
t.Fatalf("budget %dx should yield many more states: small=%d big=%d", ratio, sm, bg)
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Exception / edge cases — safety, failures, lifecycle, accounting
// ─────────────────────────────────────────────────────────────────────────────
// TestUnknownScript verifies running an unregistered key returns an error.
// Since: 2026-06-07
func TestUnknownScript(t *testing.T) {
rt := newTestManager(t, NewMapLoader(), 4)
if _, err := rt.Run(context.Background(), "nope", "run"); err == nil {
t.Fatal("expected error for unknown script")
}
}
// (the swap-based in-flight discard test is replaced by the drop-model
// TestNotifyDropWhileInUse)
// TestRunContextCancelled verifies Run returns the context error when it is
// blocked on back-pressure (capacity exhausted) and the ctx expires.
// Since: 2026-06-07
func TestRunContextCancelled(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := newTestManager(t, loader, 1) // one slot
pool, err := rt.getPool("greet")
if err != nil {
t.Fatal(err)
}
ps, err := rt.acquire(context.Background(), pool) // hold the only slot
if err != nil {
t.Fatal(err)
}
defer rt.release(pool, ps)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
defer cancel()
if _, err := rt.Run(ctx, "greet", "run", lua.MkString("a")); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected DeadlineExceeded under exhausted capacity, got %v", err)
}
}
// TestRunExecTimeout verifies that a runaway (infinite-loop) script is aborted by
// the server-side ExecTimeout hard cap and surfaces a typed DeadlineExceeded.
// Since: 2026-06-07
func TestRunExecTimeout(t *testing.T) {
loader := NewMapLoader()
loader.Set("spin", spinSrc, "v1", "")
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour, ExecTimeout: 50 * time.Millisecond})
t.Cleanup(rt.Close)
start := time.Now()
_, err := rt.Run(context.Background(), "spin", "spin")
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected DeadlineExceeded from ExecTimeout, got %v", err)
}
if elapsed := time.Since(start); elapsed > 2*time.Second {
t.Fatalf("runaway script not aborted promptly: ran %s", elapsed)
}
}
// TestRunCallerCtxCancelDuringExec verifies that, even with ExecTimeout disabled,
// a cancelable ctx the caller passes aborts a running script (Canceled).
// Since: 2026-06-07
func TestRunCallerCtxCancelDuringExec(t *testing.T) {
loader := NewMapLoader()
loader.Set("spin", spinSrc, "v1", "")
rt := newTestManager(t, loader, 4) // ExecTimeout = 0 (disabled)
ctx, cancel := context.WithCancel(context.Background())
go func() {
time.Sleep(30 * time.Millisecond)
cancel()
}()
_, err := rt.Run(ctx, "spin", "spin")
if !errors.Is(err, context.Canceled) {
t.Fatalf("expected Canceled from caller ctx, got %v", err)
}
}
// TestExecTimeoutPooledReuseClean verifies that after an execution times out, the
// pooled State is reset (RemoveContext) so a subsequent call on the same State
// succeeds — i.e. the expired context does not leak into the next reuse.
// Since: 2026-06-07
func TestExecTimeoutPooledReuseClean(t *testing.T) {
loader := NewMapLoader()
loader.Set("spin", spinSrc, "v1", "")
// MaxStates: 1 forces the next Run to reuse the very same pooled State.
rt := New(loader, Config{MaxStates: 1, IdleTTL: time.Hour, JanitorInterval: time.Hour, ExecTimeout: 100 * time.Millisecond})
t.Cleanup(rt.Close)
if _, err := rt.Run(context.Background(), "spin", "spin"); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected timeout on first call, got %v", err)
}
out, err := rt.Run(context.Background(), "spin", "ok", lua.MkString("z"))
if err != nil {
t.Fatalf("reused State after timeout must run cleanly, got %v", err)
}
if out[0].Str() != "ok:z" {
t.Fatalf("got %q, want ok:z", out[0].Str())
}
}
// TestNoExecTimeoutCompletes verifies the zero-overhead path: with ExecTimeout
// disabled and a background ctx, a normal script runs to completion.
// Since: 2026-06-07
func TestNoExecTimeoutCompletes(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := newTestManager(t, loader, 4) // ExecTimeout = 0, context.Background()
out, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a"))
if err != nil {
t.Fatalf("normal run should complete: %v", err)
}
if out[0].Str() != "v1:a" {
t.Fatalf("got %q, want v1:a", out[0].Str())
}
}
// TestCloseSemantics verifies Close closes idle States, subsequent calls return
// ErrClosed, and a double Close is safe.
// Since: 2026-06-07
func TestCloseSemantics(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour})
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
rt.Close()
if live := rt.Stats().LiveStates; live != 0 {
t.Fatalf("Close should close idle states, live=%d", live)
}
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); !errors.Is(err, ErrClosed) {
t.Fatalf("expected ErrClosed after Close, got %v", err)
}
rt.Close() // double Close must be a no-op (no panic)
}
// TestShutdownNoInflight verifies Shutdown returns nil promptly when nothing is
// in flight and leaves no live States.
// Since: 2026-06-07
func TestShutdownNoInflight(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour})
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
if err := rt.Shutdown(context.Background()); err != nil {
t.Fatalf("Shutdown: %v", err)
}
if live := rt.Stats().LiveStates; live != 0 {
t.Fatalf("after Shutdown live=%d, want 0", live)
}
}
// TestShutdownDrainsInflight verifies Shutdown waits for an in-flight State to be
// released, then returns nil with zero live States.
// Since: 2026-06-07
func TestShutdownDrainsInflight(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour})
pool, err := rt.getPool("greet")
if err != nil {
t.Fatal(err)
}
ps, err := rt.acquire(context.Background(), pool) // in flight
if err != nil {
t.Fatal(err)
}
go func() {
time.Sleep(50 * time.Millisecond)
rt.release(pool, ps)
}()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := rt.Shutdown(ctx); err != nil {
t.Fatalf("Shutdown should drain and return nil: %v", err)
}
if live := rt.Stats().LiveStates; live != 0 {
t.Fatalf("after drain live=%d, want 0", live)
}
}
// TestShutdownTimeout verifies Shutdown returns the context error when an
// in-flight State never releases within the deadline (no leak — it closes on its
// eventual release).
// Since: 2026-06-07
func TestShutdownTimeout(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour})
pool, err := rt.getPool("greet")
if err != nil {
t.Fatal(err)
}
ps, err := rt.acquire(context.Background(), pool) // held, never released until cleanup
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
if err := rt.Shutdown(ctx); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected DeadlineExceeded, got %v", err)
}
if live := rt.Stats().LiveStates; live != 1 {
t.Fatalf("in-flight state should remain until released, live=%d", live)
}
rt.release(pool, ps) // closes it (runtime is closed)
if live := rt.Stats().LiveStates; live != 0 {
t.Fatalf("after release live=%d, want 0", live)
}
}
// TestShutdownIdempotent verifies Close then Shutdown is safe and a double
// Shutdown returns nil.
// Since: 2026-06-07
func TestShutdownIdempotent(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "")
rt := New(loader, Config{MaxStates: 4, IdleTTL: time.Hour, JanitorInterval: time.Hour})
if _, err := rt.Run(context.Background(), "greet", "run", lua.MkString("a")); err != nil {
t.Fatal(err)
}
rt.Close()
if err := rt.Shutdown(context.Background()); err != nil {
t.Fatalf("Shutdown after Close: %v", err)
}
if err := rt.Shutdown(context.Background()); err != nil {
t.Fatalf("double Shutdown: %v", err)
}
}
// TestRetryAfterLoaderError verifies that after a transient loader error, a
// retry succeeds once the script exists (failure path).
// Since: 2026-06-07
func TestRetryAfterLoaderError(t *testing.T) {
loader := NewMapLoader()
rt := newTestManager(t, loader, 4)
if _, err := rt.Run(context.Background(), "lazy", "run", lua.MkString("a")); err == nil {
t.Fatal("expected error before script exists")
}
loader.Set("lazy", greetV1, "v1", "")
out, err := rt.Run(context.Background(), "lazy", "run", lua.MkString("a"))
if err != nil {
t.Fatalf("retry after transient error should succeed: %v", err)
}
if out[0].Str() != "v1:a" {
t.Fatalf("got %q", out[0].Str())
}
}
// TestCompileError verifies a malformed script surfaces a compile error through
// the manager path (failure path).
// Since: 2026-06-07
func TestCompileError(t *testing.T) {
loader := NewMapLoader()
loader.Set("bad", `function run( oops`, "v1", "")
rt := newTestManager(t, loader, 4)
if _, err := rt.Run(context.Background(), "bad", "run"); err == nil {
t.Fatal("expected compile error for malformed script")
}
}
// TestLiveCountInvariant verifies that after a workload live == sum of idle
// states across pools, checkedOut is zero, and live <= max (leak / double-count
// detector).
// Since: 2026-06-07
func TestLiveCountInvariant(t *testing.T) {
loader := NewMapLoader()
keys := []string{"a", "b", "c"}
for _, k := range keys {
loader.Set(k, doubleSrc, "v1", "")
}
rt := newTestManager(t, loader, 4)
var wg sync.WaitGroup
for i := 0; i < 200; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
if _, err := rt.Run(context.Background(), keys[i%len(keys)], "run", lua.Int(int64(i))); err != nil {
t.Errorf("#%d: %v", i, err)
}
}(i)
}
wg.Wait()
rt.mu.Lock()
defer rt.mu.Unlock()
sum := 0
for _, p := range rt.pools {
if p.checkedOut != 0 {
t.Fatalf("pool %q has checkedOut=%d after all runs returned", p.key, p.checkedOut)
}
sum += len(p.idle)
}
if rt.live != sum {
t.Fatalf("live count %d != sum of idle states %d (leak or double-count)", rt.live, sum)
}
if rt.live > rt.cfg.MaxStates {
t.Fatalf("live %d exceeds max %d", rt.live, rt.cfg.MaxStates)
}
}
// TestMeasurePerStateBytes verifies measurePerStateBytes reports a plausible
// per-state heap cost (skips when it returns 0 due to GC noise; non-deterministic).
// Since: 2026-06-07
func TestMeasurePerStateBytes(t *testing.T) {
per := measurePerStateBytes(func() *lua.LState { return lua.NewState() }, 8)
if per == 0 {
t.Skip("measurement returned 0 (GC noise); non-deterministic")
}
if per < 1024 {
t.Fatalf("per-state bytes implausibly small: %d", per)
}
}
// ─────────────────────────────────────────────────────────────────────────────
// Notification-driven hot reload (drop-and-reload)
// ─────────────────────────────────────────────────────────────────────────────
// TestNotifyDropsPool verifies a notification with a different version drops the
// key's pool and the next Run reloads the new version.
// Since: 2026-06-07
func TestNotifyDropsPool(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "1.0.0")
rt := newTestManager(t, loader, 8)
ctx := context.Background()
if out, _ := rt.Run(ctx, "greet", "run", lua.MkString("a")); out[0].Str() != "v1:a" {
t.Fatalf("got %q", out[0].Str())
}
if s := rt.Stats(); s.Pools != 1 || s.LiveStates == 0 {
t.Fatalf("expected 1 live pool, got %+v", s)
}
loader.Set("greet", greetV2, "v2", "2.0.0")
rt.Notify("greet", "v2", "2.0.0")
if s := rt.Stats(); s.Pools != 0 || s.LiveStates != 0 {
t.Fatalf("Notify should drop the pool: %+v", s)
}
out, err := rt.Run(ctx, "greet", "run", lua.MkString("a"))
if err != nil {
t.Fatal(err)
}
if out[0].Str() != "v2:a" {
t.Fatalf("after reload want v2:a, got %q", out[0].Str())
}
}
// TestContentHashVersion verifies that, with content-hash versions, identical
// content is idempotent (ignored) and changed content triggers a reload.
// Since: 2026-06-07
func TestContentHashVersion(t *testing.T) {
loader := NewMapLoader()
v1 := HashVersion(greetV1)
loader.Set("greet", greetV1, v1, "1.0.0")
rt := newTestManager(t, loader, 8)
ctx := context.Background()
rt.Run(ctx, "greet", "run", lua.MkString("a"))
rt.Notify("greet", v1, "1.0.0") // same hash → idempotent
if rt.Stats().Pools != 1 {
t.Fatalf("same hash must not drop: %+v", rt.Stats())
}
v2 := HashVersion(greetV2)
if v2 == v1 {
t.Fatal("different content must hash differently")
}
loader.Set("greet", greetV2, v2, "2.0.0")
rt.Notify("greet", v2, "2.0.0")
out, _ := rt.Run(ctx, "greet", "run", lua.MkString("a"))
if out[0].Str() != "v2:a" {
t.Fatalf("want v2:a, got %q", out[0].Str())
}
}
// TestDisplayVersionExposed verifies PoolStats exposes displayVersion and falls
// back to the hash prefix when the loader provides none.
// Since: 2026-06-07
func TestDisplayVersionExposed(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, HashVersion(greetV1), "1.2.3")
dh := HashVersion(doubleSrc)
loader.Set("double", doubleSrc, dh, "") // no displayVersion → fallback
rt := newTestManager(t, loader, 8)
ctx := context.Background()
rt.Run(ctx, "greet", "run", lua.MkString("a"))
rt.Run(ctx, "double", "run", lua.Int(2))
got := map[string]string{}
for _, ps := range rt.PoolStats() {
got[ps.Key] = ps.DisplayVersion
}
if got["greet"] != "1.2.3" {
t.Fatalf("greet display=%q, want 1.2.3", got["greet"])
}
if want := dh[:8]; got["double"] != want {
t.Fatalf("double display=%q, want hash-prefix %q", got["double"], want)
}
}
// TestDisplayVersionLabelOnlyRefresh verifies a same-hash notification that only
// changes displayVersion refreshes the label without dropping (no cold start).
// Since: 2026-06-07
func TestDisplayVersionLabelOnlyRefresh(t *testing.T) {
loader := NewMapLoader()
h := HashVersion(greetV1)
loader.Set("greet", greetV1, h, "1.0.0")
rt := newTestManager(t, loader, 8)
ctx := context.Background()
rt.Run(ctx, "greet", "run", lua.MkString("a"))
liveBefore := rt.Stats().LiveStates
rt.Notify("greet", h, "1.0.1") // same hash, label only
if s := rt.Stats(); s.Pools != 1 || s.LiveStates != liveBefore {
t.Fatalf("label-only refresh must not drop: %+v", s)
}
for _, ps := range rt.PoolStats() {
if ps.Key == "greet" && ps.DisplayVersion != "1.0.1" {
t.Fatalf("label not refreshed: %q", ps.DisplayVersion)
}
}
}
// TestNotifyDropWhileInUse is the ★ core safety test: when a drop notification
// arrives while a State is in use, ① execution completes on the old version,
// ② release closes it instead of pooling, ③ live recovers (no leak), and
// ④ a new Run uses the new-version pool.
// Since: 2026-06-07
func TestNotifyDropWhileInUse(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "1.0.0")
rt := newTestManager(t, loader, 8)
pool, err := rt.getPool("greet")
if err != nil {
t.Fatal(err)
}
ps, err := rt.acquire(context.Background(), pool) // in use (checked-out)
if err != nil {
t.Fatal(err)
}
if ps.version != "v1" {
t.Fatalf("want v1 state, got %q", ps.version)
}
liveAfterAcquire := rt.Stats().LiveStates
// External change notification while in use → drop the pool.
loader.Set("greet", greetV2, "v2", "2.0.0")
rt.Notify("greet", "v2", "2.0.0")
if rt.Stats().Pools != 0 {
t.Fatalf("pool should be removed from map after drop: %+v", rt.Stats())
}
if rt.Stats().LiveStates != liveAfterAcquire {
t.Fatalf("in-use state must NOT be closed by drop: live=%d", rt.Stats().LiveStates)
}
// ① execution completes on the old version
fn := ps.L.GetGlobal("run")
rets, err := ps.L.Call(fn, []lua.Value{lua.MkString("z")}, 1)
if err != nil {
t.Fatal(err)
}
if got := rets[0].Str(); got != "v1:z" {
t.Fatalf("in-flight should complete on old version: got %q", got)
}
// ②③ release → Close instead of pool return, live recovers
rt.release(pool, ps)
if rt.Stats().LiveStates != 0 {
t.Fatalf("released dropped-pool state must be Closed (no leak): live=%d", rt.Stats().LiveStates)
}
// ④ a new Run uses the new-version pool
out, err := rt.Run(context.Background(), "greet", "run", lua.MkString("z"))
if err != nil {
t.Fatal(err)
}
if out[0].Str() != "v2:z" {
t.Fatalf("new run want v2:z, got %q", out[0].Str())
}
}
// TestNotifyIdempotent verifies re-notifying the same version does not change
// state (idempotent).
// Since: 2026-06-07
func TestNotifyIdempotent(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "1.0.0")
rt := newTestManager(t, loader, 8)
rt.Run(context.Background(), "greet", "run", lua.MkString("a"))
before := rt.Stats()
rt.Notify("greet", "v1", "1.0.0")
rt.Notify("greet", "v1", "1.0.0")
if after := rt.Stats(); after.Pools != before.Pools || after.LiveStates != before.LiveStates {
t.Fatalf("idempotent notify changed state: %+v -> %+v", before, after)
}
}
// TestNotifyUnknownKey verifies notifying a key with no pool is a safe no-op.
// Since: 2026-06-07
func TestNotifyUnknownKey(t *testing.T) {
rt := newTestManager(t, NewMapLoader(), 4)
rt.Notify("ghost", "v9", "9.0.0")
rt.NotifyChanges([]Change{{Key: "ghost", Version: "v9", DisplayVersion: "9.0.0"}})
if rt.Stats().Pools != 0 {
t.Fatalf("unknown-key notify should be no-op, pools=%d", rt.Stats().Pools)
}
}
// TestNotifyEmptyVersion verifies an empty/invalid version notification is a
// no-op (no drop) without panicking.
// Since: 2026-06-07
func TestNotifyEmptyVersion(t *testing.T) {
loader := NewMapLoader()
loader.Set("greet", greetV1, "v1", "1.0.0")
rt := newTestManager(t, loader, 8)
rt.Run(context.Background(), "greet", "run", lua.MkString("a"))
before := rt.Stats()
rt.Notify("greet", "", "")
if after := rt.Stats(); after.Pools != before.Pools || after.LiveStates != before.LiveStates {
t.Fatalf("empty-version notify must be no-op: %+v -> %+v", before, after)
}
}
// TestNotifyConcurrentInvariant verifies the accounting invariant (live == sum
// of idle, checkedOut == 0) holds when concurrent Run and drop notifications are
// mixed (no leak/drift, -race).
// Since: 2026-06-07
func TestNotifyConcurrentInvariant(t *testing.T) {
loader := NewMapLoader()
keys := []string{"a", "b", "c"}
for _, k := range keys {
loader.Set(k, doubleSrc, "v1", "")
}
rt := newTestManager(t, loader, 4)
var wg sync.WaitGroup
for i := 0; i < 200; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
// The pool may be mid-drop, so ignore errors (a reload makes the next call succeed).
_, _ = rt.Run(context.Background(), keys[i%len(keys)], "run", lua.Int(int64(i)))
}(i)
}
for i := 0; i < 40; i++ {
// Vary the version each time to force a drop (the loader keeps v1, so a reload recreates a v1 pool).
rt.Notify(keys[i%len(keys)], fmt.Sprintf("nv%d", i), "")
}
wg.Wait()
rt.mu.Lock()
defer rt.mu.Unlock()
sum := 0
for _, p := range rt.pools {
if p.checkedOut != 0 {
t.Fatalf("pool %q checkedOut=%d after wait", p.key, p.checkedOut)
}
sum += len(p.idle)
}
if rt.live != sum {
t.Fatalf("live %d != idle sum %d (leak/drift)", rt.live, sum)
}
}
// TestDefaultLibsSandbox verifies the default library set: the 5.4 sandbox libs
// (utf8, coroutine) are available, while the base globals that compile or run
// arbitrary code/files (load, loadfile, dofile) are removed.
// Since: 2026-06-28
func TestDefaultLibsSandbox(t *testing.T) {
loader := NewMapLoader()
// present: utf8 + coroutine are part of the default sandbox set.
loader.Set("ok", `function run()
assert(utf8.len("hi") == 2)
local co = coroutine.create(function() coroutine.yield(7) end)
local _, v = coroutine.resume(co)
assert(v == 7)
return true
end`, "v1", "")
// removed: each of these globals must be nil under the default sandbox.
for _, g := range []string{"load", "loadfile", "dofile"} {
loader.Set("no_"+g, fmt.Sprintf(`function run() return %s end`, g), "v1", "")
}
rt := newTestManager(t, loader, 4)
if _, err := rt.Run(context.Background(), "ok", "run"); err != nil {
t.Fatalf("utf8/coroutine should be available under default libs: %v", err)
}
for _, g := range []string{"load", "loadfile", "dofile"} {
out, err := rt.Run(context.Background(), "no_"+g, "run")
if err != nil {
t.Fatalf("%s probe errored: %v", g, err)
}
if len(out) != 1 || !out[0].IsNil() {
t.Fatalf("%s should be removed (nil) under default libs, got %v", g, out)
}
}
}
// TestGoCallbackPanicRecovered verifies that a panicking Go callback (reachable
// via Config.Libs) does not escape to the host goroutine: protected mode turns it
// into a catchable error that wraps *lua.GoPanicError, and the same pooled State
// stays reusable for the next Run (VM unwound, not corrupted, no leak).
// Since: 2026-06-28
func TestGoCallbackPanicRecovered(t *testing.T) {
const src = `
function boom() return panicfn() end
function safe() return "ok" end`
loader := NewMapLoader()
loader.Set("k", src, "v1", "")
registerPanic := func(L *lua.LState) {
L.Register("panicfn", func(L *lua.LState) int { panic("kaboom") })
}
rt := New(loader, Config{
MaxStates: 1, // force the same State to be reused across both Runs
IdleTTL: time.Hour,
JanitorInterval: time.Hour,
Libs: []func(*lua.LState){(*lua.LState).OpenBase, registerPanic},
})
t.Cleanup(rt.Close)
// 1) the panicking callback surfaces as an error, not a host panic.
if _, err := rt.Run(context.Background(), "k", "boom"); err == nil {
t.Fatal("expected an error from the panicking callback")
} else {
var gpe *lua.GoPanicError
if !errors.As(err, &gpe) {
t.Fatalf("expected error to wrap *lua.GoPanicError, got %T: %v", err, err)
}
if gpe.Value != "kaboom" {
t.Fatalf("recovered panic value = %v, want kaboom", gpe.Value)
}
}
// 2) the same pooled State is reusable afterwards (unwound, not corrupted).
out, err := rt.Run(context.Background(), "k", "safe")
if err != nil {
t.Fatalf("State should be reusable after a recovered panic: %v", err)
}
if len(out) != 1 || out[0].Str() != "ok" {
t.Fatalf("got %v, want [ok]", out)
}
if s := rt.Stats(); s.LiveStates != 1 {
t.Fatalf("LiveStates = %d, want 1 (no leak)", s.LiveStates)
}
}
const counterSrc = `
counter = 0
function inc() counter = counter + 1; return tostring(counter) end
function usesLib() return string.upper("hi") end`
// TestIsolateGlobals verifies that with Config.IsolateGlobals each call runs under
// a fresh _ENV: a global write does not leak across Runs that reuse the same
// pooled State (counter stays 1), while reads still reach the shared libraries.
// Since: 2026-06-28
func TestIsolateGlobals(t *testing.T) {
loader := NewMapLoader()
loader.Set("k", counterSrc, "v1", "")
rt := New(loader, Config{
MaxStates: 1, // force State reuse so a leak would show
IdleTTL: time.Hour, JanitorInterval: time.Hour,
IsolateGlobals: true,
})
t.Cleanup(rt.Close)
for i := 0; i < 3; i++ {
out, err := rt.Run(context.Background(), "k", "inc")
if err != nil {
t.Fatalf("iter %d: %v", i, err)
}
if out[0].Str() != "1" {
t.Fatalf("iter %d: counter=%s, want 1 (per-call isolation)", i, out[0].Str())
}
}
// reads of undefined globals fall back to the shared libraries via __index.
out, err := rt.Run(context.Background(), "k", "usesLib")
if err != nil {
t.Fatalf("usesLib: %v", err)
}
if out[0].Str() != "HI" {
t.Fatalf("usesLib = %q, want HI (library fallback)", out[0].Str())
}
}
// TestGlobalsPersistWithoutIsolation documents the default mode: globals persist
// on the reused pooled State (counter accumulates), which is exactly what
// IsolateGlobals opts out of.
// Since: 2026-06-28
func TestGlobalsPersistWithoutIsolation(t *testing.T) {
loader := NewMapLoader()
loader.Set("k", counterSrc, "v1", "")
rt := newTestManager(t, loader, 1) // default: IsolateGlobals == false
for i := 1; i <= 3; i++ {
out, err := rt.Run(context.Background(), "k", "inc")
if err != nil {
t.Fatalf("iter %d: %v", i, err)
}
if want := fmt.Sprintf("%d", i); out[0].Str() != want {
t.Fatalf("iter %d: counter=%s, want %s (globals persist on reused State)", i, out[0].Str(), want)
}
}
}
// TestIsolateGlobalsRequiresBaseLib verifies the guard: IsolateGlobals without the
// base library (no setmetatable) surfaces an error instead of panicking.
// Since: 2026-06-28
func TestIsolateGlobalsRequiresBaseLib(t *testing.T) {
loader := NewMapLoader()
loader.Set("k", counterSrc, "v1", "")
rt := New(loader, Config{
MaxStates: 1, IdleTTL: time.Hour, JanitorInterval: time.Hour,
IsolateGlobals: true,
Libs: []func(*lua.LState){(*lua.LState).OpenString}, // no OpenBase
})
t.Cleanup(rt.Close)
if _, err := rt.Run(context.Background(), "k", "inc"); err == nil {
t.Fatal("expected an error when IsolateGlobals is set without the base library")
}
}
// TestMaxInstructions verifies the opcode cap: a runaway pure-Lua loop is aborted
// with ErrInstructionLimit, the same pooled State stays reusable afterwards, a
// short script runs fine under the cap, and the budget re-arms per Run.
// Since: 2026-06-28
func TestMaxInstructions(t *testing.T) {
const src = `
function spin() while true do end end
function add() return tostring(1 + 1) end`
loader := NewMapLoader()
loader.Set("k", src, "v1", "")
rt := New(loader, Config{
MaxStates: 1, IdleTTL: time.Hour, JanitorInterval: time.Hour,
MaxInstructions: 100000,
})
t.Cleanup(rt.Close)
// 1) a runaway loop is capped (does not hang).
if _, err := rt.Run(context.Background(), "k", "spin"); !errors.Is(err, ErrInstructionLimit) {
t.Fatalf("spin: got %v, want ErrInstructionLimit", err)
}
// 2) the same pooled State is reusable, and a short script completes under the cap.
out, err := rt.Run(context.Background(), "k", "add")
if err != nil {
t.Fatalf("add after cap: %v", err)
}
if out[0].Str() != "2" {
t.Fatalf("add = %s, want 2", out[0].Str())
}
// 3) the budget re-arms per Run — a second runaway is capped too.
if _, err := rt.Run(context.Background(), "k", "spin"); !errors.Is(err, ErrInstructionLimit) {
t.Fatalf("spin #2: got %v, want ErrInstructionLimit (budget not re-armed)", err)
}
}
// TestRunWith verifies the borrow API: handle sees the call's results (including
// a table) while the State is still owned, handle's error propagates, and handle
// is not invoked when the script itself errors.
// Since: 2026-06-28
func TestRunWith(t *testing.T) {
const src = `
function make() return {a = 1, b = "two"}, 42 end`
loader := NewMapLoader()
loader.Set("k", src, "v1", "")
rt := newTestManager(t, loader, 2)
// 1) borrow and read a returned table within ownership.
var a, n int64
var b string
var nrets int
err := rt.RunWith(context.Background(), "k", "make", func(L *lua.LState, rets []lua.Value) error {
nrets = len(rets)
tbl := rets[0].AsTable()
a = tbl.GetStr("a").AsInt()
b = tbl.GetStr("b").Str()
n = rets[1].AsInt()
return nil
})
if err != nil {
t.Fatalf("RunWith: %v", err)
}
if nrets != 2 || a != 1 || b != "two" || n != 42 {
t.Fatalf("got nrets=%d a=%d b=%q n=%d", nrets, a, b, n)
}
// 2) handle's error propagates unchanged.
sentinel := errors.New("handle boom")
if err := rt.RunWith(context.Background(), "k", "make", func(L *lua.LState, rets []lua.Value) error {