eolivelli commented on a change in pull request #13383:
URL: https://github.com/apache/pulsar/pull/13383#discussion_r791437784



##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BatchMessageWithBatchIndexLevelTest.java
##########
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+import com.google.common.collect.Lists;
+import lombok.SneakyThrows;
+import 
org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers;
+import org.apache.pulsar.broker.service.persistent.PersistentTopic;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.client.impl.ConsumerImpl;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import static org.testng.Assert.assertEquals;
+
+@Test(groups = "broker")
+public class BatchMessageWithBatchIndexLevelTest extends BatchMessageTest {
+
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        conf.setAcknowledgmentAtBatchIndexLevelEnabled(true);
+        super.baseSetup();
+    }
+
+    @Test
+    @SneakyThrows
+    public void testBatchMessageAck() {
+        int numMsgs = 40;
+        final String topicName = "persistent://prop/ns-abc/batchMessageAck-" + 
UUID.randomUUID();
+        final String subscriptionName = "sub-batch-1";
+
+        ConsumerImpl<byte[]> consumer = (ConsumerImpl<byte[]>) pulsarClient
+                .newConsumer()
+                .topic(topicName)
+                .subscriptionName(subscriptionName)
+                .receiverQueueSize(10)
+                .subscriptionType(SubscriptionType.Shared)
+                .enableBatchIndexAcknowledgment(true)
+                .negativeAckRedeliveryDelay(100, TimeUnit.MILLISECONDS)
+                .subscribe();
+
+        Producer<byte[]> producer = pulsarClient
+                .newProducer()
+                .topic(topicName)
+                .batchingMaxMessages(20)
+                .batchingMaxPublishDelay(200, TimeUnit.MILLISECONDS)
+                .enableBatching(true)
+                .create();
+
+        List<CompletableFuture<MessageId>> sendFutureList = 
Lists.newArrayList();
+        for (int i = 0; i < numMsgs; i++) {
+            byte[] message = ("batch-message-" + i).getBytes();
+            
sendFutureList.add(producer.newMessage().value(message).sendAsync());
+        }
+        FutureUtil.waitForAll(sendFutureList).get();
+        PersistentTopic topic = (PersistentTopic) 
pulsar.getBrokerService().getTopicReference(topicName).get();
+        PersistentDispatcherMultipleConsumers dispatcher = 
(PersistentDispatcherMultipleConsumers) topic
+                .getSubscription(subscriptionName).getDispatcher();
+        Message<byte[]> receive1 = consumer.receive();
+        Message<byte[]> receive2 = consumer.receive();
+        consumer.acknowledge(receive1);
+        consumer.acknowledge(receive2);
+        Awaitility.await().untilAsserted(() -> {
+            
assertEquals(dispatcher.getConsumers().get(0).getUnackedMessages(), 18);
+        });
+        Message<byte[]> receive3 = consumer.receive();
+        Message<byte[]> receive4 = consumer.receive();
+        consumer.acknowledge(receive3);
+        consumer.acknowledge(receive4);
+        Awaitility.await().untilAsserted(() -> {
+            
assertEquals(dispatcher.getConsumers().get(0).getUnackedMessages(), 16);
+        });
+        Message<byte[]> receive5 = consumer.receive();
+        consumer.negativeAcknowledge(receive5);
+        Awaitility.await().untilAsserted(() -> {
+            
assertEquals(dispatcher.getConsumers().get(0).getUnackedMessages(), 16);

Review comment:
       but you are not verifying that the number is something different from 16 
before this assertion.
   
   I see two cases:
   1) you expect that the value is 16 at line 96, then it changes to another 
value, then is it again back to 16
   2) you are expecting that the value is never changing
   
   if it is 1) then you should add some assertion between this line and like 96 
to check that the value is not 16 anymore
   if it is case 2) then you don't need `Awaitility.await().untilAsserted` but 
