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}}