@@ -545,4 +545,51 @@ public void testMultiTopicConsumerConcurrentRedeliverAndReceive() throws Excepti
545
545
consumer .close ();
546
546
admin .topics ().deletePartitionedTopic ("persistent://public/default/" + topic );
547
547
}
548
+
549
+ @ DataProvider (name = "negativeAckPrecisionBitCnt" )
550
+ public Object [][] negativeAckPrecisionBitCnt () {
551
+ return new Object [][]{
552
+ {1 }, {2 }, {3 }, {4 }, {5 }, {6 }, {7 }, {8 }, {9 }, {10 }, {11 }, {12 }
553
+ };
554
+ }
555
+
556
+ /**
557
+ * When negativeAckPrecisionBitCnt is greater than 0, the lower bits of the redelivery time will be truncated
558
+ * to reduce the memory occupation. If set to k, the redelivery time will be bucketed by 2^k ms, resulting in
559
+ * the redelivery time could be earlier(no later) than the expected time no more than 2^k ms.
560
+ * @throws Exception if an error occurs
561
+ */
562
+ @ Test (dataProvider = "negativeAckPrecisionBitCnt" )
563
+ public void testConfigureNegativeAckPrecisionBitCnt (int negativeAckPrecisionBitCnt ) throws Exception {
564
+ String topic = BrokerTestUtil .newUniqueName ("testConfigureNegativeAckPrecisionBitCnt" );
565
+ long timeDeviation = 1L << negativeAckPrecisionBitCnt ;
566
+ long delayInMs = 2000 ;
567
+
568
+ @ Cleanup
569
+ Consumer <String > consumer = pulsarClient .newConsumer (Schema .STRING )
570
+ .topic (topic )
571
+ .subscriptionName ("sub1" )
572
+ .acknowledgmentGroupTime (0 , TimeUnit .SECONDS )
573
+ .subscriptionType (SubscriptionType .Shared )
574
+ .negativeAckRedeliveryDelay (delayInMs , TimeUnit .MILLISECONDS )
575
+ .negativeAckRedeliveryDelayPrecision (negativeAckPrecisionBitCnt )
576
+ .subscribe ();
577
+
578
+ @ Cleanup
579
+ Producer <String > producer = pulsarClient .newProducer (Schema .STRING )
580
+ .topic (topic )
581
+ .create ();
582
+ producer .sendAsync ("test-0" );
583
+ producer .flush ();
584
+
585
+ // receive the message and negative ack
586
+ consumer .negativeAcknowledge (consumer .receive ());
587
+ long expectedTime = System .currentTimeMillis () + delayInMs ;
588
+
589
+ // receive the redelivered message and calculate the time deviation
590
+ // assert that the redelivery time is no earlier than the `expected time - timeDeviation`
591
+ Message <String > msg1 = consumer .receive ();
592
+ assertTrue (System .currentTimeMillis () >= expectedTime - timeDeviation );
593
+ assertNotNull (msg1 );
594
+ }
548
595
}
0 commit comments