package main import ( "strings" "testing" ) // addOutput must never silently drop a chunk for a viewer that fell behind: // terminal output is a byte stream, so a dropped chunk garbles everything // after it. Instead, a subscriber whose 256-slot buffer overflows is // force-detached (removed from ts.subs and closed) while subscribers with // room keep receiving, and the closed channel still yields its buffered // messages (ok=true) before reporting closed (ok=false) — which is exactly // the resync trigger the attach loop waits on. func TestAddOutputForceDetachesFullSubscriber(t *testing.T) { ts := &terminalSession{ owner: "resync-test@example.com", id: "resync-test-session-1", subs: make(map[chan wsMessage]struct{}), } // Register in the global map under the same key the service uses, then // clean up so other tests never observe this entry. terminalSessions.Lock() terminalSessions.items[terminalSessionKey(ts.owner, ts.id)] = ts terminalSessions.Unlock() t.Cleanup(func() { terminalSessions.Lock() delete(terminalSessions.items, terminalSessionKey(ts.owner, ts.id)) terminalSessions.Unlock() }) slow, _, ok := ts.subscribe() if !ok { t.Fatal("precondition: subscribe on a live session must succeed") } // Exactly fill the slow viewer's 256-slot buffer: still subscribed, not // yet force-detached, nothing dropped. for i := 0; i < 256; i++ { ts.addOutput("chunk") } ts.mu.Lock() _, slowSubscribed := ts.subs[slow] ts.mu.Unlock() if !slowSubscribed || len(ts.subs) != 1 { t.Fatalf("a merely-full buffer must not detach the viewer (subscribed=%v, subs=%d)", slowSubscribed, len(ts.subs)) } // A second viewer joins with an empty buffer, then one more chunk // overflows the slow viewer's buffer. fast, _, ok := ts.subscribe() if !ok { t.Fatal("precondition: second subscribe must succeed") } ts.addOutput("overflow") ts.mu.Lock() _, slowSubscribed = ts.subs[slow] _, fastSubscribed := ts.subs[fast] ts.mu.Unlock() if slowSubscribed { t.Fatal("overflowing subscriber must be removed from ts.subs") } if !fastSubscribed || len(ts.subs) != 1 { t.Fatalf("subscriber with room must stay subscribed (subscribed=%v, subs=%d)", fastSubscribed, len(ts.subs)) } select { case msg := <-fast: if msg.Type != "output" || msg.Data != "overflow" { t.Fatalf("live viewer must keep receiving: got type=%q data=%q", msg.Type, msg.Data) } default: t.Fatal("live viewer must receive the chunk that overflowed the slow viewer") } // The replay buffer kept every byte (including the chunk the slow viewer // missed), which is what makes the replay-based resync complete. if got := string(ts.replay); !strings.HasSuffix(got, "overflow") || strings.Count(got, "chunk") != 256 { t.Fatalf("replay buffer must retain the full stream including the missed chunk (len=%d)", len(got)) } // Draining the closed channel must yield the 256 buffered messages first // (ok=true, in order), then report closed (ok=false) — the attach loop's // resync trigger. The overflowing chunk must NOT be in the channel. for i := 0; i < 256; i++ { msg, ok := <-slow if !ok { t.Fatalf("buffered message %d was dropped: channel closed early", i) } if msg.Data != "chunk" { t.Fatalf("buffered message %d = %q, want %q", i, msg.Data, "chunk") } } if msg, ok := <-slow; ok { t.Fatalf("channel must report closed after the buffer drained, got ok=true (data=%q)", msg.Data) } // unsubscribe for an already-removed (and closed) channel is a no-op // delete — must not panic or close anything. ts.unsubscribe(slow) ts.mu.Lock() remaining := len(ts.subs) lastDetach := ts.lastDetach.IsZero() ts.mu.Unlock() if remaining != 1 { t.Fatalf("forced detach must already have removed the slow viewer (subs=%d)", remaining) } if lastDetach { t.Fatal("unsubscribe must refresh lastDetach") } ts.unsubscribe(fast) ts.mu.Lock() remaining = len(ts.subs) ts.mu.Unlock() if remaining != 0 { t.Fatalf("unsubscribe must remove the last viewer (subs=%d)", remaining) } } // A closed session stops fanning out entirely; the forced-detach path must // not resurrect or double-close anything on a session that already closed. func TestAddOutputAfterCloseStillSafe(t *testing.T) { ts := &terminalSession{ owner: "resync-closed-test@example.com", id: "resync-closed-session-1", done: make(chan struct{}), subs: make(map[chan wsMessage]struct{}), } terminalSessions.Lock() terminalSessions.items[terminalSessionKey(ts.owner, ts.id)] = ts terminalSessions.Unlock() t.Cleanup(func() { terminalSessions.Lock() delete(terminalSessions.items, terminalSessionKey(ts.owner, ts.id)) terminalSessions.Unlock() }) ch, _, ok := ts.subscribe() if !ok { t.Fatal("precondition: subscribe on a live session must succeed") } ts.closeWithMessage("") ts.addOutput("ignored-after-close") // must not panic, must not deliver // The close path never closes subscriber channels (a channel is closed // only by addOutput's full-buffer force-detach) and never delivers to // them afterwards: a closed+empty channel would make the receive below // ready with ok=false and hit the fatal, so reaching default proves the // channel is open and untouched. select { case msg, ok := <-ch: t.Fatalf("a closed session must not deliver output (got data=%q ok=%v)", msg.Data, ok) default: } // closeWithMessage leaves the subscriber in ts.subs — its attach loop // notices ts.done and detaches through the deferred unsubscribe, so the // close path must not close or remove the channel behind its back. ts.mu.Lock() remaining := len(ts.subs) closed := ts.closed ts.mu.Unlock() if !closed { t.Fatal("closeWithMessage must mark the session closed") } if remaining != 1 { t.Fatalf("closeWithMessage must leave the detach to the viewer's attach loop (subs=%d)", remaining) } ts.unsubscribe(ch) ts.mu.Lock() remaining = len(ts.subs) ts.mu.Unlock() if remaining != 0 { t.Fatalf("deferred-style unsubscribe must drain the last viewer (subs=%d)", remaining) } }