forked from mtusi2026/mtusi2026lab7
Prepare verified laboratory 7 implementation and evidence
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"backend/internal/adapter/freshness"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func FromEnvironment() (*freshness.Watcher, error) {
|
||||
path := os.Getenv("SECRET_FRESHNESS_FILE")
|
||||
if path == "" {
|
||||
return nil, nil
|
||||
}
|
||||
age := 120
|
||||
if s := os.Getenv("SECRET_MAX_AGE_SECONDS"); s != "" {
|
||||
n, e := strconv.Atoi(s)
|
||||
if e != nil || n < 1 || n > 3600 {
|
||||
return nil, ErrConfiguration
|
||||
}
|
||||
age = n
|
||||
}
|
||||
return freshness.New(freshness.Ports{Read: freshness.FileReader(path), Now: time.Now, NewTicker: freshness.SystemTicker}, time.Duration(age)*time.Second, min(5*time.Second, time.Duration(age)*time.Second))
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
// Package lifecycle connects an explicitly supplied health monitor to composition cancellation.
|
||||
package lifecycle
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type Ports struct {
|
||||
Watch func(context.Context) error
|
||||
Stop func()
|
||||
Report func()
|
||||
}
|
||||
type Runner struct{ ports Ports }
|
||||
|
||||
var ErrConfiguration = errors.New("invalid lifecycle configuration")
|
||||
|
||||
func New(p Ports) (*Runner, error) {
|
||||
if p.Watch == nil || p.Stop == nil || p.Report == nil {
|
||||
return nil, ErrConfiguration
|
||||
}
|
||||
return &Runner{p}, nil
|
||||
}
|
||||
func (r *Runner) Run(ctx context.Context) {
|
||||
if e := r.ports.Watch(ctx); e != nil && ctx.Err() == nil {
|
||||
r.ports.Report()
|
||||
r.ports.Stop()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user