From 4c25cfc5462c48dd00162cf378028971260357ff Mon Sep 17 00:00:00 2001 From: Lukasz Raczylo Date: Thu, 26 Nov 2026 03:43:01 +0000 Subject: [PATCH] test(yaegi): exercise shared breaker and in-flight under yaegi The probe trips a shared breaker through one interpreted instance over an in-memory store. It checks that a second instance sees the breaker open after one refresh and sees it closed after a success. It also sums in-flight counts across two instances. A mismatch fails make yaegi-check. --- tools/yaegi-check/main.go | 3 + tools/yaegi-check/shared_state.go | 99 +++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 tools/yaegi-check/shared_state.go diff --git a/tools/yaegi-check/main.go b/tools/yaegi-check/main.go index 44f0ba4..f648199 100644 --- a/tools/yaegi-check/main.go +++ b/tools/yaegi-check/main.go @@ -1089,6 +1089,9 @@ func run() error { if err := exerciseRouterFirstByte(i, pkgName); err != nil { return err } + if err := exerciseSharedState(i, pkgName); err != nil { + return err + } return exerciseAdminRedesignStats(i, pkgName) } diff --git a/tools/yaegi-check/shared_state.go b/tools/yaegi-check/shared_state.go new file mode 100644 index 0000000..78011f1 --- /dev/null +++ b/tools/yaegi-check/shared_state.go @@ -0,0 +1,99 @@ +// This file probes the shared breaker and shared in-flight counter under Yaegi. Two loops stand for two pods over +// one in-memory store, so the interpreter runs the loop, the flush, the refresh and the merge, not only the import. +package main + +import ( + "fmt" + "strings" + "time" + + "github.com/traefik/yaegi/interp" +) + +const ( + ssProbeOK = "ok" + // ssOpenBase, ssOpenMax and ssFailWindow are untyped nanosecond counts, so the interpreted call converts them to time.Duration. + ssOpenBase = int64(30 * time.Second) + ssOpenMax = int64(5 * time.Minute) + ssFailWindow = int64(time.Minute) + // ssMaxRequest gives the in-flight tracker two epochs, so a flush that straddles an epoch edge is still read. + ssMaxRequest = int64(5 * time.Minute) +) + +// ssProbeSource returns "ok" or the first mismatch. PKG stands for the interpreted package's name. +// The loops write synchronously so each refresh reads what the previous step wrote. +const ssProbeSource = `func sharedStateProbe() string { + store := PKG.newMemoryStore() + sync := func(f func()) { f() } + la := PKG.newSharedStateLoop(store, sync, nil) + lb := PKG.newSharedStateLoop(store, sync, nil) + defer la.close() + defer lb.close() + + a := PKG.newSharedBreaker(la, "probe", []string{"p"}, 3, %d, %d, %d) + b := PKG.newSharedBreaker(lb, "probe", []string{"p"}, 3, %d, %d, %d) + for i := 0; i < 3; i++ { + a.recordFailure("p") + } + la.refresh() + lb.refresh() + if !b.isOpen("p") { + return "breaker: second instance closed after the first tripped it and one refresh" + } + if b.isOpen("other") { + return "breaker: a trip on one id opened another" + } + b.recordSuccess("p") + lb.refresh() + if b.isOpen("p") { + return "breaker: second instance still open after its own success" + } + + ia := PKG.newSharedInflight(la, []string{"p"}, %d) + ib := PKG.newSharedInflight(lb, []string{"p"}, %d) + e1 := ia.begin("p") + e2 := ia.begin("p") + e3 := ib.begin("p") + // Each refresh flushes its own deltas then reads, so the first instance needs a second pass to see the other's. + la.refresh() + lb.refresh() + la.refresh() + if got := ia.shared("p"); got != 3 { + return fmt.Sprintf("inflight: first instance sees %%d in flight, want 3", got) + } + if got := ib.merged("p", 1); got != 3 { + return fmt.Sprintf("inflight: second instance merged %%d, want 3", got) + } + e1() + e1() + e2() + e3() + la.refresh() + lb.refresh() + la.refresh() + if got := ia.shared("p"); got != 0 { + return fmt.Sprintf("inflight: %%d still in flight after every end, want 0", got) + } + return "ok" +}` + +// exerciseSharedState trips a shared breaker through one interpreted instance, reads it from another, and sums in-flight across two. +func exerciseSharedState(i *interp.Interpreter, pkgName string) error { + src := fmt.Sprintf(strings.ReplaceAll(ssProbeSource, "PKG", pkgName), + ssOpenBase, ssOpenMax, ssFailWindow, ssOpenBase, ssOpenMax, ssFailWindow, ssMaxRequest, ssMaxRequest) + if _, err := i.Eval(`import "fmt"`); err != nil { + return fmt.Errorf("shared state probe: import fmt under yaegi: %w", err) + } + if _, err := i.Eval(src); err != nil { + return fmt.Errorf("shared state probe: declare the probe under yaegi: %w", err) + } + v, err := i.Eval("sharedStateProbe()") + if err != nil { + return fmt.Errorf("shared state probe: run under yaegi: %w", err) + } + if got := v.String(); got != ssProbeOK { + return fmt.Errorf("shared state probe: %s", got) + } + fmt.Println("yaegi-check: shared breaker and shared in-flight matched native Go under the interpreter") + return nil +}