package hub import ( "context" "fmt" "sync" "testing" "time" ) // drain returns every event currently buffered on a sub, non-blocking. func drain(s *Sub) []Message { var out []Message for { select { case m, ok := <-s.C(): if !ok { return out } out = append(out, m) default: return out } } } func types(ms []Message) []string { out := make([]string, len(ms)) for i, m := range ms { out[i] = m.Type } return out } func TestMemberJoinsAndGuestWaits(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) if err := h.Join(ctx, "r1", "sm", m.Peer); err != nil { t.Fatal(err) } ev := drain(m.Sub) if len(ev) != 1 || ev[0].Type != TypeRoster || len(ev[0].Roster.Peers) != 1 { t.Fatalf("member join events %v", types(ev)) } g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) if err := h.Join(ctx, "r1", "sg", g.Peer); err != nil { t.Fatal(err) } if ev := drain(g.Sub); len(ev) != 0 { t.Fatalf("waiting guest received %v", types(ev)) } ev = drain(m.Sub) if len(ev) != 1 || ev[0].Type != TypeKnock || ev[0].From != g.Peer || ev[0].Name != "Gus" { t.Fatalf("member did not get the knock: %v", types(ev)) } } func TestAdmitSendsRosterToGuestPeerToOthersAndKnockGone(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) drain(m.Sub) if err := h.Admit(ctx, "r1", "sm", m.Peer, g.Peer); err != nil { t.Fatal(err) } gev := drain(g.Sub) if len(gev) != 1 || gev[0].Type != TypeRoster || len(gev[0].Roster.Peers) != 2 || gev[0].Roster.Waiting != nil { t.Fatalf("guest got %v (waiting queue must not reach a guest)", types(gev)) } mev := types(drain(m.Sub)) if len(mev) != 2 || mev[0] != TypePeer || mev[1] != TypeKnockGone { t.Fatalf("member got %v", mev) } } func TestOnlyAnAdmittedMemberMayAdmit(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) g1, _ := h.Open("r1", "s1", 100, "G1", KindGuest) h.Join(ctx, "r1", "s1", g1.Peer) g2, _ := h.Open("r1", "s2", 101, "G2", KindGuest) h.Join(ctx, "r1", "s2", g2.Peer) h.Admit(ctx, "r1", "sm", m.Peer, g1.Peer) if err := h.Admit(ctx, "r1", "s1", g1.Peer, g2.Peer); err != ErrNotMember { t.Fatalf("admitted guest admitting: %v", err) } if err := h.Admit(ctx, "r1", "s2", g2.Peer, g2.Peer); err != ErrNotAdmitted { t.Fatalf("waiting guest admitting: %v", err) } } // Task 5 review Important 3: Admit must settle the actor's own // authority completely before it ever looks at the target, so a // participation with no admitting authority cannot use the shape of the // refusal to learn whether a candidate peer id exists in the room, and // the target's StandingFor is never spent on an actor that had no // business asking. func TestAdmitByAWaitingGuestNeverLooksAtTheTargetOrItsStanding(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) var calls int h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { calls++ if id < 100 { return KindMember, true, nil } return KindGuest, true, nil }}) ctx := context.Background() g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) // waiting, never admitted calls = 0 errAbsent := h.Admit(ctx, "r1", "sg", g.Peer, "0123456789abcdef0123456789abcdef") callsForAbsent := calls m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) // present, and admitted immediately as a member calls = 0 errPresent := h.Admit(ctx, "r1", "sg", g.Peer, m.Peer) callsForPresent := calls if errAbsent != ErrNotAdmitted || errPresent != ErrNotAdmitted { t.Fatalf("waiting guest's admit: absent target %v, present target %v, want ErrNotAdmitted both times", errAbsent, errPresent) } if callsForAbsent != 1 || callsForPresent != 1 { t.Fatalf("StandingFor calls: absent target %d, present target %d, want 1 both times (the actor's own — the target is never reached)", callsForAbsent, callsForPresent) } // The same short-circuit, one step later: an *admitted* guest is // past the !a.Admitted check but still fails a.Kind != KindMember, // so the target is still never reached (review Task 5 fix round 2 // item 6). if err := h.Admit(ctx, "r1", "sm", m.Peer, g.Peer); err != nil { t.Fatalf("member admitting the waiting guest: %v", err) } calls = 0 errAbsentAdmitted := h.Admit(ctx, "r1", "sg", g.Peer, "0123456789abcdef0123456789abcdef") callsForAbsentAdmitted := calls calls = 0 errPresentAdmitted := h.Admit(ctx, "r1", "sg", g.Peer, m.Peer) callsForPresentAdmitted := calls if errAbsentAdmitted != ErrNotMember || errPresentAdmitted != ErrNotMember { t.Fatalf("admitted guest's admit: absent target %v, present target %v, want ErrNotMember both times", errAbsentAdmitted, errPresentAdmitted) } if callsForAbsentAdmitted != 1 || callsForPresentAdmitted != 1 { t.Fatalf("StandingFor calls (admitted guest actor): absent target %d, present target %d, want 1 both times (the actor's own — the target is never reached)", callsForAbsentAdmitted, callsForPresentAdmitted) } } func TestCapAtJoinAndAtAdmit(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() var members []OpenResult for i := 0; i < 5; i++ { o, _ := h.Open("r1", "s"+string(rune('a'+i)), int64(1+i), "M", KindMember) members = append(members, o) h.Join(ctx, "r1", "s"+string(rune('a'+i)), o.Peer) } fifth := drain(members[4].Sub) if len(fifth) != 1 || fifth[0].Type != TypeFull { t.Fatalf("fifth member got %v, want full", types(fifth)) } // One leaves; four queued guests and the freed slot yield one admission. h.Leave("r1", "sd", members[3].Peer) var guests []OpenResult for i := 0; i < 4; i++ { o, _ := h.Open("r1", "g"+string(rune('a'+i)), int64(100+i), "G", KindGuest) guests = append(guests, o) h.Join(ctx, "r1", "g"+string(rune('a'+i)), o.Peer) } drain(members[0].Sub) if err := h.Admit(ctx, "r1", "sa", members[0].Peer, guests[0].Peer); err != nil { t.Fatal(err) } if err := h.Admit(ctx, "r1", "sa", members[0].Peer, guests[1].Peer); err != nil { t.Fatal(err) // Admit itself returns nil; the outcome is the `full` event } ev := types(drain(members[0].Sub)) if ev[len(ev)-1] != TypeFull { t.Fatalf("second admit over the cap: %v, want full last", ev) } if p, _ := h.Find("r1", "gb", guests[1].Peer); p.Admitted { t.Fatal("guest admitted over the cap") } } func TestEightWaitingThenFull(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() for i := 0; i < 8; i++ { o, _ := h.Open("r1", "g"+string(rune('a'+i)), int64(100+i), "G", KindGuest) h.Join(ctx, "r1", "g"+string(rune('a'+i)), o.Peer) } o, _ := h.Open("r1", "gz", 200, "G", KindGuest) if err := h.Join(ctx, "r1", "gz", o.Peer); err != ErrTooManyWaiting { t.Fatalf("ninth waiting: %v", err) } if ev := drain(o.Sub); len(ev) != 1 || ev[0].Type != TypeFull { t.Fatalf("ninth got %v", types(ev)) } } func TestGuestFirstArrivalIsInTheMembersRoster(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) ev := drain(m.Sub) if len(ev) != 1 || ev[0].Type != TypeRoster || len(ev[0].Roster.Waiting) != 1 || ev[0].Roster.Waiting[0].Peer != g.Peer { t.Fatalf("member roster %+v", ev) } } func TestAdmitRefusesAnExpiredGrantBeforeTheTick(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) authority := map[int64]bool{100: true} h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { if id < 100 { return KindMember, true, nil } return KindGuest, authority[id], nil }}) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) authority[100] = false // grant expired, no tick yet drain(m.Sub) if err := h.Admit(ctx, "r1", "sm", m.Peer, g.Peer); err != nil { t.Fatal(err) } if p, ok := h.Find("r1", "sg", g.Peer); ok && p.Admitted { t.Fatal("expired guest was admitted") } gev := drain(g.Sub) if len(gev) == 0 || gev[len(gev)-1].Type != TypeLost || gev[len(gev)-1].Reason != ReasonExpired { t.Fatalf("expired guest got %v", types(gev)) } if ev := types(drain(m.Sub)); len(ev) != 1 || ev[0] != TypeKnockGone { t.Fatalf("member got %v", ev) } } func TestTickDropsAMemberWhoLostStandingAndPromotesAWaitingGuest(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) kinds := map[int64]Kind{1: KindMember, 100: KindGuest} ok := map[int64]bool{1: true, 100: true} h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { return kinds[id], ok[id], nil }}) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) drain(m.Sub) kinds[100] = KindMember // promoted while waiting h.Tick(ctx) if p, _ := h.Find("r1", "sg", g.Peer); !p.Admitted || p.Kind != KindMember { t.Fatal("waiting guest promoted to member was not admitted on the tick") } ok[1] = false // member removed from the allowlist drain(g.Sub) h.Tick(ctx) if _, found := h.Find("r1", "sm", m.Peer); found { t.Fatal("removed member still in the room") } mev := drain(m.Sub) if len(mev) == 0 || mev[len(mev)-1].Reason != ReasonStanding { t.Fatalf("removed member got %v", types(mev)) } if ev := types(drain(g.Sub)); len(ev) == 0 || ev[len(ev)-1] != TypeGone { t.Fatalf("remaining peer got %v, want gone last", ev) } } // An admitted guest is never ended by their grant expiring: the // participation is the authority now (spec §2.4, ruling 14). func TestJoinKeepsAnAdmittedGuestWhoseGrantExpired(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) authority := map[int64]bool{100: true} h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { if id < 100 { return KindMember, true, nil } return KindGuest, authority[id], nil }}) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) h.Admit(ctx, "r1", "sm", m.Peer, g.Peer) drain(m.Sub) drain(g.Sub) now = now.Add(2 * time.Hour) authority[100] = false // the two-hour grant lapsed mid-call if err := h.Join(ctx, "r1", "sg", g.Peer); err != nil { t.Fatalf("admitted guest re-posting join after expiry: %v", err) } if p, found := h.Find("r1", "sg", g.Peer); !found || !p.Admitted { t.Fatal("an admitted guest was ended by their grant expiring") } if ev := types(drain(m.Sub)); len(ev) != 0 { t.Fatalf("the room was told of a departure that did not happen: %v", ev) } if ev := types(drain(g.Sub)); len(ev) != 0 { t.Fatalf("the admitted guest got %v", ev) } h.Tick(ctx) // and the tick agrees with the request path if p, found := h.Find("r1", "sg", g.Peer); !found || !p.Admitted { t.Fatal("the tick ended an admitted guest whose grant expired") } } // A guest the tick promoted to member while the room was full waits on // as a member and may retry with join (spec §2.4). func TestAWaitingMemberMayRetryJoin(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) kinds := map[int64]Kind{100: KindGuest} h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { if id < 100 { return KindMember, true, nil } return kinds[id], true, nil }}) ctx := context.Background() var members []OpenResult for i := 0; i < MaxAdmitted; i++ { o, _ := h.Open("r1", "m"+string(rune('a'+i)), int64(1+i), "M", KindMember) members = append(members, o) h.Join(ctx, "r1", "m"+string(rune('a'+i)), o.Peer) } g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) kinds[100] = KindMember // promoted while the room is full h.Tick(ctx) p, _ := h.Find("r1", "sg", g.Peer) if p.Admitted || p.Kind != KindMember { t.Fatalf("promoted guest: admitted=%v kind=%v, want waiting as a member", p.Admitted, p.Kind) } if ev := types(drain(g.Sub)); len(ev) != 1 || ev[0] != TypeFull { t.Fatalf("promotion into a full room got %v, want full", ev) } // Retrying while the cap is still held earns another full, not silence. if err := h.Join(ctx, "r1", "sg", g.Peer); err != ErrMeshFull { t.Fatalf("retry into a full room: %v", err) } if ev := types(drain(g.Sub)); len(ev) != 1 || ev[0] != TypeFull { t.Fatalf("retry got %v, want full", ev) } // A slot frees and the same retry admits. h.Leave("r1", "ma", members[0].Peer) if err := h.Join(ctx, "r1", "sg", g.Peer); err != nil { t.Fatal(err) } if p, _ := h.Find("r1", "sg", g.Peer); !p.Admitted { t.Fatal("the retry did not admit the waiting member") } if ev := types(drain(g.Sub)); len(ev) != 1 || ev[0] != TypeRoster { t.Fatalf("admitted retry got %v, want roster", ev) } } // A kind change is announced whichever path notices it: the request one // re-derives standing too, and the tick will never see this change again // (spec §2.4, §2.5). func TestAKindChangeOnTheRequestPathRefreshesRosters(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) kinds := map[int64]Kind{1: KindMember, 2: KindMember} h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { return kinds[id], true, nil }}) ctx := context.Background() a, _ := h.Open("r1", "sa", 1, "Ann", KindMember) h.Join(ctx, "r1", "sa", a.Peer) b, _ := h.Open("r1", "sb", 2, "Bob", KindMember) h.Join(ctx, "r1", "sb", b.Peer) drain(a.Sub) drain(b.Sub) kinds[2] = KindGuest // removed from the allowlist, live grant, no tick yet if err := h.Join(ctx, "r1", "sb", b.Peer); err != nil { t.Fatal(err) } ev := drain(a.Sub) if len(ev) != 1 || ev[0].Type != TypeRoster { t.Fatalf("the other admitted peer got %v, want one roster", types(ev)) } got := "" for _, rp := range ev[0].Roster.Peers { if rp.Peer == b.Peer { got = rp.Kind } } if got != "guest" { t.Fatalf("Ann's roster shows Bob as %q, want guest", got) } // The demoted peer keeps the room but loses the authority to admit. if err := h.Admit(ctx, "r1", "sb", b.Peer, a.Peer); err != ErrNotMember { t.Fatalf("demoted member admitting: %v", err) } } func TestJoinDropsAndAnnouncesAMemberWhoLostStanding(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) standing := map[int64]bool{1: true, 2: true} h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { return KindMember, standing[id], nil }}) ctx := context.Background() a, _ := h.Open("r1", "sa", 1, "Ann", KindMember) h.Join(ctx, "r1", "sa", a.Peer) b, _ := h.Open("r1", "sb", 2, "Bob", KindMember) h.Join(ctx, "r1", "sb", b.Peer) drain(a.Sub) drain(b.Sub) standing[2] = false // removed from the allowlist between joins if err := h.Join(ctx, "r1", "sb", b.Peer); err != ErrNotYours { t.Fatalf("re-join without standing: %v", err) } if _, found := h.Find("r1", "sb", b.Peer); found { t.Fatal("the removed member is still in the room") } bev := drain(b.Sub) if len(bev) == 0 || bev[len(bev)-1].Type != TypeLost || bev[len(bev)-1].Reason != ReasonStanding { t.Fatalf("removed member got %v, want lost/standing last", types(bev)) } if ev := types(drain(a.Sub)); len(ev) != 1 || ev[0] != TypeGone { t.Fatalf("the peer left behind got %v, want gone", ev) } } func TestTakeoverDepartureDependsOnState(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) ag, _ := h.Open("r1", "sag", 100, "Adm", KindGuest) h.Join(ctx, "r1", "sag", ag.Peer) h.Admit(ctx, "r1", "sm", m.Peer, ag.Peer) w, _ := h.Open("r1", "sw", 101, "Wait", KindGuest) h.Join(ctx, "r1", "sw", w.Peer) u, _ := h.Open("r1", "su", 102, "Unj", KindGuest) drain(m.Sub) drain(ag.Sub) // waiting takeover: knock-gone to members only if _, err := h.Takeover("r1", "sw"); err != nil { t.Fatal(err) } if ev := types(drain(ag.Sub)); len(ev) != 0 { t.Fatalf("admitted guest saw a waiting departure: %v", ev) } if ev := types(drain(m.Sub)); len(ev) != 1 || ev[0] != TypeKnockGone { t.Fatalf("member got %v", ev) } // unjoined takeover: nothing h.Takeover("r1", "su") _ = u if ev := types(drain(m.Sub)); len(ev) != 0 { t.Fatalf("unjoined takeover announced %v", ev) } // admitted takeover: gone to admitted peers, old stream lost/replaced h.Takeover("r1", "sag") agev := drain(ag.Sub) if len(agev) == 0 || agev[len(agev)-1].Reason != ReasonReplaced { t.Fatalf("replaced peer got %v", types(agev)) } if ev := types(drain(m.Sub)); len(ev) != 1 || ev[0] != TypeGone { t.Fatalf("member got %v", ev) } if _, err := h.Open("r1", "sag", 100, "Adm", KindGuest); err != nil { t.Fatalf("fresh stream after takeover: %v", err) } } func TestEndSendsEndedToEveryoneAndDropsTheRoom(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() m, _ := h.Open("r1", "sm", 1, "Mia", KindMember) h.Join(ctx, "r1", "sm", m.Peer) g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) h.Join(ctx, "r1", "sg", g.Peer) h.End("r1") for _, s := range []*Sub{m.Sub, g.Sub} { ev := drain(s) if len(ev) == 0 || ev[len(ev)-1].Type != TypeLost || ev[len(ev)-1].Reason != ReasonEnded { t.Fatalf("got %v, want lost/ended last", types(ev)) } } if _, err := h.Open("r1", "sx", 5, "X", KindMember); err == nil { t.Fatal("ended room accepted a stream") } } // Spec §13's "the cap at join and at admit including four queued guests // and concurrent admissions": the mesh cap is checked under the hub's // own lock, inside admitLocked, so five members racing to fill the one // remaining slot cannot between them put five people in a room of four. // // Every call here is a legitimate request that the hub either performed // or refused for the cap, so none of them is an error: the loser learns // it lost from the `full` event on its own stream (§4.3), which is why // this asserts on the room's state rather than on a returned error. func TestConcurrentAdmissionsFillTheLastSlotExactlyOnce(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) h := testHub(&now) ctx := context.Background() // Five member participations open, of which the first three join and // are admitted: three of the four mesh slots taken, one free. Only // those three may admit, so the five goroutines below use them // round-robin — five real callers through three sessions, rather // than one caller five times. actors := make([]struct { session string peer PeerID }, 5) for i := range actors { session := fmt.Sprintf("sm%d", i) m, err := h.Open("r1", session, int64(1+i), "Mia", KindMember) if err != nil { t.Fatalf("open member %d: %v", i, err) } actors[i].session, actors[i].peer = session, m.Peer if i < 3 { if err := h.Join(ctx, "r1", session, m.Peer); err != nil { t.Fatalf("join member %d: %v", i, err) } } } if got := admittedNow(t, h, "r1"); got != 3 { t.Fatalf("setup left %d admitted, want 3", got) } // The two members that did not join are still members with a live // participation, but they are not admitted, so they may not admit: // only the three that joined are used as actors below. guests := make([]struct { session string peer PeerID }, 5) for i := range guests { session := fmt.Sprintf("sg%d", i) g, err := h.Open("r1", session, int64(100+i), "Gus", KindGuest) if err != nil { t.Fatalf("open guest %d: %v", i, err) } if err := h.Join(ctx, "r1", session, g.Peer); err != nil { t.Fatalf("guest %d knock: %v", i, err) } guests[i].session, guests[i].peer = session, g.Peer } var wg sync.WaitGroup errs := make([]error, len(guests)) start := make(chan struct{}) for i := range guests { wg.Add(1) go func(i int) { defer wg.Done() a := actors[i%3] // the three admitted members, round-robin <-start errs[i] = h.Admit(ctx, "r1", a.session, a.peer, guests[i].peer) }(i) } close(start) wg.Wait() for i, err := range errs { if err != nil { t.Fatalf("admit %d: %v", i, err) } } admitted := 0 for _, g := range guests { p, ok := h.Find("r1", g.session, g.peer) if !ok { t.Fatalf("a guest vanished: %s", g.peer) } if p.Admitted { admitted++ } } if admitted != 1 { t.Fatalf("%d of five guests were let into one free slot", admitted) } if got := admittedNow(t, h, "r1"); got != MaxAdmitted { t.Fatalf("the room holds %d admitted, want %d", got, MaxAdmitted) } } // admittedNow counts the room's admitted participations through the // hub's own lock, without reaching past its API for a field. Since // slice 5 that is exactly AdmittedCount, the drive surface's own // reader (count.go), so this is its name in the tests that predate it // rather than a second copy of the same walk. func admittedNow(t *testing.T, h *Hub, sqid string) int { t.Helper() return h.AdmittedCount(sqid) } // Spec §2.4 and ruling 14: a loss of standing costs what the kind held // earns, and standingLostLocked is the one place that decides which. // Admit's expired-target path used to call endLocked(ReasonExpired) // directly, which is right for a guest and wrong for the one target // that is waiting and a member — the tick promotes a waiting guest to // member and leaves it waiting when the mesh is full (§2.4's retry), and // that participation is owed reason "standing". func TestAdmitOfAWaitingMemberWhoLostStandingEndsItAsAMember(t *testing.T) { now := time.Date(2026, 9, 10, 9, 0, 0, 0, time.UTC) kinds := map[int64]Kind{100: KindGuest} authority := map[int64]bool{100: true} h := New(Config{Now: func() time.Time { return now }, StandingFor: func(ctx context.Context, room string, id int64) (Kind, bool, error) { if id < 100 { return KindMember, true, nil } return kinds[id], authority[id], nil }}) ctx := context.Background() // Four admitted members: the mesh is full, so the promotion below // cannot admit and the guest waits on as a member. var actor OpenResult for i := 0; i < MaxAdmitted; i++ { session := fmt.Sprintf("sm%d", i) m, _ := h.Open("r1", session, int64(1+i), "Mia", KindMember) if err := h.Join(ctx, "r1", session, m.Peer); err != nil { t.Fatalf("join member %d: %v", i, err) } if i == 0 { actor = m } } g, _ := h.Open("r1", "sg", 100, "Gus", KindGuest) if err := h.Join(ctx, "r1", "sg", g.Peer); err != nil { t.Fatal(err) } kinds[100] = KindMember // promoted while waiting h.Tick(ctx) p, ok := h.Find("r1", "sg", g.Peer) if !ok || p.Admitted || p.Kind != KindMember { t.Fatalf("setup: waiting member is %+v (admitted=%v)", p, ok && p.Admitted) } drain(g.Sub) // Standing goes before anyone gets round to admitting them. authority[100] = false if err := h.Admit(ctx, "r1", "sm0", actor.Peer, g.Peer); err != nil { t.Fatalf("admit: %v", err) } if _, ok := h.Find("r1", "sg", g.Peer); ok { t.Fatal("a target with no standing was left in the room") } ev := drain(g.Sub) if len(ev) == 0 || ev[len(ev)-1].Type != TypeLost { t.Fatalf("the dropped target got %v", types(ev)) } if got := ev[len(ev)-1].Reason; got != ReasonStanding { t.Fatalf("a waiting member who lost standing was told %q, want %q", got, ReasonStanding) } }