Files
BountyBoard/internal/extsvc/breaker_test.go
T
etalon f87b954f27 feat: atomization pipeline with job queue, atomizer client, WS hub, and board UI (phase 7)
- internal/extsvc: circuit breaker (§11.16) + retrying bearer JSON client
- atomizer client: §5.1 contract incl. defensive coefficient normalization
  and extension coefficient range (0,2]
- persisted jobs collection: atomic claim, panic recovery, exponential
  backoff (max 3), stale-running requeue, shutdown-safe bookkeeping
- subdivide (async 202, atomizing status, re-run replaces unpublished
  children after confirm) and extend (sibling task, source budget)
- failure path reverts status and notifies the consultant on final attempt
- PATCH task editing with bounty recompute, budget cascade to descendants
- publish single + bulk; task detail endpoint role-scoped
- coder/websocket hub: multiplexed channels, origin check, 30s heartbeats;
  client ws.js with reconnect + 15s polling fallback
- consultant atomization board: tree by root, coefficient sliders with live
  per-parent sum indicator, bounty preview, modals, shimmer, atomizer-down
  gating via /api/v1/service-health
- fix: tasks external-ref unique index is partial, not sparse (compound
  sparse indexed every task through customerId and broke child inserts)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 19:17:09 +02:00

76 lines
1.6 KiB
Go

package extsvc
import (
"errors"
"testing"
"time"
)
func TestBreakerLifecycle(t *testing.T) {
b := NewBreaker()
now := time.Date(2026, 6, 12, 12, 0, 0, 0, time.UTC)
b.now = func() time.Time { return now }
boom := errors.New("boom")
if b.State() != "closed" || !b.Allow() {
t.Fatal("fresh breaker must be closed")
}
// two failures: still closed
b.Record(boom)
b.Record(boom)
if b.State() != "closed" || !b.Allow() {
t.Fatal("breaker must stay closed below threshold")
}
// third consecutive failure opens it
b.Record(boom)
if b.State() != "open" {
t.Fatalf("state = %s, want open", b.State())
}
if b.Allow() {
t.Fatal("open breaker must reject calls")
}
// after the cooldown one probe is allowed (half-open)
now = now.Add(61 * time.Second)
if !b.Allow() {
t.Fatal("half-open must allow one probe")
}
if b.Allow() {
t.Fatal("only one probe may be in flight")
}
if b.State() != "half-open" {
t.Fatalf("state = %s, want half-open", b.State())
}
// failed probe re-opens for another cooldown
b.Record(boom)
if b.Allow() {
t.Fatal("failed probe must re-open the breaker")
}
now = now.Add(61 * time.Second)
if !b.Allow() {
t.Fatal("next probe window")
}
// successful probe closes it fully
b.Record(nil)
if b.State() != "closed" || !b.Allow() || !b.Allow() {
t.Fatal("successful probe must close the breaker")
}
}
func TestBreakerSuccessResetsCount(t *testing.T) {
b := NewBreaker()
boom := errors.New("x")
b.Record(boom)
b.Record(boom)
b.Record(nil) // non-consecutive failures never open
b.Record(boom)
b.Record(boom)
if b.State() != "closed" {
t.Fatal("interleaved success must reset the failure count")
}
}