major refactor to accommodate feed

This commit is contained in:
2024-09-09 11:22:00 +01:00
parent 4741acc016
commit a394dab041
9 changed files with 360 additions and 223 deletions

62
feed_test.go Normal file
View File

@@ -0,0 +1,62 @@
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(count int) {
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) {
feedback := make(chan int)
go func() {
for i := range Feed(tc.topic, false) {
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)
}
}
})
}
}