forked from mtusi2026/mtusi2026lab6
146 lines
5.0 KiB
Go
146 lines
5.0 KiB
Go
package postgres
|
|
|
|
import (
|
|
"backend/db"
|
|
"backend/internal/config"
|
|
"backend/internal/domain/model"
|
|
"context"
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestReadySLIIncludesFailedStuckReplayAndReprocessedAcceptances(t *testing.T) {
|
|
if os.Getenv("RUN_SLO_DB_TESTS") != "1" {
|
|
t.Skip("isolated real PostgreSQL required")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
dsn, e := config.ReadSecret("/run/secrets/database_url")
|
|
if e != nil {
|
|
t.Fatal("database file unavailable")
|
|
}
|
|
operator, e := pgxpool.New(ctx, dsn)
|
|
if e != nil {
|
|
t.Fatal("operator unavailable")
|
|
}
|
|
defer operator.Close()
|
|
schema := "slo_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
|
if _, e = operator.Exec(ctx, "CREATE SCHEMA "+schema); e != nil {
|
|
t.Fatal("isolated schema failed")
|
|
}
|
|
defer func() {
|
|
c, stop := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer stop()
|
|
if _, e := operator.Exec(c, "DROP SCHEMA "+schema+" CASCADE"); e != nil {
|
|
t.Error("isolated schema cleanup failed")
|
|
}
|
|
}()
|
|
u, e := url.Parse(dsn)
|
|
if e != nil {
|
|
t.Fatal("DSN unavailable")
|
|
}
|
|
query := u.Query()
|
|
query.Set("search_path", schema)
|
|
u.RawQuery = query.Encode()
|
|
if e = db.Migrate(ctx, u.String()); e != nil {
|
|
t.Fatal("isolated real migrations failed")
|
|
}
|
|
pool, e := pgxpool.New(ctx, u.String())
|
|
if e != nil {
|
|
t.Fatal("isolated pool unavailable")
|
|
}
|
|
defer pool.Close()
|
|
s := New(pool, nil)
|
|
user, e := s.CreateUser(ctx, model.UserDraft{Username: "slo_" + uuid.NewString()[:8], DisplayName: "Synthetic SLO", PasswordHash: "disabled"})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
chat, e := s.CreateChat(ctx, model.ChatDraft{Title: "Owned cohort fixture", Creator: user.ID, MemberIDs: []uuid.UUID{user.ID}})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
reserve := func() model.Attachment {
|
|
t.Helper()
|
|
a, e := s.ReserveAttachment(ctx, model.Attachment{ID: uuid.New(), ChatID: chat.ID, OwnerID: user.ID, Filename: "cohort.png", ContentType: "image/png", SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", SizeBytes: 1})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return a
|
|
}
|
|
var replayID uuid.UUID
|
|
for _, f := range []struct {
|
|
age, latency int
|
|
status string
|
|
advance bool
|
|
}{{15, 5, "ready", false}, {16, 11, "ready", false}, {17, -1, "failed", false}, {18, -1, "processing", false}, {35, 6, "ready", false}, {100, 6, "ready", false}, {5, 1, "ready", false}, {500, 2, "ready", false}, {15, 2, "ready", true}} {
|
|
a := reserve()
|
|
if _, e = s.QueueAttachment(ctx, a, "", ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = pool.Exec(ctx, "UPDATE attachments SET accepted_at=clock_timestamp()-$2*interval '1 second',first_ready_at=CASE WHEN $3::int>=0 THEN clock_timestamp()-($2-$3)*interval '1 second' ELSE NULL END,status=$4 WHERE id=$1", a.ID, f.age, f.latency, f.status); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if f.advance {
|
|
if ok, e := s.AdvancePipeline(ctx, a.ID); e != nil || !ok {
|
|
t.Fatal("actual version advance failed")
|
|
}
|
|
}
|
|
if f.status == "failed" {
|
|
replayID = a.ID
|
|
}
|
|
}
|
|
// A late explicit replay may become ready, but cannot erase the missed original deadline.
|
|
if ok, e := s.ReplayJob(ctx, replayID); e != nil || !ok {
|
|
t.Fatal("actual replay failed")
|
|
}
|
|
jobs, e := s.PendingJobs(ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, j := range jobs {
|
|
if j.AttachmentID == replayID {
|
|
lease := uuid.New()
|
|
owned, _, ok, e := s.AcquireJob(ctx, j, lease, 30*time.Second)
|
|
if e != nil || !ok {
|
|
t.Fatal("replay acquisition failed")
|
|
}
|
|
if ok, e = s.CompleteJob(ctx, owned, lease, []model.Variant{}); e != nil || !ok {
|
|
t.Fatal("replayed late completion failed")
|
|
}
|
|
}
|
|
}
|
|
// Upload can start long before durable acceptance. QueueAttachment must establish
|
|
// its own timestamp, while legacy rows with unknown acceptance remain unmeasured.
|
|
oldUpload := reserve()
|
|
if _, e = pool.Exec(ctx, "UPDATE attachments SET created_at=clock_timestamp()-interval '1 hour' WHERE id=$1", oldUpload.ID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if _, e = s.QueueAttachment(ctx, oldUpload, "", ""); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var age float64
|
|
if e = pool.QueryRow(ctx, "SELECT EXTRACT(EPOCH FROM clock_timestamp()-accepted_at)::float8 FROM attachments WHERE id=$1", oldUpload.ID).Scan(&age); e != nil || age < 0 || age > 2 {
|
|
t.Fatal("acceptance clock incorrectly reused upload start")
|
|
}
|
|
legacy := reserve()
|
|
if _, e = pool.Exec(ctx, "UPDATE attachments SET created_at=clock_timestamp()-interval '15 seconds',status='ready' WHERE id=$1", legacy.ID); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
r, e := s.(*Store).q.ReadySLI(ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if r.Matured10 != 5 || r.Timely10 != 2 || r.Matured30 != 6 || r.Timely30 != 3 || r.Matured300 != 7 || r.Timely300 != 4 {
|
|
t.Fatal("SLO denominator or immutable first-ready outcome changed after replay/advance or counted immature/unknown upload")
|
|
}
|
|
metrics, e := s.PipelineMetrics(ctx)
|
|
if e != nil || metrics.Oldest < 0 || metrics.Oldest > 5 {
|
|
t.Fatal("queue age included pre-accept upload time instead of durable logical job")
|
|
}
|
|
}
|