Files
wehub-resource-sync a06f331eb8
CI / benchmark (push) Has been skipped
install-script / posix-syntax (push) Successful in 6m1s
CI / build-onnx (push) Failing after 6m43s
init-smoke / dry-run (push) Failing after 15m57s
security / govulncheck (push) Has been cancelled
security / trivy-fs (push) Has been cancelled
CI / test (1.26, ubuntu-latest) (push) Has been cancelled
Scorecard supply-chain security / Scorecard analysis (push) Has been cancelled
CI / test (1.26, macos-latest) (push) Has been cancelled
CI / build-windows (push) Has been cancelled
CI / lint (push) Has been cancelled
install-script / powershell-syntax (push) Has been cancelled
install-script / install (macos-14) (push) Has been cancelled
install-script / install (ubuntu-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:33:42 +08:00

105 lines
2.9 KiB
Go

package indexer
import (
"testing"
"github.com/zzet/gortex/internal/parser/crashpool"
)
func TestEffectiveExtractBudget(t *testing.T) {
cases := []struct {
name string
base int
bytes int
want int
}{
{"disabled base zero", 0, 100000, 0},
{"disabled base negative", -1, 100000, -1},
{"empty file gets base", 100, 0, 100},
{"small file adds proportional ms", 100, 5 * extractBudgetBytesPerMs, 105},
{"one unit", 100, extractBudgetBytesPerMs, 101},
{"sub-unit truncates", 100, extractBudgetBytesPerMs - 1, 100},
{"huge file capped at multiple", 100, 1 << 30, 100 * extractBudgetMaxMultiple},
{"exactly at cap boundary", 100, (extractBudgetMaxMultiple - 1) * 100 * extractBudgetBytesPerMs, 100 * extractBudgetMaxMultiple},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if got := effectiveExtractBudget(c.base, c.bytes); got != c.want {
t.Errorf("effectiveExtractBudget(%d, %d) = %d, want %d", c.base, c.bytes, got, c.want)
}
})
}
}
func TestExtractFileRetry(t *testing.T) {
t.Run("crash then success retries once on a clean worker", func(t *testing.T) {
calls := 0
res := submitWithRetry(func() crashpool.Result {
calls++
if calls == 1 {
return crashpool.Result{Crashed: true, Err: "worker died"}
}
return crashpool.Result{Nodes: nil, Edges: nil}
})
if calls != 2 {
t.Fatalf("expected exactly one retry (2 submits), got %d", calls)
}
if res.Bad() {
t.Errorf("retry on a clean worker should have succeeded, got %+v", res)
}
})
t.Run("panic then success retries once", func(t *testing.T) {
calls := 0
res := submitWithRetry(func() crashpool.Result {
calls++
if calls == 1 {
return crashpool.Result{Panicked: true, Err: "extractor panic"}
}
return crashpool.Result{}
})
if calls != 2 {
t.Fatalf("expected 2 submits, got %d", calls)
}
if res.Bad() {
t.Errorf("retry should have succeeded, got %+v", res)
}
})
t.Run("clean parse does not retry", func(t *testing.T) {
calls := 0
submitWithRetry(func() crashpool.Result {
calls++
return crashpool.Result{}
})
if calls != 1 {
t.Fatalf("a clean parse must not retry; got %d submits", calls)
}
})
t.Run("persistent crash retries exactly once then gives up", func(t *testing.T) {
calls := 0
res := submitWithRetry(func() crashpool.Result {
calls++
return crashpool.Result{Crashed: true, Err: "always crashes"}
})
if calls != 2 {
t.Fatalf("a persistently bad file must retry exactly once (2 submits), got %d", calls)
}
if !res.Crashed {
t.Errorf("a persistently crashing file must surface the crash, got %+v", res)
}
})
t.Run("plain extraction error is not retried", func(t *testing.T) {
calls := 0
submitWithRetry(func() crashpool.Result {
calls++
return crashpool.Result{Err: "unsupported language"}
})
if calls != 1 {
t.Fatalf("a deterministic extraction error must not retry; got %d submits", calls)
}
})
}