labuladong commented on code in PR #904:
URL: https://github.com/apache/pulsar-client-go/pull/904#discussion_r1044943070
##########
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:
Great, I've change it!
##########
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:
Great, I've changed it!
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]