forked from longtao.wu/droboticflow
130 lines
4.8 KiB
Go
130 lines
4.8 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestQueryReplicaCountOnlyLeavesListsAndCoreReadsOnAuthority(t *testing.T) {
|
|
s := &MongoStore{queryReplicaReads: &queryReplicaReads{counts: true}}
|
|
plain := context.Background()
|
|
list := WithQueryReplicaPoolRead(plain)
|
|
count := WithQueryReplicaCountRead(list)
|
|
if s.UsesQueryReplicaPool(plain) || s.UsesQueryReplicaPool(list) || !s.UsesQueryReplicaPool(count) {
|
|
t.Fatal("count-only routing escaped its marked count request")
|
|
}
|
|
if target, err := s.poolReplica(list); target != nil || err != nil {
|
|
t.Fatal("unavailable count infrastructure blocked legacy lists", err)
|
|
}
|
|
if _, err := s.poolReplica(count); !errors.Is(err, ErrQueryReplicaNotReady) {
|
|
t.Fatal("explicit count mode silently fell back to authority", err)
|
|
}
|
|
s.EnableQueryReplicaPoolReads(true)
|
|
if !s.UsesQueryReplicaPool(list) || !s.UsesQueryReplicaPool(count) {
|
|
t.Fatal("legacy list+count switch lost compatibility")
|
|
}
|
|
s.EnableQueryReplicaPoolReads(false)
|
|
s.EnableQueryReplicaCountReads(false)
|
|
if s.UsesQueryReplicaPool(list) || s.UsesQueryReplicaPool(count) {
|
|
t.Fatal("disabled modes still routed")
|
|
}
|
|
}
|
|
|
|
func TestQueryReplicaCandidateCachePrivateMetadataAndIsolation(t *testing.T) {
|
|
original := domain.HumanItemRecord{ID: "i", LegacyAssignment: map[string]string{"role": "u"}, AccountHandoff: &domain.AccountTaskHandoff{Pending: true}, AcceptanceClaim: &domain.AcceptanceClaimAccess{UserID: "u", SupplierID: "s"}, Subject: domain.HumanSubjectRef{Payload: map[string]any{"annotationDraft": "never copy"}}}
|
|
safe := cloneQueryCountRow(original)
|
|
raw, e := encodeQueryCountRows([]domain.HumanItemRecord{safe})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
rows, e := decodeQueryCountRows(raw)
|
|
if e != nil || len(rows) != 1 {
|
|
t.Fatal(e)
|
|
}
|
|
if rows[0].Subject.Payload != nil || !rows[0].AccountHandoff.Pending || rows[0].AcceptanceClaim.UserID != "u" {
|
|
t.Fatal("private eligibility metadata lost")
|
|
}
|
|
private := cloneQueryCountRow(rows[0])
|
|
private.AcceptanceClaim.SupplierID = "other"
|
|
private.AccountHandoff.Pending = false
|
|
private.LegacyAssignment["role"] = "other"
|
|
if rows[0].AcceptanceClaim.SupplierID != "s" || !rows[0].AccountHandoff.Pending || rows[0].LegacyAssignment["role"] != "u" {
|
|
t.Fatal("caller modified cached data")
|
|
}
|
|
if _, e = decodeQueryCountRows([]byte("invalid-cache")); e == nil {
|
|
t.Fatal("corrupt shared cache accepted")
|
|
}
|
|
}
|
|
|
|
func TestQueryReplicaMissingConfigurationCannotPanicCoreCache(t *testing.T) {
|
|
t.Setenv("QUERY_REPLICA_REPORTS", "1")
|
|
t.Setenv("QUERY_REPLICA_POOLS", "1")
|
|
t.Setenv("QUERY_SOURCE_TRACKING", "0")
|
|
s := &MongoStore{}
|
|
close, e := s.ConfigureQueryReplicaFromEnvironment(t.Context())
|
|
defer close()
|
|
if e == nil {
|
|
t.Fatal("serving without write tracking accepted")
|
|
}
|
|
if _, e = s.poolReplica(WithQueryReplicaPoolRead(t.Context())); !errors.Is(e, ErrQueryReplicaNotReady) {
|
|
t.Fatal("invalid query configuration fell back to source pool", e)
|
|
}
|
|
if s.SharedQueryCache() != nil {
|
|
t.Fatal("invalid replica returned source cache")
|
|
}
|
|
if _, e = s.reportReplica(t.Context()); !errors.Is(e, ErrQueryReplicaNotReady) {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
|
|
func TestQueryCountCacheEvictsOldestInsteadOfClearingAll(t *testing.T) {
|
|
c := &queryCountCache{}
|
|
c.remember("k0", []domain.HumanItemRecord{{ID: "0"}}, 1024)
|
|
time.Sleep(2 * time.Millisecond)
|
|
for i := 1; i < 32; i++ {
|
|
c.remember(fmt.Sprintf("k%d", i), []domain.HumanItemRecord{{ID: fmt.Sprintf("%d", i)}}, 1024)
|
|
}
|
|
if len(c.entries) != 32 || c.bytes != 32*1024 {
|
|
t.Fatalf("unexpected cache size=%d bytes=%d", len(c.entries), c.bytes)
|
|
}
|
|
c.remember("k32", []domain.HumanItemRecord{{ID: "32"}}, 1024)
|
|
if len(c.entries) != 32 {
|
|
t.Fatalf("expected 32 entries after eviction, got %d", len(c.entries))
|
|
}
|
|
if c.bytes != 32*1024 {
|
|
t.Fatalf("expected bytes=%d, got %d", 32*1024, c.bytes)
|
|
}
|
|
if _, ok := c.entries["k0"]; ok {
|
|
t.Fatal("expected oldest entry k0 to be evicted")
|
|
}
|
|
if _, ok := c.entries["k1"]; !ok {
|
|
t.Fatal("expected newer entry k1 to stay cached")
|
|
}
|
|
if _, ok := c.entries["k32"]; !ok {
|
|
t.Fatal("expected newest entry k32 to be cached")
|
|
}
|
|
}
|
|
|
|
func TestFilterAuthoritativeAdminPoolPageDropsConcurrentlyClaimedItems(t *testing.T) {
|
|
q := AdminAnnotationPoolQuery{Pool: "available"}
|
|
candidates := []domain.HumanItemRecord{
|
|
{ID: "a"},
|
|
{ID: "claimed"},
|
|
{ID: "missing"},
|
|
{ID: "b"},
|
|
}
|
|
hydrated := []domain.HumanItemRecord{
|
|
{ID: "b", CurrentStageID: "semantic_annotation", Status: domain.HumanItemReady},
|
|
{ID: "claimed", CurrentStageID: "semantic_annotation", Status: domain.HumanItemClaimed, AssignedUserID: "annotator-1"},
|
|
{ID: "a", CurrentStageID: "semantic_annotation", Status: domain.HumanItemReady},
|
|
}
|
|
got := filterAuthoritativeAdminPoolPage(candidates, hydrated, q)
|
|
if len(got) != 2 || got[0].ID != "a" || got[1].ID != "b" {
|
|
t.Fatalf("expected [a b] in candidate order without failing page, got %+v", got)
|
|
}
|
|
}
|