forked from mtusi2026/mtusi2026lab7
191 lines
6.1 KiB
Go
191 lines
6.1 KiB
Go
package usecase
|
|
|
|
import (
|
|
"backend/internal/domain/model"
|
|
"backend/internal/domain/port"
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"github.com/google/uuid"
|
|
"io"
|
|
"syscall"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type pipelineStoreFixture struct {
|
|
failed bool
|
|
code string
|
|
complete bool
|
|
a model.Attachment
|
|
cleaned int
|
|
}
|
|
|
|
func (f *pipelineStoreFixture) PendingJobs(context.Context) ([]model.Job, error) { return nil, nil }
|
|
func (f *pipelineStoreFixture) Published(context.Context, uuid.UUID) error { return nil }
|
|
func (f *pipelineStoreFixture) AcquireJob(_ context.Context, j model.Job, _ uuid.UUID, _ time.Duration) (model.Job, model.Attachment, bool, error) {
|
|
j.Attempt = 1
|
|
return j, f.a, !f.complete, nil
|
|
}
|
|
func (f *pipelineStoreFixture) CompleteJob(context.Context, model.Job, uuid.UUID, []model.Variant) (bool, error) {
|
|
f.complete = true
|
|
f.a.Status = "ready"
|
|
return true, nil
|
|
}
|
|
func (f *pipelineStoreFixture) RetryJob(_ context.Context, _ model.Job, _ uuid.UUID, code string, _ time.Duration, failed bool) error {
|
|
f.failed = failed
|
|
f.code = code
|
|
return nil
|
|
}
|
|
func (f *pipelineStoreFixture) ReplayJob(context.Context, uuid.UUID) (bool, error) { return false, nil }
|
|
func (f *pipelineStoreFixture) AdvancePipeline(context.Context, uuid.UUID) (bool, error) {
|
|
return false, nil
|
|
}
|
|
func (f *pipelineStoreFixture) StagingCleanup(context.Context) ([]model.Attachment, error) {
|
|
if f.complete && f.cleaned == 0 {
|
|
return []model.Attachment{f.a}, nil
|
|
}
|
|
return nil, nil
|
|
}
|
|
func (f *pipelineStoreFixture) StagingCleaned(context.Context, uuid.UUID) error {
|
|
f.cleaned++
|
|
return nil
|
|
}
|
|
|
|
type pipelineObjectsFixture struct {
|
|
content []byte
|
|
removeErr error
|
|
removed int
|
|
}
|
|
|
|
func (f *pipelineObjectsFixture) Get(context.Context, string) (io.ReadCloser, error) {
|
|
return io.NopCloser(bytes.NewReader(f.content)), nil
|
|
}
|
|
func (f *pipelineObjectsFixture) Put(context.Context, string, io.Reader, int64, string) error {
|
|
return nil
|
|
}
|
|
func (f *pipelineObjectsFixture) Stat(context.Context, string) (port.Object, error) {
|
|
return port.Object{Size: int64(len(f.content))}, nil
|
|
}
|
|
func (f *pipelineObjectsFixture) Remove(context.Context, string) error {
|
|
if f.removeErr != nil {
|
|
return f.removeErr
|
|
}
|
|
f.removed++
|
|
return nil
|
|
}
|
|
func (f *pipelineObjectsFixture) List(context.Context, string) ([]port.Object, bool, error) {
|
|
return nil, false, nil
|
|
}
|
|
func (f *pipelineObjectsFixture) Ready(context.Context) error { return nil }
|
|
|
|
type transformFixture struct{ err error }
|
|
|
|
func (f transformFixture) Transform(context.Context, io.Reader) ([]port.ProcessedVariant, error) {
|
|
return nil, f.err
|
|
}
|
|
func TestInfrastructureTransformFailureIsRetried(t *testing.T) {
|
|
raw := []byte("valid durable bytes")
|
|
sum := sha256.Sum256(raw)
|
|
f := &pipelineStoreFixture{a: model.Attachment{ID: uuid.New(), SizeBytes: int64(len(raw)), SHA256: hex.EncodeToString(sum[:])}}
|
|
for _, failure := range []error{syscall.ENOSPC, errors.New("convert executable unavailable")} {
|
|
p := NewPipeline(f, &pipelineObjectsFixture{content: raw}, transformFixture{failure}, 5)
|
|
if e := p.Process(context.Background(), model.Job{ID: uuid.New(), PipelineVersion: 1}); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if f.failed || f.code != "processing_failed" {
|
|
t.Fatalf("temporary transform infrastructure failure became permanent: failed=%t code=%s", f.failed, f.code)
|
|
}
|
|
}
|
|
}
|
|
func TestCompletedDeliveryRecoversStagingCleanupAfterCrash(t *testing.T) {
|
|
raw := []byte("immutable original")
|
|
sum := sha256.Sum256(raw)
|
|
store := &pipelineStoreFixture{a: model.Attachment{ID: uuid.New(), SizeBytes: int64(len(raw)), SHA256: hex.EncodeToString(sum[:])}}
|
|
objects := &pipelineObjectsFixture{content: raw}
|
|
p := NewPipeline(store, objects, transformFixture{}, 5)
|
|
p.Fault = func(point string) error {
|
|
if point == "after_commit" {
|
|
return errors.New("worker crash")
|
|
}
|
|
return nil
|
|
}
|
|
j := model.Job{ID: uuid.New(), PipelineVersion: 1}
|
|
if e := p.Process(context.Background(), j); e == nil {
|
|
t.Fatal("crash was not injected")
|
|
}
|
|
if objects.removed != 0 {
|
|
t.Fatal("staging removed before simulated crash")
|
|
}
|
|
p = NewPipeline(store, objects, transformFixture{}, 5)
|
|
if e := p.Process(context.Background(), j); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if objects.removed != 1 || store.cleaned != 1 || store.a.Status != "ready" {
|
|
t.Fatalf("completed redelivery did not recover cleanup: removed=%d durable_cleaned=%d status=%s", objects.removed, store.cleaned, store.a.Status)
|
|
}
|
|
if e := p.Process(context.Background(), j); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if objects.removed != 1 {
|
|
t.Fatal("completed cleanup was repeated")
|
|
}
|
|
}
|
|
|
|
type blockedCleanupObjects struct {
|
|
*pipelineObjectsFixture
|
|
entered, release chan struct{}
|
|
}
|
|
|
|
func (f *blockedCleanupObjects) Remove(ctx context.Context, key string) error {
|
|
close(f.entered)
|
|
select {
|
|
case <-f.release:
|
|
return f.pipelineObjectsFixture.Remove(ctx, key)
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
func TestReadyTelemetryPublishedBeforeSlowStagingCleanup(t *testing.T) {
|
|
raw := []byte("immutable original")
|
|
sum := sha256.Sum256(raw)
|
|
store := &pipelineStoreFixture{a: model.Attachment{ID: uuid.New(), SizeBytes: int64(len(raw)), SHA256: hex.EncodeToString(sum[:])}}
|
|
objects := &blockedCleanupObjects{pipelineObjectsFixture: &pipelineObjectsFixture{content: raw}, entered: make(chan struct{}), release: make(chan struct{})}
|
|
p := NewPipeline(store, objects, transformFixture{}, 5)
|
|
events := make(chan string, 2)
|
|
p.OnResult = func(_ context.Context, _ model.Job, outcome string, _ time.Duration) { events <- outcome }
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
|
defer cancel()
|
|
done := make(chan error, 1)
|
|
go func() { done <- p.Process(ctx, model.Job{ID: uuid.New(), PipelineVersion: 1, CreatedAt: time.Now()}) }()
|
|
select {
|
|
case <-objects.entered:
|
|
case <-ctx.Done():
|
|
t.Fatal("cleanup was not reached")
|
|
}
|
|
reported := 0
|
|
select {
|
|
case outcome := <-events:
|
|
reported++
|
|
if outcome != "ready" {
|
|
t.Error("wrong published outcome")
|
|
}
|
|
default:
|
|
t.Error("ready telemetry was delayed until staging cleanup")
|
|
}
|
|
close(objects.release)
|
|
if e := <-done; e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
select {
|
|
case <-events:
|
|
reported++
|
|
default:
|
|
}
|
|
if reported != 1 {
|
|
t.Error("ready attempt emitted an incorrect event count")
|
|
}
|
|
}
|