12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- package broadcaster_test
- import (
- "context"
- "testing"
- "github.com/iwanhae/nodb/pkg/broadcaster"
- "github.com/stretchr/testify/assert"
- )
- func TestNewBroadcaster(t *testing.T) {
- b := broadcaster.NewBroadcaster[int](50)
- if b == nil {
- t.Error("NewBroadcaster should not return nil")
- }
- chA := b.Subscribe()
- chB := b.Subscribe()
- ctx := context.Background()
- b.Publish(ctx, 1)
- b.Publish(ctx, 2)
- b.Publish(ctx, 3)
- idx := 1
- loopA:
- for {
- select {
- case val := <-chA:
- assert.Equal(t, idx, val)
- idx += 1
- default:
- break loopA
- }
- }
- idx = 1
- loopB:
- for {
- select {
- case val := <-chB:
- assert.Equal(t, idx, val)
- idx += 1
- default:
- break loopB
- }
- }
- }
- func TestNewBroadcaster_auto_close(t *testing.T) {
- b := broadcaster.NewBroadcaster[int](50)
- if b == nil {
- t.Error("NewBroadcaster should not return nil")
- }
- ch := b.Subscribe()
- ctx := context.Background()
- for i := 0; i < 52; i++ {
- b.Publish(ctx, i)
- }
- for i := 0; i < 50; i++ {
- _, ok := <-ch
- assert.True(t, ok)
- }
- _, ok := <-ch
- assert.False(t, ok)
- }
|