From 1b12971382728249ca9831c69d444ccc9b459610 Mon Sep 17 00:00:00 2001 From: HolyKingCrusader Date: Mon, 27 May 2024 22:53:33 +0200 Subject: [PATCH 1/4] Adds feature to use ULID instead of UUID in topic name and consumer group --- pubsub/tests/bench_pubsub.go | 2 +- pubsub/tests/test_pubsub.go | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/pubsub/tests/bench_pubsub.go b/pubsub/tests/bench_pubsub.go index a27ff6afc..f55cb4aa8 100644 --- a/pubsub/tests/bench_pubsub.go +++ b/pubsub/tests/bench_pubsub.go @@ -15,7 +15,7 @@ type BenchmarkPubSubConstructor func(n int) (message.Publisher, message.Subscrib // BenchSubscriber runs benchmark on a message Subscriber. func BenchSubscriber(b *testing.B, pubSubConstructor BenchmarkPubSubConstructor) { pub, sub := pubSubConstructor(b.N) - topicName := testTopicName(NewTestID()) + topicName := testTopicName(NewTestID(false)) messages, err := sub.Subscribe(context.Background(), topicName) if err != nil { diff --git a/pubsub/tests/test_pubsub.go b/pubsub/tests/test_pubsub.go index 4defb7633..a628437d5 100644 --- a/pubsub/tests/test_pubsub.go +++ b/pubsub/tests/test_pubsub.go @@ -121,6 +121,9 @@ type Features struct { // NewSubscriberReceivesOldMessages should be set to true if messages are persisted even // if they are already consumed (for example, like in Kafka). NewSubscriberReceivesOldMessages bool + + // UseULID should be set to true if ULID should be used instead of UUID + UseULID bool } // RunOnlyFastTests returns true if -short flag was provided -race was not provided. @@ -151,7 +154,10 @@ func getTestName(testFunc interface{}) string { type TestID string // NewTestID returns a new unique TestID. -func NewTestID() TestID { +func NewTestID(useULID bool) TestID { + if useULID { + return TestID(watermill.NewULID()) + } return TestID(watermill.NewUUID()) } @@ -175,7 +181,7 @@ func runTest( if parallel { t.Parallel() } - testID := NewTestID() + testID := NewTestID(features.UseULID) t.Run(string(testID), func(t *testing.T) { tCtx := TestContext{ @@ -804,8 +810,8 @@ func TestConsumerGroups( } totalMessagesCount := 50 - group1 := generateConsumerGroup(t, pubSubConstructor, topicName) - group2 := generateConsumerGroup(t, pubSubConstructor, topicName) + group1 := generateConsumerGroup(t, pubSubConstructor, topicName, tCtx.Features.UseULID) + group2 := generateConsumerGroup(t, pubSubConstructor, topicName, tCtx.Features.UseULID) messagesToPublish := PublishSimpleMessages(t, totalMessagesCount, publisherPub, topicName) @@ -1225,8 +1231,13 @@ func closePubSub(t *testing.T, pub message.Publisher, sub message.Subscriber) { require.NoError(t, err) } -func generateConsumerGroup(t *testing.T, pubSubConstructor ConsumerGroupPubSubConstructor, topicName string) string { - groupName := "cg_" + watermill.NewUUID() +func generateConsumerGroup(t *testing.T, pubSubConstructor ConsumerGroupPubSubConstructor, topicName string, useULID bool) string { + groupName := "cg_" + if useULID { + groupName += watermill.NewULID() + } else { + groupName += watermill.NewUUID() + } // create a pubsub to ensure that the consumer group exists // for those providers that require subscription before publishing messages (e.g. Google Cloud PubSub) From 640974c52d2614c5ebfa89af0d29e045f9fe7970 Mon Sep 17 00:00:00 2001 From: HolyKingCrusader Date: Wed, 29 May 2024 13:32:19 +0200 Subject: [PATCH 2/4] split ULID id generation into a seperate function --- pubsub/tests/bench_pubsub.go | 2 +- pubsub/tests/test_pubsub.go | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pubsub/tests/bench_pubsub.go b/pubsub/tests/bench_pubsub.go index f55cb4aa8..a27ff6afc 100644 --- a/pubsub/tests/bench_pubsub.go +++ b/pubsub/tests/bench_pubsub.go @@ -15,7 +15,7 @@ type BenchmarkPubSubConstructor func(n int) (message.Publisher, message.Subscrib // BenchSubscriber runs benchmark on a message Subscriber. func BenchSubscriber(b *testing.B, pubSubConstructor BenchmarkPubSubConstructor) { pub, sub := pubSubConstructor(b.N) - topicName := testTopicName(NewTestID(false)) + topicName := testTopicName(NewTestID()) messages, err := sub.Subscribe(context.Background(), topicName) if err != nil { diff --git a/pubsub/tests/test_pubsub.go b/pubsub/tests/test_pubsub.go index a628437d5..337096201 100644 --- a/pubsub/tests/test_pubsub.go +++ b/pubsub/tests/test_pubsub.go @@ -122,7 +122,7 @@ type Features struct { // if they are already consumed (for example, like in Kafka). NewSubscriberReceivesOldMessages bool - // UseULID should be set to true if ULID should be used instead of UUID + // UseULID should be set to true if ULID should be used instead of UUID for generating test IDs. UseULID bool } @@ -154,12 +154,12 @@ func getTestName(testFunc interface{}) string { type TestID string // NewTestID returns a new unique TestID. -func NewTestID(useULID bool) TestID { - if useULID { - return TestID(watermill.NewULID()) - } +func NewTestID() TestID { return TestID(watermill.NewUUID()) } +func NewTestID_ULID() TestID { + return TestID(watermill.NewULID()) +} // TestContext is a collection of values that belong to a single test. type TestContext struct { @@ -181,7 +181,7 @@ func runTest( if parallel { t.Parallel() } - testID := NewTestID(features.UseULID) + testID := NewTestID() t.Run(string(testID), func(t *testing.T) { tCtx := TestContext{ From 94113facd45b0e40ba49c93bb617d0c90f742826 Mon Sep 17 00:00:00 2001 From: HolyKingCrusader Date: Sat, 1 Jun 2024 17:38:29 +0200 Subject: [PATCH 3/4] changed func name --- pubsub/tests/test_pubsub.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubsub/tests/test_pubsub.go b/pubsub/tests/test_pubsub.go index 337096201..aadb1de8a 100644 --- a/pubsub/tests/test_pubsub.go +++ b/pubsub/tests/test_pubsub.go @@ -157,7 +157,7 @@ type TestID string func NewTestID() TestID { return TestID(watermill.NewUUID()) } -func NewTestID_ULID() TestID { +func NewTestULID() TestID { return TestID(watermill.NewULID()) } From 247f2e9e75b87c262cd6650ada13af2e2599a73f Mon Sep 17 00:00:00 2001 From: HolyKingCrusader Date: Mon, 3 Jun 2024 14:22:20 +0200 Subject: [PATCH 4/4] added a field for setting function used in ID generation --- pubsub/tests/test_pubsub.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pubsub/tests/test_pubsub.go b/pubsub/tests/test_pubsub.go index aadb1de8a..ebd1320e7 100644 --- a/pubsub/tests/test_pubsub.go +++ b/pubsub/tests/test_pubsub.go @@ -124,6 +124,9 @@ type Features struct { // UseULID should be set to true if ULID should be used instead of UUID for generating test IDs. UseULID bool + + // TestIDGenerator determines which function should be used for generating test IDs, NewTestID is used by default. + TestIDGenerator func() TestID `default:"NewTestID()"` } // RunOnlyFastTests returns true if -short flag was provided -race was not provided. @@ -157,6 +160,7 @@ type TestID string func NewTestID() TestID { return TestID(watermill.NewUUID()) } +// NewTestULID returns a new unique TestID using ULID. func NewTestULID() TestID { return TestID(watermill.NewULID()) }