Files
mtusi2026lab7/backend/internal/adapter/lifecycle/lifecycle_test.go

38 lines
1.0 KiB
Go

package lifecycle
import (
"context"
"errors"
"testing"
)
func TestMonitorStopsNormalWorkOnceOnFreshnessFailure(t *testing.T) {
stops, reports := 0, 0
r, e := New(Ports{Watch: func(context.Context) error { return errors.New("expired") }, Stop: func() { stops++ }, Report: func() { reports++ }})
if e != nil {
t.Fatal(e)
}
r.Run(context.Background())
if stops != 1 || reports != 1 {
t.Fatal("freshness failure did not stop normal work")
}
}
func TestMonitorDoesNotReportNormalCancellation(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
stops := 0
r, e := New(Ports{Watch: func(context.Context) error { return context.Canceled }, Stop: func() { stops++ }, Report: func() { t.Fatal("normal stop reported failure") }})
if e != nil {
t.Fatal(e)
}
r.Run(ctx)
if stops != 0 {
t.Fatal("normal cancellation invoked failure stop")
}
}
func TestMonitorRejectsMissingPorts(t *testing.T) {
if _, e := New(Ports{}); e == nil {
t.Fatal("process monitor accepted implicit production ports")
}
}