nodece commented on code in PR #1301:
URL: https://github.com/apache/pulsar-client-go/pull/1301#discussion_r1825570231


##########
pulsar/consumer_test.go:
##########
@@ -4691,3 +4691,115 @@ func TestPartitionConsumerGetLastMessageIDs(t 
*testing.T) {
        }
 
 }
+
+func TestAckIDList(t *testing.T) {
+       for _, params := range []bool{true, false} {
+               t.Run(fmt.Sprintf("TestAckIDList_%v", params), func(t 
*testing.T) {
+                       runAckIDListTest(t, params)
+               })
+       }
+}
+
+func runAckIDListTest(t *testing.T, enableBatchIndexAck bool) {
+       client, err := NewClient(ClientOptions{URL: lookupURL})
+       assert.Nil(t, err)
+       defer client.Close()
+
+       topic := fmt.Sprintf("test-ack-id-list-%v", time.Now().Nanosecond())
+
+       consumer := createSharedConsumer(t, client, topic, enableBatchIndexAck)
+       sendMessages(t, client, topic, 0, 5, true)  // entry 0: [0, 1, 2, 3, 4]
+       sendMessages(t, client, topic, 5, 3, false) // entry 2: [5], 3: [6], 4: 
[7]
+       sendMessages(t, client, topic, 8, 2, true)  // entry 5: [8, 9]
+
+       msgs := receiveMessages(t, consumer, 10)
+       originalMsgIDs := make([]MessageID, 0)
+       for i := 0; i < 10; i++ {
+               originalMsgIDs = append(originalMsgIDs, msgs[i].ID())
+               assert.Equal(t, fmt.Sprintf("msg-%d", i), 
string(msgs[i].Payload()))
+       }
+
+       ackedIndexes := []int{0, 2, 3, 6, 8, 9}
+       unackedIndexes := []int{1, 4, 5, 7}
+       if !enableBatchIndexAck {
+               // [0, 4] is the first batch range but only partial of it is 
acked
+               unackedIndexes = []int{0, 1, 2, 3, 4, 5, 7}
+       }
+       msgIDs := make([]MessageID, len(ackedIndexes))
+       for i := 0; i < len(ackedIndexes); i++ {
+               msgIDs[i] = msgs[ackedIndexes[i]].ID()
+       }
+       assert.Empty(t, consumer.AckIDList(msgIDs))
+       consumer.Close()
+
+       consumer = createSharedConsumer(t, client, topic, enableBatchIndexAck)
+       msgs = receiveMessages(t, consumer, len(unackedIndexes))
+       for i := 0; i < len(unackedIndexes); i++ {
+               assert.Equal(t, fmt.Sprintf("msg-%d", unackedIndexes[i]), 
string(msgs[i].Payload()))
+       }
+
+       if !enableBatchIndexAck {
+               msgIDs = make([]MessageID, 0)
+               for i := 0; i < 5; i++ {
+                       msgIDs = append(msgIDs, originalMsgIDs[i])
+               }
+               consumer.AckIDList(msgIDs)
+               consumer.Close()
+
+               consumer = createSharedConsumer(t, client, topic, 
enableBatchIndexAck)
+               msgs = receiveMessages(t, consumer, 2)
+               assert.Equal(t, "msg-5", string(msgs[0].Payload()))
+               assert.Equal(t, "msg-7", string(msgs[1].Payload()))
+               consumer.Close()
+       }
+}
+
+func createSharedConsumer(t *testing.T, client Client, topic string, 
enableBatchIndexAck bool) Consumer {
+       consumer, err := client.Subscribe(ConsumerOptions{
+               Topic:                          topic,
+               SubscriptionName:               "my-sub",
+               SubscriptionInitialPosition:    SubscriptionPositionEarliest,
+               Type:                           Shared,
+               EnableBatchIndexAcknowledgment: enableBatchIndexAck,
+               AckWithResponse:                true,
+       })
+       assert.Nil(t, err)
+       return consumer
+}
+
+func sendMessages(t *testing.T, client Client, topic string, startIndex int, 
numMessages int, batching bool) {
+       producer, err := client.CreateProducer(ProducerOptions{
+               Topic:           topic,
+               DisableBatching: !batching,
+       })
+       assert.Nil(t, err)
+       defer producer.Close()
+
+       ctx := context.Background()
+       for i := 0; i < numMessages; i++ {
+               msg := &ProducerMessage{Payload: []byte(fmt.Sprintf("msg-%d", 
startIndex+i))}
+               if batching {
+                       producer.SendAsync(ctx, msg, func(_ MessageID, _ 
*ProducerMessage, _ error) {})
+               } else {
+                       producer.Send(ctx, msg)
+               }
+       }
+       producer.Flush()
+}
+
+func receiveMessages(t *testing.T, consumer Consumer, numMessages int) 
[]Message {
+       ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
+       defer cancel()
+       msgs := make([]Message, 0)
+       for i := 0; i < numMessages; i++ {
+               if msg, err := consumer.Receive(ctx); err == nil {
+                       fmt.Println("Received message: ", string(msg.Payload()))
+                       msgs = append(msgs, msg)
+               } else {
+                       fmt.Printf("Failed to receive message: %v", err)

Review Comment:
   ```suggestion
                        t.Logf("Failed to receive message: %v", err)
   ```



-- 
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]

Reply via email to