Some checks are pending
web-ci / checks (pull_request) Blocked by required conditions
web-ci / pool-search-mongo (pull_request) Blocked by required conditions
web-ci / work-availability (pull_request) Blocked by required conditions
web-ci / query-serving (pull_request) Blocked by required conditions
(cherry picked from commit fb4f6715bb58c1c512284d0651024ac890e6f896)
120 lines
4.3 KiB
Go
120 lines
4.3 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"fmt"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/event"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
"os"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAcceptancePriorityConfigMongo(t *testing.T) {
|
|
if os.Getenv("ACCEPTANCE_PRIORITY_LOCAL_MONGO") != "1" {
|
|
t.Skip("isolated local Mongo opt-in")
|
|
}
|
|
f := priorityFixture(t)
|
|
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
|
|
defer cancel()
|
|
var scans atomic.Int32
|
|
monitor := &event.CommandMonitor{Started: func(_ context.Context, e *event.CommandStartedEvent) {
|
|
if e.CommandName == "find" || e.CommandName == "aggregate" {
|
|
col, _ := e.Command.Lookup(e.CommandName).StringValueOK()
|
|
if col == "humanItems" || col == "humanBatches" || col == "annotationTasks" {
|
|
scans.Add(1)
|
|
}
|
|
}
|
|
}}
|
|
client, e := mongo.Connect(ctx, options.Client().ApplyURI("mongodb://127.0.0.1:27018").SetMonitor(monitor))
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer client.Disconnect(context.Background())
|
|
db := client.Database(fmt.Sprintf("acceptance_priority_config_test_%d", time.Now().UnixNano()))
|
|
defer db.Drop(context.Background())
|
|
s := store.NewMongoStore(db)
|
|
supplier, _ := f.store.GetSupplier(ctx, "priority-production")
|
|
if e = s.UpsertSupplier(ctx, *supplier); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.UpsertUser(ctx, f.admin); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
org := f.item.OrganizationID
|
|
template, err := f.store.GetHumanBatch(ctx, f.item.BatchID)
|
|
if err != nil || template == nil {
|
|
t.Fatal("missing fixture batch", err)
|
|
}
|
|
for i := 0; i < 56; i++ {
|
|
b := *template
|
|
b.ID = fmt.Sprintf("target-%d", i)
|
|
b.SourceBatchID = fmt.Sprintf("source-%d", i)
|
|
if err := s.UpsertHumanBatch(ctx, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b.ID = b.SourceBatchID
|
|
b.SourceBatchID = ""
|
|
if err := s.UpsertHumanBatch(ctx, b); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.UpsertHumanItem(ctx, domain.HumanItemRecord{ID: b.ID, BatchID: b.ID, OrganizationID: org, ProjectID: b.ProjectID, SupplierID: supplier.ID, Status: domain.HumanItemAccepted}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
scans.Store(0)
|
|
docs := make([]any, 5600)
|
|
for i := range docs {
|
|
docs[i] = bson.M{"id": fmt.Sprintf("candidate-%06d", i), "organizationId": f.item.OrganizationID, "currentStageId": "prepackage_acceptance", "status": "ready", "projectId": template.ProjectID, "batchId": fmt.Sprintf("target-%d", i/100), "pluginId": "annotation-qa", "subject": bson.M{"payload": bson.M{"synthetic": "fixture"}}}
|
|
}
|
|
if _, e = db.Collection("humanItems").InsertMany(ctx, docs); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
f.handler = NewHandler(s, nil)
|
|
for i, op := range []struct {
|
|
method string
|
|
body any
|
|
}{{"GET", nil}, {"PUT", map[string]any{"enabled": true, "version": 0, "expiresAt": time.Now().Add(time.Hour).UTC().Format(time.RFC3339)}}, {"PUT", map[string]any{"enabled": false, "version": 1}}} {
|
|
at := time.Now()
|
|
r := f.request(op.method, "/api/suppliers/priority-production/acceptance-priority", f.admin, op.body)
|
|
elapsed := time.Since(at)
|
|
t.Logf("operation%d %s: %s", i, op.method, elapsed)
|
|
if r.Code != 200 || elapsed >= time.Second {
|
|
t.Fatalf("config %d %s", r.Code, r.Body)
|
|
}
|
|
}
|
|
if scans.Load() != 0 {
|
|
t.Fatal("metadata used candidate/source queries")
|
|
}
|
|
rows, e := store.NewMongoStore(db).ListAcceptancePriorities(ctx, f.item.OrganizationID)
|
|
if e != nil || len(rows) != 1 || rows[0].Version != 2 || rows[0].Enabled || len(rows[0].History) != 2 {
|
|
t.Fatal("confirmed writes did not survive new store")
|
|
}
|
|
at := time.Now()
|
|
n, err := refreshAcceptanceSnapshot(ctx, s, org)
|
|
if err != nil || n != len(docs) {
|
|
t.Fatalf("refresh rows=%d err=%v", n, err)
|
|
}
|
|
t.Logf("nightly complete: %d candidates, 56 source batches, %s", n, time.Since(at))
|
|
scans.Store(0)
|
|
restarted := store.NewMongoStore(db)
|
|
f.handler = NewHandler(restarted, nil)
|
|
r := f.request("GET", "/api/suppliers/priority-production/acceptance-priority/statistics?version=2", f.admin, nil)
|
|
if !strings.Contains(r.Body.String(), `"remainingCount":5600`) || scans.Load() != 0 {
|
|
t.Fatal("snapshot read", r.Body, scans.Load())
|
|
}
|
|
for i, replica := range []*store.MongoStore{s, restarted} {
|
|
claimed, err := replica.ClaimAcceptanceRefresh(ctx, "2026-11-18T01:00:00+08:00")
|
|
if claimed != (i == 0) || err != nil {
|
|
t.Fatalf("replica %d claimed=%v err=%v", i, claimed, err)
|
|
}
|
|
}
|
|
|
|
}
|