f243f3004e
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
43 lines
801 B
Go
43 lines
801 B
Go
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")
|
|
}
|
|
}
|