Add SSE broadcaster

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-05-31 11:49:57 -04:00
parent a8216095de
commit f243f3004e
2 changed files with 84 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
package web
import (
"testing"
"time"
)
func TestBroadcasterDeliversToSubscribers(t *testing.T) {
b := NewBroadcaster()
ch := b.Subscribe()
defer b.Unsubscribe(ch)
b.Publish("hello")
select {
case msg := <-ch:
if msg != "hello" {
t.Fatalf("got %q want hello", msg)
}
case <-time.After(time.Second):
t.Fatal("subscriber did not receive message")
}
}
func TestBroadcasterDropsWhenSubscriberFull(t *testing.T) {
b := NewBroadcaster()
ch := b.Subscribe()
defer b.Unsubscribe(ch)
// Publishing many messages without reading must not block (slow client).
done := make(chan struct{})
go func() {
for i := 0; i < 100; i++ {
b.Publish("x")
}
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("Publish blocked on a full subscriber")
}
}