add Feed test

This commit is contained in:
2024-08-22 16:13:50 +01:00
parent 00d98a9185
commit b56a4180d0

View File

@@ -2,6 +2,7 @@ package gubgub
import (
"context"
"sync"
"testing"
"time"
@@ -248,6 +249,48 @@ func TestAsyncTopic_AllPublishedBeforeClosedAreDeliveredAfterClosed(t *testing.T
}
}
func TestAsyncTopic_Feed(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
subscriberReady := make(chan struct{}, 1)
defer close(subscriberReady)
topic := NewAsyncTopic[int](ctx,
WithOnSubscribe(func(count int) {
subscriberReady <- struct{}{}
}),
)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
seen := make(map[int]struct{})
for i := range topic.Feed() {
seen[i] = struct{}{}
if len(seen) >= 9 {
return
}
}
}()
<-subscriberReady
wg.Add(1)
go func() {
defer wg.Done()
for i := range 10 {
topic.Publish(i)
}
}()
wg.Wait()
}
func testTimer(t testing.TB, d time.Duration) *time.Timer {
t.Helper()