forked from mtusi2026/mtusi2026lab6
93 lines
2.6 KiB
Go
93 lines
2.6 KiB
Go
package pipeline
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type observerFixture struct {
|
|
extracted string
|
|
injected bool
|
|
started int
|
|
finished int
|
|
ready int
|
|
spans int
|
|
}
|
|
|
|
// Start counts actual operation scopes without imposing an SDK on pipeline tests.
|
|
func (o *observerFixture) Start(ctx context.Context, _ string, _ string) (context.Context, func(error)) {
|
|
o.spans++
|
|
return ctx, func(error) {
|
|
}
|
|
}
|
|
|
|
// Extract captures only the intended durable carrier.
|
|
func (o *observerFixture) Extract(ctx context.Context, parent, _ string) context.Context {
|
|
o.extracted = parent
|
|
return ctx
|
|
}
|
|
|
|
// Inject models the producer's outgoing parent context.
|
|
func (o *observerFixture) Inject(context.Context) (string, string) {
|
|
o.injected = true
|
|
return "producer-parent", "state"
|
|
}
|
|
|
|
// JobStarted records one claimed attempt.
|
|
func (o *observerFixture) JobStarted() {
|
|
o.started++
|
|
}
|
|
|
|
// JobFinished releases one claimed attempt after its actual result.
|
|
func (o *observerFixture) JobFinished(context.Context, time.Duration, error) {
|
|
o.finished++
|
|
}
|
|
|
|
// Ready counts only confirmed durable results.
|
|
func (o *observerFixture) Ready(context.Context, time.Time) {
|
|
o.ready++
|
|
}
|
|
|
|
// TestObservedDispatchPropagatesProducer verifies producer spans surround the real queue call.
|
|
func TestObservedDispatchPropagatesProducer(t *testing.T) {
|
|
worker, _, _ := fixtureWorker(t)
|
|
observer := &observerFixture{}
|
|
worker.WithObserver(observer)
|
|
if err := worker.Dispatch(t.Context()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !observer.injected || observer.spans != 1 {
|
|
t.Fatal("producer handoff not observed")
|
|
}
|
|
}
|
|
|
|
// TestObservedAttemptMeasuresDurableCompletion ensures metrics do not count only notification receipt.
|
|
func TestObservedAttemptMeasuresDurableCompletion(t *testing.T) {
|
|
worker, _, delivery := fixtureWorker(t)
|
|
delivery.Job.Traceparent = "accepted-parent"
|
|
observer := &observerFixture{}
|
|
worker.WithObserver(observer)
|
|
if err := worker.Handle(t.Context(), delivery); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if observer.extracted != "accepted-parent" || observer.started != 1 || observer.finished != 1 || observer.ready != 1 {
|
|
t.Fatal("incorrect attempt observation")
|
|
}
|
|
}
|
|
|
|
// TestObservedFailedCommitDoesNotCountReady protects the end-to-end success histogram from failed publication.
|
|
func TestObservedFailedCommitDoesNotCountReady(t *testing.T) {
|
|
worker, fixture, delivery := fixtureWorker(t)
|
|
fixture.completeErr = errors.New("uncertain commit")
|
|
observer := &observerFixture{}
|
|
worker.WithObserver(observer)
|
|
if err := worker.Handle(t.Context(), delivery); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if observer.ready != 0 || observer.finished != 1 {
|
|
t.Fatal("uncertain result counted ready")
|
|
}
|
|
}
|