feat(master): in-process broker for live UI change signals
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBroker_DeliversToAllSubscribers(t *testing.T) {
|
||||
b := NewBroker()
|
||||
a, cancelA := b.Subscribe()
|
||||
defer cancelA()
|
||||
c, cancelC := b.Subscribe()
|
||||
defer cancelC()
|
||||
|
||||
b.Publish(Services)
|
||||
assert.Equal(t, Services, <-a)
|
||||
assert.Equal(t, Services, <-c)
|
||||
}
|
||||
|
||||
func TestBroker_CancelStopsDelivery(t *testing.T) {
|
||||
b := NewBroker()
|
||||
ch, cancel := b.Subscribe()
|
||||
cancel()
|
||||
cancel() // safe to call twice
|
||||
b.Publish(Agents)
|
||||
assert.Empty(t, ch)
|
||||
}
|
||||
|
||||
func TestBroker_SlowSubscriberDoesNotBlockPublish(t *testing.T) {
|
||||
b := NewBroker()
|
||||
_, cancel := b.Subscribe()
|
||||
defer cancel()
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
for range 1000 {
|
||||
b.Publish(Services) // must return even though nobody reads
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Publish blocked on a slow subscriber")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user