package host import ( "testing" "github.com/carlosframework/platform/internal/hostwrite" "github.com/carlosframework/platform/internal/lbhealth" ) // Region HA spec 2 §5, §6: the health answer's hosting condition is the // mode RunAgent's gate chose (agentHosting), held only while the claim // worker runs. Claim mode off never says hosting, a joining process never // does, and a hosting box's answer turns 503 when its worker ends. func TestClaimWiringHostingFollowsTheModeAndTheWorker(t *testing.T) { lbhealth.Default.SetHosting(false) was := agentHosting.Load() t.Cleanup(func() { lbhealth.Default.SetHosting(false); agentHosting.Store(was) }) agentHosting.Store(true) off := &ClaimWiring{} if err := off.startRegional(t.Context(), nil, nil); err != nil { t.Fatal(err) } if lbhealth.Default.Hosting() { t.Fatal("claim mode off marked the box hosting") } start := func() *ClaimWiring { loop := testRegionalLoop{started: make(chan struct{})} w := &ClaimWiring{Writes: hostwrite.Config{Enabled: true}, Providers: ClaimProviders{Regional: loop}} if err := w.startRegional(t.Context(), nil, nil); err != nil { t.Fatal(err) } <-loop.started return w } agentHosting.Store(false) w := start() if lbhealth.Default.Hosting() { t.Fatal("a process that is not hosting marked the box hosting") } if err := w.close(t.Context()); err != nil { t.Fatal(err) } agentHosting.Store(true) w = start() if !lbhealth.Default.Hosting() { t.Fatal("the claim agent of an enrolled box is not hosting") } if err := w.close(t.Context()); err != nil { t.Fatal(err) } if lbhealth.Default.Hosting() { t.Fatal("the box still says hosting after its claim worker ended") } }