@@ -441,6 +441,44 @@ TEST_P(ProducerTest, testFlushNoBatch) {
441441 client.close ();
442442}
443443
444+ // Verifies that getLastSequenceId() is correct after sendAsync + flush when batching is enabled.
445+ // Previously the batch used the last message's sequence_id, causing lastSequenceIdPublished_ to be
446+ // doubled (e.g. 3 messages yielded 4 instead of 2). The batch must use the first message's
447+ // sequence_id so that lastSequenceIdPublished_ = sequenceId + messagesCount - 1 is correct.
448+ TEST (ProducerTest, testGetLastSequenceIdAfterBatchFlush) {
449+ Client client (serviceUrl);
450+
451+ const std::string topicName =
452+ " persistent://public/default/testGetLastSequenceIdAfterBatchFlush-" + std::to_string (time (nullptr ));
453+
454+ ProducerConfiguration producerConfiguration;
455+ producerConfiguration.setBatchingEnabled (true );
456+ producerConfiguration.setBatchingMaxMessages (10 );
457+ producerConfiguration.setBatchingMaxPublishDelayMs (60000 );
458+
459+ Producer producer;
460+ ASSERT_EQ (ResultOk, client.createProducer (topicName, producerConfiguration, producer));
461+
462+ // Send 3 messages in a batch, then flush. Sequence ids are [0, 1, 2], so getLastSequenceId() must be 2.
463+ for (int i = 0 ; i < 3 ; i++) {
464+ Message msg = MessageBuilder ().setContent (" content" ).build ();
465+ producer.sendAsync (msg, nullptr );
466+ }
467+ ASSERT_EQ (ResultOk, producer.flush ());
468+ ASSERT_EQ (producer.getLastSequenceId (), 2 ) << " After 3 messages, last sequence id should be 2" ;
469+
470+ // Send 2 more (total 5), flush. Sequence ids for these are [3, 4], so getLastSequenceId() must be 4.
471+ for (int i = 0 ; i < 2 ; i++) {
472+ Message msg = MessageBuilder ().setContent (" content" ).build ();
473+ producer.sendAsync (msg, nullptr );
474+ }
475+ ASSERT_EQ (ResultOk, producer.flush ());
476+ ASSERT_EQ (producer.getLastSequenceId (), 4 ) << " After 5 messages total, last sequence id should be 4" ;
477+
478+ producer.close ();
479+ client.close ();
480+ }
481+
444482TEST_P (ProducerTest, testFlushBatch) {
445483 Client client (serviceUrl);
446484
0 commit comments