improved mem & cpu consumption

This commit is contained in:
2024-08-22 11:54:57 +01:00
parent 938217dd64
commit 7fd9c030c0
10 changed files with 220 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ package gubgub
import (
"context"
"sync"
"testing"
"time"
@@ -55,10 +56,13 @@ func TestAsyncTopic_MultiPublishersMultiSubscribers(t *testing.T) {
msgCount = pubCount * 100 // total messages to publish (delivered to EACH subscriber)
)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
subscribersReady := make(chan struct{}, 1)
defer close(subscribersReady)
topic := NewAsyncTopic[int](context.Background(), WithOnSubscribe(func(count int) {
topic := NewAsyncTopic[int](ctx, WithOnSubscribe(func(count int) {
if count == subCount {
subscribersReady <- struct{}{}
}
@@ -245,6 +249,50 @@ 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()
time.Sleep(time.Second)
}
func testTimer(t testing.TB, d time.Duration) *time.Timer {
t.Helper()