forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,255 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/model/dto"
|
||||
)
|
||||
|
||||
var errInjected = errors.New("errInjected dependency failure")
|
||||
|
||||
type workerFixture struct {
|
||||
lease *Lease
|
||||
source []byte
|
||||
outputs []Output
|
||||
dueErr error
|
||||
publishErr error
|
||||
confirmErr error
|
||||
claimErr error
|
||||
getErr error
|
||||
readErr error
|
||||
closeErr error
|
||||
processErr error
|
||||
completeErr error
|
||||
failErr error
|
||||
ackErr error
|
||||
putAt int
|
||||
puts int
|
||||
code string
|
||||
delay time.Duration
|
||||
acked int
|
||||
completed int
|
||||
received bool
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
// fixtureWorker provides independent dependencies for one delivery invariant.
|
||||
func fixtureWorker(t *testing.T) (*Worker, *workerFixture, Delivery) {
|
||||
t.Helper()
|
||||
source := []byte("bounded synthetic original")
|
||||
sum := sha256.Sum256(source)
|
||||
job := Job{ID: uuid.New(), AttachmentID: uuid.New(), Version: 1, Attempt: 1}
|
||||
f := &workerFixture{source: source, lease: &Lease{Job: job, Token: uuid.New(), Attachment: dto.Attachment{SHA256: hex.EncodeToString(sum[:]), SizeBytes: int64(len(source)), ContentType: "image/png"}, StagingKey: "staging/test"}, outputs: []Output{{Metadata: dto.Variant{Name: "thumbnail", ContentType: "image/jpeg"}, Bytes: []byte("variant")}}}
|
||||
worker, err := New(f, f, f, f, zap.NewNop())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return worker, f, Delivery{Job: job, Receipt: "receipt"}
|
||||
}
|
||||
|
||||
// Due supplies one durable fixture intent or a database failure.
|
||||
func (f *workerFixture) Due(context.Context, int) ([]Job, error) {
|
||||
return []Job{f.lease.Job}, f.dueErr
|
||||
}
|
||||
|
||||
// Published models acknowledgement persistence after broker acceptance.
|
||||
func (f *workerFixture) Published(context.Context, Job) error {
|
||||
return f.confirmErr
|
||||
}
|
||||
|
||||
// Claim supplies a fenced claim or the configured database outcome.
|
||||
func (f *workerFixture) Claim(context.Context, Job, time.Duration, int) (*Lease, error) {
|
||||
return f.lease, f.claimErr
|
||||
}
|
||||
|
||||
// Complete records publication attempts without implementing a second SQL model.
|
||||
func (f *workerFixture) Complete(context.Context, Lease, string, []dto.Variant) (bool, error) {
|
||||
f.completed++
|
||||
return f.completeErr == nil, f.completeErr
|
||||
}
|
||||
|
||||
// Fail captures safe failure classification and bounded retry scheduling.
|
||||
func (f *workerFixture) Fail(_ context.Context, _ Lease, code string, delay time.Duration, _ int) error {
|
||||
f.code = code
|
||||
f.delay = delay
|
||||
return f.failErr
|
||||
}
|
||||
|
||||
// Publish supplies one broker acceptance outcome.
|
||||
func (f *workerFixture) Publish(context.Context, Job) error {
|
||||
return f.publishErr
|
||||
}
|
||||
|
||||
// Receive delivers once and cancels the fixture loop on its second read.
|
||||
func (f *workerFixture) Receive(context.Context) (*Delivery, error) {
|
||||
if f.received {
|
||||
f.cancel()
|
||||
return nil, errInjected
|
||||
}
|
||||
f.received = true
|
||||
return &Delivery{Job: f.lease.Job, Receipt: "receipt"}, nil
|
||||
}
|
||||
|
||||
// Ack records whether the coordinator released the delivery.
|
||||
func (f *workerFixture) Ack(context.Context, string) error {
|
||||
f.acked++
|
||||
return f.ackErr
|
||||
}
|
||||
|
||||
// Get supplies a bounded reader with independent read and close failures.
|
||||
func (f *workerFixture) Get(context.Context, string) (io.ReadCloser, error) {
|
||||
return &faultReader{Reader: bytes.NewReader(f.source), readErr: f.readErr, closeErr: f.closeErr}, f.getErr
|
||||
}
|
||||
|
||||
// Put injects a failure at a configured original or variant write.
|
||||
func (f *workerFixture) Put(context.Context, string, string, io.Reader, int64) error {
|
||||
f.puts++
|
||||
if f.putAt == f.puts {
|
||||
return errInjected
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Process supplies one independent transformation outcome.
|
||||
func (f *workerFixture) Process(context.Context, []byte) ([]Output, error) {
|
||||
return f.outputs, f.processErr
|
||||
}
|
||||
|
||||
type faultReader struct {
|
||||
*bytes.Reader
|
||||
readErr error
|
||||
closeErr error
|
||||
}
|
||||
|
||||
// Read injects a transport read failure before consuming source bytes.
|
||||
func (r *faultReader) Read(p []byte) (int, error) {
|
||||
if r.readErr != nil {
|
||||
return 0, r.readErr
|
||||
}
|
||||
return r.Reader.Read(p)
|
||||
}
|
||||
|
||||
// Close exposes an independently failed stream finalization.
|
||||
func (r *faultReader) Close() error {
|
||||
return r.closeErr
|
||||
}
|
||||
|
||||
// TestHandlePublishesBeforeAck verifies all output writes precede the durable completion.
|
||||
func TestHandlePublishesBeforeAck(t *testing.T) {
|
||||
worker, f, delivery := fixtureWorker(t)
|
||||
if err := worker.Handle(t.Context(), delivery); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.puts != 2 || f.completed != 1 || f.acked != 1 || f.code != "" {
|
||||
t.Fatal("incomplete publication acknowledged")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandlePersistsRetryBeforeAck checks all transient I/O windows converge to durable retry.
|
||||
func TestHandlePersistsRetryBeforeAck(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
setup func(*workerFixture)
|
||||
code string
|
||||
}{
|
||||
{"read source", func(f *workerFixture) { f.getErr = errInjected }, "processing_failed"},
|
||||
{"read bytes", func(f *workerFixture) { f.readErr = errInjected }, "processing_failed"},
|
||||
{"close source", func(f *workerFixture) { f.closeErr = errInjected }, "processing_failed"},
|
||||
{"decode", func(f *workerFixture) { f.processErr = ErrInvalidImage }, "invalid_image"},
|
||||
{"checksum", func(f *workerFixture) { f.source = []byte("tampered") }, "invalid_image"},
|
||||
{"original write", func(f *workerFixture) { f.putAt = 1 }, "processing_failed"},
|
||||
{"variant write", func(f *workerFixture) { f.putAt = 2 }, "processing_failed"},
|
||||
{"result commit", func(f *workerFixture) { f.completeErr = errInjected }, "processing_failed"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
worker, f, delivery := fixtureWorker(t)
|
||||
tc.setup(f)
|
||||
if err := worker.Handle(t.Context(), delivery); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if f.code != tc.code || f.acked != 1 || f.delay < 2*time.Second || f.delay >= 3*time.Second {
|
||||
t.Fatal("unsafe failure transition")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleLeavesUncommittedFailurePending prevents acknowledgement when retry persistence fails.
|
||||
func TestHandleLeavesUncommittedFailurePending(t *testing.T) {
|
||||
worker, f, delivery := fixtureWorker(t)
|
||||
f.getErr = errInjected
|
||||
f.failErr = errInjected
|
||||
if err := worker.Handle(t.Context(), delivery); !errors.Is(err, errInjected) || f.acked != 0 {
|
||||
t.Fatal("uncommitted retry acknowledged")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleUnavailableClaimLeavesPending preserves delivery when the source of truth is unavailable.
|
||||
func TestHandleUnavailableClaimLeavesPending(t *testing.T) {
|
||||
worker, f, delivery := fixtureWorker(t)
|
||||
f.claimErr = errInjected
|
||||
if err := worker.Handle(t.Context(), delivery); !errors.Is(err, errInjected) || f.acked != 0 {
|
||||
t.Fatal("failed claim acknowledged")
|
||||
}
|
||||
}
|
||||
|
||||
// TestHandleDuplicateAcknowledgesWithoutEffects verifies fenced no-ops never touch S3.
|
||||
func TestHandleDuplicateAcknowledgesWithoutEffects(t *testing.T) {
|
||||
worker, f, delivery := fixtureWorker(t)
|
||||
f.lease = nil
|
||||
if err := worker.Handle(t.Context(), delivery); err != nil || f.acked != 1 || f.puts != 0 {
|
||||
t.Fatal("duplicate caused effects")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDispatchFailureBoundaries checks uncertainty leaves durable outbox intent for another pass.
|
||||
func TestDispatchFailureBoundaries(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
setup func(*workerFixture)
|
||||
}{
|
||||
{"database read", func(f *workerFixture) { f.dueErr = errInjected }},
|
||||
{"broker publish", func(f *workerFixture) { f.publishErr = errInjected }},
|
||||
{"confirmation commit", func(f *workerFixture) { f.confirmErr = errInjected }},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
worker, f, _ := fixtureWorker(t)
|
||||
tc.setup(f)
|
||||
if err := worker.Dispatch(t.Context()); !errors.Is(err, errInjected) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkerLoopStops verifies the loop releases ownership on cancellation.
|
||||
func TestWorkerLoopStops(t *testing.T) {
|
||||
worker, f, _ := fixtureWorker(t)
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
f.cancel = cancel
|
||||
f.failErr = errInjected
|
||||
f.getErr = errInjected
|
||||
worker.Run(ctx)
|
||||
if ctx.Err() == nil {
|
||||
t.Fatal("worker did not stop")
|
||||
}
|
||||
}
|
||||
|
||||
// TestWorkerRejectsMissingDependencies keeps invalid composition from starting a background loop.
|
||||
func TestWorkerRejectsMissingDependencies(t *testing.T) {
|
||||
if _, err := New(nil, nil, nil, nil, nil); err == nil {
|
||||
t.Fatal("missing dependencies accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user