168 lines
4.9 KiB
Go
168 lines
4.9 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"droboticflow/internal/domain"
|
|
)
|
|
|
|
func TestRunSummaryAggregateCacheKeyIgnoresPageOnlyFields(t *testing.T) {
|
|
first := newRunSummaryAggregateCacheKey("org-a", domain.RunSummaryQuery{
|
|
Limit: 25,
|
|
Status: domain.RunStatusRunning,
|
|
Cursor: &domain.RunSummaryCursor{CreatedAt: "2026-07-10T03:00:00Z", ID: "run-3"},
|
|
Search: "vision",
|
|
})
|
|
second := newRunSummaryAggregateCacheKey("org-a", domain.RunSummaryQuery{
|
|
Limit: 100,
|
|
Status: domain.RunStatusFailed,
|
|
Cursor: &domain.RunSummaryCursor{CreatedAt: "2026-07-10T01:00:00Z", ID: "run-1"},
|
|
Search: "vision",
|
|
})
|
|
if first != second {
|
|
t.Fatalf("page-only fields should share aggregate cache key: first=%#v second=%#v", first, second)
|
|
}
|
|
|
|
filtered := newRunSummaryAggregateCacheKey("org-a", domain.RunSummaryQuery{Search: "audio"})
|
|
if first == filtered {
|
|
t.Fatalf("different aggregate filters should not share cache key: first=%#v filtered=%#v", first, filtered)
|
|
}
|
|
}
|
|
|
|
func TestRunSummaryAggregateCacheKeyPartitionsByMode(t *testing.T) {
|
|
prodKey := newRunSummaryAggregateCacheKey("org-a", domain.RunSummaryQuery{
|
|
Mode: domain.RunModeProduction,
|
|
Search: "vision",
|
|
})
|
|
debugKey := newRunSummaryAggregateCacheKey("org-a", domain.RunSummaryQuery{
|
|
Mode: domain.RunModeDebug,
|
|
Search: "vision",
|
|
})
|
|
allKey := newRunSummaryAggregateCacheKey("org-a", domain.RunSummaryQuery{
|
|
Mode: "",
|
|
Search: "vision",
|
|
})
|
|
|
|
if prodKey == debugKey {
|
|
t.Fatalf("production and debug queries must have distinct cache keys: prodKey=%#v debugKey=%#v", prodKey, debugKey)
|
|
}
|
|
if prodKey == allKey {
|
|
t.Fatalf("production and all queries must have distinct cache keys: prodKey=%#v allKey=%#v", prodKey, allKey)
|
|
}
|
|
if prodKey.Mode != domain.RunModeProduction {
|
|
t.Fatalf("prodKey.Mode = %s, want %s", prodKey.Mode, domain.RunModeProduction)
|
|
}
|
|
if debugKey.Mode != domain.RunModeDebug {
|
|
t.Fatalf("debugKey.Mode = %s, want %s", debugKey.Mode, domain.RunModeDebug)
|
|
}
|
|
}
|
|
|
|
func TestRunSummaryAggregateCacheCachesClonesAndExpires(t *testing.T) {
|
|
now := time.Date(2026, time.July, 11, 1, 0, 0, 0, time.UTC)
|
|
cache := newRunSummaryAggregateCache(5*time.Second, 8)
|
|
cache.now = func() time.Time { return now }
|
|
key := runSummaryAggregateCacheKey{OrganizationID: "org-a"}
|
|
loads := 0
|
|
loader := func(context.Context) (runSummaryAggregates, error) {
|
|
loads++
|
|
return runSummaryAggregates{
|
|
Total: 3,
|
|
StatusCounts: map[domain.RunStatus]int64{
|
|
domain.RunStatusRunning: 1,
|
|
domain.RunStatusFailed: 2,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
first, err := cache.getOrLoad(context.Background(), key, loader)
|
|
if err != nil {
|
|
t.Fatalf("first cache load: %v", err)
|
|
}
|
|
first.StatusCounts[domain.RunStatusRunning] = 99
|
|
|
|
second, err := cache.getOrLoad(context.Background(), key, loader)
|
|
if err != nil {
|
|
t.Fatalf("cached load: %v", err)
|
|
}
|
|
if loads != 1 {
|
|
t.Fatalf("loader calls before expiry = %d, want 1", loads)
|
|
}
|
|
if second.StatusCounts[domain.RunStatusRunning] != 1 {
|
|
t.Fatalf("cached value was mutated through caller map: %#v", second.StatusCounts)
|
|
}
|
|
|
|
now = now.Add(5 * time.Second)
|
|
if _, err := cache.getOrLoad(context.Background(), key, loader); err != nil {
|
|
t.Fatalf("expired cache reload: %v", err)
|
|
}
|
|
if loads != 2 {
|
|
t.Fatalf("loader calls after expiry = %d, want 2", loads)
|
|
}
|
|
}
|
|
|
|
func TestRunSummaryAggregateCacheCoalescesConcurrentLoads(t *testing.T) {
|
|
cache := newRunSummaryAggregateCache(time.Minute, 8)
|
|
key := runSummaryAggregateCacheKey{OrganizationID: "org-a"}
|
|
started := make(chan struct{})
|
|
release := make(chan struct{})
|
|
var startOnce sync.Once
|
|
var loads atomic.Int32
|
|
loader := func(context.Context) (runSummaryAggregates, error) {
|
|
loads.Add(1)
|
|
startOnce.Do(func() { close(started) })
|
|
<-release
|
|
return runSummaryAggregates{Total: 1, StatusCounts: emptyRunStatusCounts()}, nil
|
|
}
|
|
|
|
const callers = 8
|
|
var waitGroup sync.WaitGroup
|
|
errors := make(chan error, callers)
|
|
waitGroup.Add(callers)
|
|
for range callers {
|
|
go func() {
|
|
defer waitGroup.Done()
|
|
_, err := cache.getOrLoad(context.Background(), key, loader)
|
|
errors <- err
|
|
}()
|
|
}
|
|
<-started
|
|
close(release)
|
|
waitGroup.Wait()
|
|
close(errors)
|
|
for err := range errors {
|
|
if err != nil {
|
|
t.Fatalf("concurrent cache load: %v", err)
|
|
}
|
|
}
|
|
if got := loads.Load(); got != 1 {
|
|
t.Fatalf("concurrent loader calls = %d, want 1", got)
|
|
}
|
|
}
|
|
|
|
func BenchmarkRunSummaryAggregateCacheHit(b *testing.B) {
|
|
cache := newRunSummaryAggregateCache(time.Hour, 8)
|
|
key := runSummaryAggregateCacheKey{OrganizationID: "org-a"}
|
|
loader := func(context.Context) (runSummaryAggregates, error) {
|
|
return runSummaryAggregates{
|
|
Total: 558,
|
|
StatusCounts: emptyRunStatusCounts(),
|
|
}, nil
|
|
}
|
|
if _, err := cache.getOrLoad(context.Background(), key, loader); err != nil {
|
|
b.Fatalf("prime cache: %v", err)
|
|
}
|
|
|
|
b.ResetTimer()
|
|
b.RunParallel(func(parallel *testing.PB) {
|
|
for parallel.Next() {
|
|
if _, err := cache.getOrLoad(context.Background(), key, loader); err != nil {
|
|
b.Fatalf("cache hit: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|