poorbarcode commented on code in PR #21406:
URL: https://github.com/apache/pulsar/pull/21406#discussion_r1724984248


##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/transaction/buffer/TopicTransactionBufferTest.java:
##########
@@ -405,4 +413,173 @@ private void assertGetLastMessageId(Consumer<?> consumer, 
MessageIdImpl expected
         assertEquals(expected.getLedgerId(), actual.getLedgerId());
     }
 
+    /**
+     * This test verifies the state changes of a TransactionBuffer within a 
topic under different conditions.
+     * Initially, the TransactionBuffer is in a NoSnapshot state upon topic 
creation.
+     * It remains in the NoSnapshot state even after a normal message is sent.
+     * The state changes to Ready only after a transactional message is sent.
+     * The test also ensures that the TransactionBuffer can be correctly 
recovered after the topic is unloaded.
+     */
+    @Test
+    public void testWriteSnapshotWhenFirstTxnMessageSend() throws Exception {
+        // 1. Prepare test environment.
+        String topic = "persistent://" + NAMESPACE1 + 
"/testWriteSnapshotWhenFirstTxnMessageSend";
+        String txnMsg = "transaction message";
+        String normalMsg = "normal message";
+        admin.topics().createNonPartitionedTopic(topic);
+        PersistentTopic persistentTopic = (PersistentTopic) 
pulsarServiceList.get(0).getBrokerService()
+                .getTopic(topic, false)
+                .get()
+                .get();
+        @Cleanup
+        Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
+                .topic(topic)
+                .create();
+        @Cleanup
+        Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING)
+                .topic(topic)
+                .subscriptionName("my-sub")
+                .subscribe();
+        // 2. Test the state of transaction buffer after building producer 
with no new messages.
+        // The TransactionBuffer should be in NoSnapshot state before 
transaction message sent.
+        TopicTransactionBuffer topicTransactionBuffer = 
(TopicTransactionBuffer) persistentTopic.getTransactionBuffer();
+        Awaitility.await().untilAsserted(() -> {
+            Assert.assertEquals(topicTransactionBuffer.getState(), 
TopicTransactionBufferState.State.NoSnapshot);
+        });
+        // 3. Test the state of transaction buffer after sending normal 
messages.
+        // The TransactionBuffer should still be in NoSnapshot state after a 
normal message is sent.
+        producer.newMessage().value(normalMsg).send();
+        Assert.assertEquals(topicTransactionBuffer.getState(), 
TopicTransactionBufferState.State.NoSnapshot);
+        // 4. Test the state of transaction buffer after sending transaction 
messages.
+        // The transaction buffer should be in Ready state at this time.
+        Transaction transaction = pulsarClient.newTransaction()
+                .withTransactionTimeout(5, TimeUnit.HOURS)
+                .build()
+                .get();
+        producer.newMessage(transaction).value(txnMsg).send();
+        Assert.assertEquals(topicTransactionBuffer.getState(), 
TopicTransactionBufferState.State.Ready);
+        // 5. Test transaction buffer can be recovered correctly.
+        // There are 4 message sent to this topic, 2 normal message and 2 
transaction message |m1|m2-txn1|m3-txn1|m4|.
+        // Aborting the transaction and unload the topic and then redelivering 
unacked messages,
+        // only normal messages can be received.
+        transaction.abort().get(5, TimeUnit.SECONDS);
+        producer.newMessage().value(normalMsg).send();
+        admin.topics().unload(topic);
+        PersistentTopic persistentTopic2 = (PersistentTopic) 
pulsarServiceList.get(0).getBrokerService()
+                .getTopic(topic, false)
+                .get()
+                .get();
+        TopicTransactionBuffer topicTransactionBuffer2 = 
(TopicTransactionBuffer) persistentTopic2
+                .getTransactionBuffer();
+        Awaitility.await().untilAsserted(() -> {
+            Assert.assertEquals(topicTransactionBuffer2.getState(), 
TopicTransactionBufferState.State.Ready);
+        });
+        consumer.redeliverUnacknowledgedMessages();
+        for (int i = 0; i < 2; i++) {
+            Message<String> message = consumer.receive(5, TimeUnit.SECONDS);
+            Assert.assertEquals(message.getValue(), normalMsg);
+        }
+        Message<String> message = consumer.receive(5, TimeUnit.SECONDS);
+        Assert.assertNull(message);
+    }
+
+    /**
+     * Send some messages before transaction buffer ready and then send some 
messages after transaction buffer ready,
+     * these messages should be received in order.
+     */
+    @Test
+    public void testMessagePublishInOrder() throws Exception {
+        // 1. Prepare test environment.
+        this.pulsarServiceList.forEach(pulsarService ->  {
+            pulsarService.setTransactionExecutorProvider(new 
TransactionBufferTestProvider());
+        });
+        String topic = "persistent://" + NAMESPACE1 + 
"/testMessagePublishInOrder" + RandomUtils.nextLong();
+        admin.topics().createNonPartitionedTopic(topic);
+        PersistentTopic persistentTopic = (PersistentTopic) 
pulsarServiceList.get(0).getBrokerService()
+                .getTopic(topic, false)
+                .get()
+                .get();
+        @Cleanup
+        Producer<Integer> producer = pulsarClient.newProducer(Schema.INT32)
+                .topic(topic)
+                .create();
+        @Cleanup
+        Consumer<Integer> consumer = pulsarClient.newConsumer(Schema.INT32)
+                .topic(topic)
+                .subscriptionName("sub")
+                .subscribe();
+        Transaction transaction = pulsarClient.newTransaction()
+                .withTransactionTimeout(5, TimeUnit.HOURS)
+                .build().get();
+
+        // 2. Set a new future in transaction buffer as 
`transactionBufferFuture` to simulate whether the
+        // transaction buffer recover completely.
+        TransactionBufferTestImpl topicTransactionBuffer = 
(TransactionBufferTestImpl) persistentTopic
+                .getTransactionBuffer();
+        CompletableFuture<Void> completableFuture = new CompletableFuture<>();
+        topicTransactionBuffer.setTransactionBufferFuture(completableFuture);

Review Comment:
   Even though you set a non-completed future here, the future 
`transactionBuffer.publishFuture` is done when TransactionBuffer is created.



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