Add SSE broadcaster
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user