you can simply leave
   `assertEquals(dispatcher.getConsumers().get(0).getUnackedMessages(), 16);`

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/service/Consumer.java
##########
@@ -811,8 +916,15 @@ public Subscription getSubscription() {
     }
 
     private int addAndGetUnAckedMsgs(Consumer consumer, int ackedMessages) {
-        subscription.addUnAckedMessages(ackedMessages);
-        return UNACKED_MESSAGES_UPDATER.addAndGet(consumer, ackedMessages);
+        int unackedMsgs = 0;
+        if (Subscription.isIndividualAckMode(subType)) {
+            subscription.addUnAckedMessages(ackedMessages);
+            unackedMsgs = UNACKED_MESSAGES_UPDATER.addAndGet(consumer, 
ackedMessages);
+        }
+        if (unackedMsgs < 0) {
+            log.error("unackedMsgs is : {}, ackedMessages : {}, consumer : 
{}", unackedMsgs, ackedMessages, consumer);

Review comment:
       makes sense

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BatchMessageWithBatchIndexLevelTest.java
##########
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.broker.service;
+
+import com.google.common.collect.Lists;
+import lombok.SneakyThrows;
+import 
org.apache.pulsar.broker.service.persistent.PersistentDispatcherMultipleConsumers;
+import org.apache.pulsar.broker.service.persistent.PersistentTopic;
+import org.apache.pulsar.client.api.Message;
+import org.apache.pulsar.client.api.MessageId;
+import org.apache.pulsar.client.api.Producer;
+import org.apache.pulsar.client.api.SubscriptionType;
+import org.apache.pulsar.client.impl.ConsumerImpl;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.awaitility.Awaitility;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import java.util.List;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.TimeUnit;
+import static org.testng.Assert.assertEquals;
+
+@Test(groups = "broker")
+public class BatchMessageWithBatchIndexLevelTest extends BatchMessageTest {
+
+    @BeforeClass
+    @Override
+    protected void setup() throws Exception {
+        conf.setAcknowledgmentAtBatchIndexLevelEnabled(true);
+        super.baseSetup();
+    }
+
+    @Test
+    @SneakyThrows
+    public void testBatchMessageAck() {
+        int numMsgs = 40;
+        final String topicName = "persistent://prop/ns-abc/batchMessageAck-" + 
UUID.randomUUID();
+        final String subscriptionName = "sub-batch-1";
+
+        ConsumerImpl<byte[]> consumer = (ConsumerImpl<byte[]>) pulsarClient
+                .newConsumer()
+                .topic(topicName)
+                .subscriptionName(subscriptionName)
+                .receiverQueueSize(10)
+                .subscriptionType(SubscriptionType.Shared)
+                .enableBatchIndexAcknowledgment(true)
+                .negativeAckRedeliveryDelay(100, TimeUnit.MILLISECONDS)
+                .subscribe();
+
+        Producer<byte[]> producer = pulsarClient
+                .newProducer()
+                .topic(topicName)
+                .batchingMaxMessages(20)
+                .batchingMaxPublishDelay(200, TimeUnit.MILLISECONDS)
+                .enableBatching(true)
+                .create();
+
+        List<CompletableFuture<MessageId>> sendFutureList = 
Lists.newArrayList();
+        for (int i = 0; i < numMsgs; i++) {
+            byte[] message = ("batch-message-" + i).getBytes();
+            
sendFutureList.add(producer.newMessage().value(message).sendAsync());
+        }
+        FutureUtil.waitForAll(sendFutureList).get();
+        PersistentTopic topic = (PersistentTopic) 
pulsar.getBrokerService().getTopicReference(topicName).get();
+        PersistentDispatcherMultipleConsumers dispatcher = 
(PersistentDispatcherMultipleConsumers) topic
+                .getSubscription(subscriptionName).getDispatcher();
+        Message<byte[]> receive1 = consumer.receive();
+        Message<byte[]> receive2 = consumer.receive();
+        consumer.acknowledge(receive1);
+        consumer.acknowledge(receive2);
+        Awaitility.await().untilAsserted(() -> {
+            
assertEquals(dispatcher.getConsumers().get(0).getUnackedMessages(), 18);
+        });
+        Message<byte[]> receive3 = consumer.receive();
+        Message<byte[]> receive4 = consumer.receive();
+        consumer.acknowledge(receive3);
+        consumer.acknowledge(receive4);
+        Awaitility.await().untilAsserted(() -> {
+            
assertEquals(dispatcher.getConsumers().get(0).getUnackedMessages(), 16);
+        });
+        Message<byte[]> receive5 = consumer.receive();
+        consumer.negativeAcknowledge(receive5);
+        Awaitility.await().untilAsserted(() -> {
+            
assertEquals(dispatcher.getConsumers().get(0).getUnackedMessages(), 16);

Review comment:
       > As negativeAcknowledge will be executed async, Then, I have to make 
the unack-msg count is also 16. So it should add Awaitility.await()
   
   I agree, but how can you verify that the value simply **never changed** from 
16 ?
   because even if the code you changed did not run at all, then the value was 
16 and it will still be 16, so your test is not adding any additional 
verification.
   
   In other words: this test is not testing that you added the code, it may 
"work" even if your code is not executed because the message did not reach to 
the broker, because it is async.
   we should add something that asserts that we are executing that code in 
Consumer#line 875~880




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