forked from mtusi2026/mtusi2026lab5
Добавить версии pipeline и защиту от устаревших попыток
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/repositories"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPipelineUpgradeFencesOldLeaseAndPublishesV2(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
ctx := context.Background()
|
||||
store := repositories.NewAttachmentStore(f.db)
|
||||
user, _ := createTestUser(t, f.users, "versions", "Versions")
|
||||
chat := createTestChat(t, f.chats, "Versions", []string{user.ID})
|
||||
id, oldID := uuid.NewString(), uuid.NewString()
|
||||
if err := store.Create(ctx, repositories.Attachment{ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "v.png", ObjectKey: "originals/" + id, ContentType: "image/png", SizeBytes: 1, SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", CreatedAt: time.Now(), UpdatedAt: time.Now()}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `UPDATE attachments SET status='queued' WHERE id=$1`, id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `INSERT INTO attachment_jobs(id,attachment_id,staging_key) VALUES($1,$2,$3)`, oldID, id, "staging/jobs/"+id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
old := repositories.Job{ID: oldID, AttachmentID: id, PipelineVersion: 1}
|
||||
token, err := store.StartAttempt(ctx, old, time.Minute, 5)
|
||||
if err != nil || token == "" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
changed, err := store.UpgradePipeline(ctx, id, 2, "unit test upgrade", func(context.Context, string) error { return nil })
|
||||
if err != nil || !changed {
|
||||
t.Fatal(err)
|
||||
}
|
||||
effects := 0
|
||||
callback := func(a repositories.Attachment, _ string) ([]repositories.Variant, string, error) {
|
||||
effects++
|
||||
if a.PipelineVersion != 2 {
|
||||
t.Fatal("old version reached effects")
|
||||
}
|
||||
return []repositories.Variant{{Name: "thumbnail"}, {Name: "crop"}, {Name: "optimized"}}, "", nil
|
||||
}
|
||||
if err = store.ProcessJob(ctx, old, token, 5, callback); !errors.Is(err, repositories.ErrStaleJob) || effects != 0 {
|
||||
t.Fatal("old lease not fenced", err)
|
||||
}
|
||||
if terminal, err := store.JobTerminal(ctx, oldID); err != nil || !terminal {
|
||||
t.Fatal("superseded delivery not terminal", err)
|
||||
}
|
||||
var newID string
|
||||
if err = f.db.QueryRow(ctx, `SELECT id FROM attachment_jobs WHERE attachment_id=$1 AND pipeline_version=2`, id).Scan(&newID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
next := repositories.Job{ID: newID, AttachmentID: id, PipelineVersion: 2}
|
||||
token, err = store.StartAttempt(ctx, next, time.Minute, 5)
|
||||
if err != nil || token == "" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = store.ProcessJob(ctx, next, token, 5, callback); err != nil || effects != 1 {
|
||||
t.Fatal(err)
|
||||
}
|
||||
published, err := store.GetForMember(ctx, id, user.ID)
|
||||
if err != nil || published.Status != "ready" || published.PipelineVersion != 2 {
|
||||
t.Fatal("wrong active publication", err)
|
||||
}
|
||||
if changed, err = store.UpgradePipeline(ctx, id, 1, "no downgrade", nil); err != nil || changed {
|
||||
t.Fatal("version rollback allowed", err)
|
||||
}
|
||||
if changed, err = store.ReplayJob(ctx, oldID, "obsolete replay", nil); err != nil || changed {
|
||||
t.Fatal("old job reopened", err)
|
||||
}
|
||||
var count int
|
||||
if err = f.db.QueryRow(ctx, `SELECT count(*) FROM attachment_publications WHERE attachment_id=$1 AND pipeline_version=2`, id).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("publication count", count, err)
|
||||
}
|
||||
}
|
||||
@@ -121,6 +121,13 @@ func newTestFixture(t *testing.T) *testFixture {
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000009_pipeline_versions.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = redisClient.Close() })
|
||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in New Issue
Block a user