From 7d212516051bc83ee96b0abf06f1001a51aed77e Mon Sep 17 00:00:00 2001 From: Zhero & Rin Date: Tue, 1 Dec 2026 14:22:57 +0800 Subject: [PATCH] =?UTF-8?q?test(reconciliation):=20=E5=9B=BA=E5=AE=9A?= =?UTF-8?q?=E7=A1=AE=E8=AE=A4=E9=98=B6=E6=AE=B5=E5=B9=B6=E5=8F=91=E4=B8=8A?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 覆盖 acceptance #17:确认并发不超过配置值。 --- internal/reconciliation/service_test.go | 50 +++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/internal/reconciliation/service_test.go b/internal/reconciliation/service_test.go index ed90179..462cfde 100644 --- a/internal/reconciliation/service_test.go +++ b/internal/reconciliation/service_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "strings" + "sync" "testing" "time" @@ -276,6 +277,55 @@ func TestR8DoesNotFlagAdoptedDuplicatesWithDistinctPublications(t *testing.T) { } } +// The scan phase is serial, but confirmation may fan out up to the configured +// bound and never past it (acceptance #17). +func TestConfirmAllRespectsConfiguredConcurrency(t *testing.T) { + remote := &concurrencyRemote{} + service, err := New(Config{ + Store: &fakeReconciliationStore{}, Remote: remote, ConfirmConcurrency: 2, + Now: time.Now, + }) + if err != nil { + t.Fatal(err) + } + results := service.confirmAll(context.Background(), "kb-1", []string{"doc-1", "doc-2", "doc-3", "doc-4"}) + if len(results) != 4 { + t.Fatalf("results = %d", len(results)) + } + for id, result := range results { + if !result.notFound { + t.Fatalf("%s = %#v", id, result) + } + } + remote.mu.Lock() + peak := remote.maxInFlight + remote.mu.Unlock() + if peak != 2 { + t.Fatalf("confirm in-flight peak = %d, want 2", peak) + } +} + +type concurrencyRemote struct { + fakeReconciliationRemote + mu sync.Mutex + inFlight int + maxInFlight int +} + +func (r *concurrencyRemote) GetKnowledge(context.Context, string) (weknora.Knowledge, error) { + r.mu.Lock() + r.inFlight++ + if r.inFlight > r.maxInFlight { + r.maxInFlight = r.inFlight + } + r.mu.Unlock() + time.Sleep(20 * time.Millisecond) + r.mu.Lock() + r.inFlight-- + r.mu.Unlock() + return weknora.Knowledge{}, &weknora.HTTPError{StatusCode: 404} +} + func TestExecuteRunFailsWholeBatchWithoutFindings(t *testing.T) { now := time.Date(2026, 12, 1, 12, 0, 0, 0, time.UTC) store := &fakeReconciliationStore{run: governance.ReconciliationRun{ID: "run-1", KBID: "kb-1", State: "running", StartedAt: now}}