Some output tweaks; and some additional transition events upon resume (paused->unknown->{up|down})

This commit is contained in:
2026-04-11 03:04:47 +02:00
parent 46e78ec36f
commit 56a4a6ba25
4 changed files with 85 additions and 25 deletions

View File

@@ -104,13 +104,15 @@ type Backend struct {
Transitions []Transition // newest first, capped at maxHistory
}
// New creates a Backend in StateUnknown.
// New creates a Backend in StateUnknown with the health counter pre-loaded to
// Rise-1, so the very first probe resolves the state: one pass → Up, any
// fail → Down (via the StateUnknown shortcut in Record).
func New(name string, addr net.IP, rise, fall int) *Backend {
return &Backend{
Name: name,
Address: addr,
State: StateUnknown,
Counter: HealthCounter{Rise: rise, Fall: fall},
Counter: HealthCounter{Rise: rise, Fall: fall, Health: rise - 1},
}
}
@@ -155,7 +157,7 @@ func (b *Backend) Resume(maxHistory int) bool {
return false
}
b.transition(StateUnknown, ProbeResult{}, maxHistory)
b.Counter.Health = 0
b.Counter.Health = b.Counter.Rise - 1
return true
}

View File

@@ -23,12 +23,14 @@ func TestInitialState(t *testing.T) {
if len(b.Transitions) != 0 {
t.Errorf("initial transitions: got %d, want 0", len(b.Transitions))
}
if b.Counter.Health != 0 {
t.Errorf("initial counter health: got %d, want 0", b.Counter.Health)
// Counter pre-loaded to Rise-1 so first probe resolves state immediately.
if want := b.Counter.Rise - 1; b.Counter.Health != want {
t.Errorf("initial counter health: got %d, want %d (rise-1)", b.Counter.Health, want)
}
}
// TestRiseToUp: rise=2 passes from Down/Unknown → Up.
// TestRiseToUp: from Unknown/Down with counter pre-loaded to Rise-1, a single
// pass is enough to reach Up.
func TestRiseToUp(t *testing.T) {
tests := []struct {
name string
@@ -39,18 +41,11 @@ func TestRiseToUp(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
b := newBackend()
b := newBackend() // rise=2 → Health starts at Rise-1=1
b.State = tt.initialState
// First pass: counter=1, still in DOWN range (rise=2), no transition.
if b.Record(pass(), 5) {
t.Error("should not transition after 1 pass (rise=2)")
}
if b.State != tt.initialState {
t.Errorf("state changed early: got %s", b.State)
}
// Second pass: counter=2=rise, transitions to Up.
// Counter is already at Rise-1; one pass reaches Rise → Up.
if !b.Record(pass(), 5) {
t.Error("should transition to Up after 2 passes")
t.Error("should transition to Up after 1 pass (counter pre-loaded to rise-1)")
}
if b.State != StateUp {
t.Errorf("state: got %s, want up", b.State)