refactor some tests

This commit is contained in:
2024-09-09 16:58:47 +01:00
parent 9283306006
commit 8e048013d9
3 changed files with 10 additions and 13 deletions

View File

@@ -32,7 +32,7 @@ func consumer(msg MyMessage) {
fmt.Printf("Hello %s", msg.Name)
}
func m2ain() {
func main() {
topic := gubgub.NewAsyncTopic[MyMessage]()
defer topic.Close() // Returns after all messages are delivered

View File

@@ -10,7 +10,8 @@ func BenchmarkAsyncTopic_Publish(b *testing.B) {
for _, tc := range publishCases {
b.Run(tc.Name, func(b *testing.B) {
onSubscribe, subscribersReady := withNotifyOnNthSubscriber(b, int64(tc.Count))
topic := newTestAsyncTopic[int](b, onSubscribe)
topic := NewAsyncTopic[int](onSubscribe)
b.Cleanup(topic.Close)
for range tc.Count {
require.NoError(b, topic.Subscribe(tc.Subscriber))

View File

@@ -13,7 +13,8 @@ func TestAsyncTopic_SinglePublisherSingleSubscriber(t *testing.T) {
const msgCount = 10
onSubscribe, subscriberReady := withNotifyOnNthSubscriber(t, 1)
topic := newTestAsyncTopic[int](t, onSubscribe)
topic := NewAsyncTopic[int](onSubscribe)
t.Cleanup(topic.Close)
feedback := make(chan struct{}, msgCount)
defer close(feedback)
@@ -52,7 +53,8 @@ func TestAsyncTopic_MultiPublishersMultiSubscribers(t *testing.T) {
)
onSubscribe, subscribersReady := withNotifyOnNthSubscriber(t, subCount)
topic := newTestAsyncTopic[int](t, onSubscribe)
topic := NewAsyncTopic[int](onSubscribe)
t.Cleanup(topic.Close)
expFeedbackCount := msgCount * subCount
feedback := make(chan int, expFeedbackCount)
@@ -172,11 +174,12 @@ func TestAsyncTopic_ClosedTopicError(t *testing.T) {
}
}
func TestAsyncTopic_AllPublishedBeforeClosedAreDeliveredAfterClosed(t *testing.T) {
func TestAsyncTopic_AllPublishedBeforeClosedAreDelivered(t *testing.T) {
const msgCount = 10
onSubscribe, subscriberReady := withNotifyOnNthSubscriber(t, 1)
topic := newTestAsyncTopic[int](t, onSubscribe)
topic := NewAsyncTopic[int](onSubscribe)
t.Cleanup(topic.Close)
feedback := make(chan int) // unbuffered will cause choke point for publishers
defer close(feedback)
@@ -220,13 +223,6 @@ func testTimer(t testing.TB, d time.Duration) *time.Timer {
return timer
}
func newTestAsyncTopic[T any](t testing.TB, opts ...TopicOption) *AsyncTopic[T] {
t.Helper()
topic := NewAsyncTopic[T](opts...)
t.Cleanup(topic.Close)
return topic
}
func withNotifyOnNthSubscriber(t testing.TB, n int64) (TopicOption, <-chan struct{}) {
t.Helper()