Revalidate admin roles from the database and serialize report generation registration with owner-scoped deletion. Refs #9 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
)
|
|
|
|
// The owner is part of the work identity: a report/message identifier alone
|
|
// must never select another tenant's cancellation function.
|
|
type ownerScopedReportManager interface {
|
|
Register(ownerID, operationID string, cancel context.CancelFunc)
|
|
Cancel(ownerID, operationID string)
|
|
Remove(ownerID, operationID string)
|
|
}
|
|
|
|
func scopedManager(t *testing.T) ownerScopedReportManager {
|
|
t.Helper()
|
|
manager, ok := any(NewReportManager()).(ownerScopedReportManager)
|
|
if !ok {
|
|
t.Fatal("ReportManager has no owner-scoped Register/Cancel/Remove contract")
|
|
}
|
|
return manager
|
|
}
|
|
|
|
func TestReportManagerScopesEqualOperationIDsByOwner(t *testing.T) {
|
|
manager := scopedManager(t)
|
|
var cancelledA, cancelledB atomic.Bool
|
|
manager.Register("owner-a", "same-report-id", func() { cancelledA.Store(true) })
|
|
manager.Register("owner-b", "same-report-id", func() { cancelledB.Store(true) })
|
|
|
|
manager.Cancel("owner-a", "same-report-id")
|
|
if !cancelledA.Load() {
|
|
t.Fatal("cancel for owner-a did not cancel owner-a work")
|
|
}
|
|
if cancelledB.Load() {
|
|
t.Fatal("cancel for owner-a cancelled owner-b work with the same operation ID")
|
|
}
|
|
manager.Cancel("owner-b", "same-report-id")
|
|
if !cancelledB.Load() {
|
|
t.Fatal("cancel for owner-b did not cancel owner-b work")
|
|
}
|
|
}
|
|
|
|
func TestReportManagerConcurrentOwnerScopedLifecycle(t *testing.T) {
|
|
manager := scopedManager(t)
|
|
var cancelledA, cancelledB atomic.Int64
|
|
var wg sync.WaitGroup
|
|
for _, owner := range []string{"owner-a", "owner-b"} {
|
|
owner := owner
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for range 250 {
|
|
cancel := func() {
|
|
if owner == "owner-a" {
|
|
cancelledA.Add(1)
|
|
} else {
|
|
cancelledB.Add(1)
|
|
}
|
|
}
|
|
manager.Register(owner, "same-report-id", cancel)
|
|
manager.Cancel(owner, "same-report-id")
|
|
manager.Remove(owner, "same-report-id")
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
if cancelledA.Load() == 0 || cancelledB.Load() == 0 {
|
|
t.Fatalf("both owners must complete cancellation; owner-a=%d owner-b=%d", cancelledA.Load(), cancelledB.Load())
|
|
}
|
|
}
|
|
|
|
// Exercise the current manager's mutex under the race detector even before its
|
|
// owner-aware API exists; tenant-isolation assertions live above.
|
|
func TestReportManagerConcurrentRegisterCancelRemoveIsRaceSafe(t *testing.T) {
|
|
manager := NewReportManager()
|
|
var wg sync.WaitGroup
|
|
for range 8 {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
for range 250 {
|
|
manager.Register("owner", "shared-id", func() {})
|
|
manager.Cancel("owner", "shared-id")
|
|
manager.Remove("owner", "shared-id")
|
|
}
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
}
|