You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/12/09 08:48:26 UTC

[GitHub] [pulsar-client-go] RobertIndie commented on a diff in pull request #904: [feat] support for consumer event listener

RobertIndie commented on code in PR #904:
URL: https://github.com/apache/pulsar-client-go/pull/904#discussion_r1044204949


##########
pulsar/consumer_test.go:
##########
@@ -384,6 +385,138 @@ func TestPartitionTopicsConsumerPubSub(t *testing.T) {
 	assert.Equal(t, len(msgs), 10)
 }
 
+type TestActiveConsumerListener struct {
+	lock             sync.RWMutex
+	nameToPartitions map[string]map[int32]struct{}
+}
+
+func (t *TestActiveConsumerListener) getConsumerCount() int {
+	t.lock.RLock()
+	defer t.lock.RUnlock()
+	return len(t.nameToPartitions)
+}
+
+func (t *TestActiveConsumerListener) getPartitionCount(consumerName string) int {
+	t.lock.RLock()
+	defer t.lock.RUnlock()
+	return len(t.nameToPartitions[consumerName])
+}
+
+func (t *TestActiveConsumerListener) BecameActive(consumer Consumer, topicName string, partition int32) {
+	fmt.Printf("%s become active on %s - %d\n", consumer.Name(), topicName, partition)

Review Comment:
   Better to use the `Logf` method in `testing.T`



##########
pulsar/consumer_test.go:
##########
@@ -384,6 +385,138 @@ func TestPartitionTopicsConsumerPubSub(t *testing.T) {
 	assert.Equal(t, len(msgs), 10)
 }
 
+type TestActiveConsumerListener struct {
+	lock             sync.RWMutex
+	nameToPartitions map[string]map[int32]struct{}
+}
+
+func (t *TestActiveConsumerListener) getConsumerCount() int {
+	t.lock.RLock()
+	defer t.lock.RUnlock()
+	return len(t.nameToPartitions)
+}
+
+func (t *TestActiveConsumerListener) getPartitionCount(consumerName string) int {
+	t.lock.RLock()
+	defer t.lock.RUnlock()
+	return len(t.nameToPartitions[consumerName])
+}
+
+func (t *TestActiveConsumerListener) BecameActive(consumer Consumer, topicName string, partition int32) {
+	fmt.Printf("%s become active on %s - %d\n", consumer.Name(), topicName, partition)
+	t.lock.Lock()
+	defer t.lock.Unlock()
+	partitionSet := t.nameToPartitions[consumer.Name()]
+	if partitionSet == nil {
+		partitionSet = map[int32]struct{}{}
+	}
+	partitionSet[partition] = struct{}{}
+	t.nameToPartitions[consumer.Name()] = partitionSet
+}
+
+func (t *TestActiveConsumerListener) BecameInactive(consumer Consumer, topicName string, partition int32) {
+	fmt.Printf("%s become inactive on %s - %d\n", consumer.Name(), topicName, partition)
+	t.lock.Lock()
+	defer t.lock.Unlock()
+	partitionSet := t.nameToPartitions[consumer.Name()]
+	if _, ok := partitionSet[partition]; ok {
+		delete(partitionSet, partition)
+		if len(partitionSet) == 0 {
+			delete(t.nameToPartitions, consumer.Name())
+		}
+	}
+}
+
+func allConsume(consumers []Consumer) {
+	for i := 0; i < len(consumers); i++ {
+		ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
+		defer cancel()
+		consumers[i].Receive(ctx)
+	}
+}
+
+func TestPartitionTopic_ActiveConsumerChanged(t *testing.T) {
+	client, err := NewClient(ClientOptions{
+		URL: lookupURL,
+	})
+	assert.Nil(t, err)
+	defer client.Close()
+
+	topic := "persistent://public/default/testGetPartitions6"

Review Comment:
   Better to add timestamp as the topic name suffix. Like `fmt.Sprintf("my-topic-%v", time.Now().Nanosecond())` to avoid conflict when running multiple times. You can also use `newTopicName` in helper_for_test.go



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org