65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package gubgub
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestFeed_Topics(t *testing.T) {
|
|
const msgCount = 10
|
|
|
|
subscriberReady := make(chan struct{}, 1)
|
|
defer close(subscriberReady)
|
|
|
|
onSubscribe := WithOnSubscribe(func() {
|
|
subscriberReady <- struct{}{}
|
|
})
|
|
|
|
testCases := []struct {
|
|
name string
|
|
topic Topic[int]
|
|
}{
|
|
{
|
|
name: "sync topic",
|
|
topic: NewSyncTopic[int](onSubscribe),
|
|
},
|
|
{
|
|
name: "async topic",
|
|
topic: NewAsyncTopic[int](onSubscribe),
|
|
},
|
|
}
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
feed, err := Feed(tc.topic, false)
|
|
require.NoError(t, err)
|
|
|
|
feedback := make(chan int)
|
|
go func() {
|
|
for i := range feed {
|
|
feedback <- i
|
|
}
|
|
}()
|
|
|
|
go func() {
|
|
<-subscriberReady
|
|
for i := range msgCount {
|
|
require.NoError(t, tc.topic.Publish(i))
|
|
}
|
|
}()
|
|
|
|
var counter int
|
|
timeout := testTimer(t, time.Second)
|
|
for counter < msgCount {
|
|
select {
|
|
case <-feedback:
|
|
counter++
|
|
case <-timeout.C:
|
|
t.Fatalf("expected %d feedback values by now but only got %d", msgCount, counter)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|