Files
knowledge-governance/internal/releaseadapter/v071/library_capture_test.go
T
姚章浩 bf049cee1f
build / build (push) Successful in 1m12s
fix(snapshot): 暂停前后强制检查共享源全库任务
2026-10-28 01:05:44 +08:00

139 lines
5.0 KiB
Go

package v071
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"sync/atomic"
"testing"
"time"
"knowledge-governance/internal/libraryfreeze"
"knowledge-governance/internal/librarysnapshot"
)
type captureWriter struct {
running atomic.Bool
db *libraryFixture
late bool
lateSource bool
stops int
}
func (c *captureWriter) Inventory(context.Context) ([]libraryfreeze.Service, error) {
r := c.running.Load()
return []libraryfreeze.Service{{ID: "test-writer", Running: r, Healthy: r}}, nil
}
func (c *captureWriter) Stop(context.Context, string) error {
c.stops++
c.running.Store(false)
if c.late {
c.db.tx.pending = 1
}
if c.lateSource {
c.db.tx.sourcePending = 1
}
return nil
}
func (c *captureWriter) Start(context.Context, string) error { c.running.Store(true); return nil }
func TestLibraryCaptureMaintenanceOwnsArchiveAndRestoresOnFailure(t *testing.T) {
for _, mode := range []string{"success", "initial-busy", "late-pending", "search-failure", "other-library-busy", "other-library-late-pending"} {
t.Run(mode, func(t *testing.T) {
db := newLibraryFixture()
writer := &captureWriter{db: db, late: mode == "late-pending", lateSource: mode == "other-library-late-pending"}
if mode == "other-library-busy" {
db.tx.sourcePending = 1
}
writer.running.Store(true)
if mode == "initial-busy" {
db.tx.pending = 1
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/chunks/_mapping":
fmt.Fprint(w, `{"chunks":{"mappings":{"properties":{"knowledge_base_id":{"type":"keyword"},"knowledge_id":{"type":"keyword"},"tenant_id":{"type":"long"}}}}}`)
case "/chunks/_pit":
if writer.running.Load() {
t.Error("PIT opened before writers stopped")
}
fmt.Fprint(w, `{"id":"fixed-pit"}`)
case "/_search":
if mode == "search-failure" {
http.Error(w, "fixture failure", 503)
return
}
fmt.Fprint(w, `{"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":{"value":0,"relation":"eq"},"hits":[]}}`)
case "/_pit":
fmt.Fprint(w, `{"succeeded":true}`)
default:
http.Error(w, "unexpected", 400)
}
}))
defer server.Close()
search, err := NewSearchExporter(SearchExporterConfig{BaseURL: server.URL, Index: "chunks", PageSize: 10})
if err != nil {
t.Fatal(err)
}
mapping, err := search.inspectMapping(context.Background())
if err != nil {
t.Fatal(err)
}
root := t.TempDir()
_ = os.Chmod(root, 0700)
freeze, err := libraryfreeze.Open(root, "test-source", writer)
if err != nil {
t.Fatal(err)
}
defer freeze.Close()
archiveRoot := t.TempDir()
_ = os.Chmod(archiveRoot, 0700)
archive, err := librarysnapshot.Open(archiveRoot, 1024*1024)
if err != nil {
t.Fatal(err)
}
defer archive.Close()
req := LibraryCaptureRequest{JobID: "capture-job", Scope: "internal", Identity: PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, Compatibility: librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "fixture-model", EmbeddingDimension: 1024, IndexMappingSHA256: mapping.digest}, CreatedAt: time.Date(2026, 10, 27, 0, 0, 0, 0, time.UTC), MaxDocuments: 100}
checks := 0
ready := func(context.Context) error { checks++; return nil }
result, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req)
if !writer.running.Load() {
t.Fatal("test writer left stopped")
}
if mode != "success" {
if err == nil {
t.Fatal("invalid candidate succeeded")
}
if (mode == "initial-busy" || mode == "other-library-busy") && writer.stops != 0 {
t.Fatal("busy source stopped")
}
return
}
if err != nil || result.State != "succeeded" {
t.Fatalf("capture=%+v err=%v", result, err)
}
saved, err := archive.Load(result.SnapshotID)
if err != nil || saved.Scope != "internal" || checks != 2 {
t.Fatalf("archive=%+v checks=%d err=%v", saved, checks, err)
}
db.tx.pending = 1
replay, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req)
if err != nil || replay.SnapshotID != result.SnapshotID || writer.stops != 1 {
t.Fatal("sealed replay consulted current source")
}
req.Scope = "external"
if got, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req); err == nil || got.State == "succeeded" {
t.Fatal("conflicting replay returned a successful candidate")
}
req.Scope = "internal"
if err := os.Remove(archiveRoot + "/versions/" + result.SnapshotID + ".json"); err != nil {
t.Fatal(err)
}
if got, err := CaptureLibrary(context.Background(), freeze, archive, db, search, &libraryCandidateReader{}, ready, req); err == nil || got.State == "succeeded" {
t.Fatal("missing archive returned a successful candidate")
}
})
}
}