| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "github.com/carlosframework/platform/internal/hostwrite" |
| 7 | "github.com/carlosframework/platform/internal/lbhealth" |
| 8 | ) |
| 9 | |
| 10 | // Region HA spec 2 §5, §6: the health answer's hosting condition is the |
| 11 | // mode RunAgent's gate chose (agentHosting), held only while the claim |
| 12 | // worker runs. Claim mode off never says hosting, a joining process never |
| 13 | // does, and a hosting box's answer turns 503 when its worker ends. |
| 14 | func TestClaimWiringHostingFollowsTheModeAndTheWorker(t *testing.T) { |
| 15 | lbhealth.Default.SetHosting(false) |
| 16 | was := agentHosting.Load() |
| 17 | t.Cleanup(func() { lbhealth.Default.SetHosting(false); agentHosting.Store(was) }) |
| 18 | |
| 19 | agentHosting.Store(true) |
| 20 | off := &ClaimWiring{} |
| 21 | if err := off.startRegional(t.Context(), nil, nil); err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | if lbhealth.Default.Hosting() { |
| 25 | t.Fatal("claim mode off marked the box hosting") |
| 26 | } |
| 27 | |
| 28 | start := func() *ClaimWiring { |
| 29 | loop := testRegionalLoop{started: make(chan struct{})} |
| 30 | w := &ClaimWiring{Writes: hostwrite.Config{Enabled: true}, Providers: ClaimProviders{Regional: loop}} |
| 31 | if err := w.startRegional(t.Context(), nil, nil); err != nil { |
| 32 | t.Fatal(err) |
| 33 | } |
| 34 | <-loop.started |
| 35 | return w |
| 36 | } |
| 37 | |
| 38 | agentHosting.Store(false) |
| 39 | w := start() |
| 40 | if lbhealth.Default.Hosting() { |
| 41 | t.Fatal("a process that is not hosting marked the box hosting") |
| 42 | } |
| 43 | if err := w.close(t.Context()); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | |
| 47 | agentHosting.Store(true) |
| 48 | w = start() |
| 49 | if !lbhealth.Default.Hosting() { |
| 50 | t.Fatal("the claim agent of an enrolled box is not hosting") |
| 51 | } |
| 52 | if err := w.close(t.Context()); err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | if lbhealth.Default.Hosting() { |
| 56 | t.Fatal("the box still says hosting after its claim worker ended") |
| 57 | } |
| 58 | } |
| 59 | |