chia7712 commented on code in PR #21251:
URL: https://github.com/apache/kafka/pull/21251#discussion_r2679506147
##########
core/src/test/scala/unit/kafka/coordinator/transaction/TransactionStateManagerTest.scala:
##########
@@ -1365,4 +1365,59 @@ class TransactionStateManagerTest {
assertEquals(transactionVersion,
transactionManager.transactionVersionLevel())
}
+
+ @Test
+ def testLoadTransactionMetadata_InvalidKeyVersion(): Unit = {
+ // Test that records with invalid key versions throw IllegalStateException
and are skipped
+ val loadedTransactions = new ConcurrentHashMap[String,
TransactionMetadata]()
+ var currOffset = 0L
+
+ // Create an invalid key with unknown version
+ val invalidKeyBytes = ByteBuffer.allocate(10)
+ invalidKeyBytes.putShort(999.toShort) // Unknown version number
+ invalidKeyBytes.putInt(12345)
+ invalidKeyBytes.flip()
+
+ val validTxnId = "valid-txn"
+ val timestamp = time.milliseconds()
+ val validMetadata = new TransactionMetadata(validTxnId, 400L,
RecordBatch.NO_PRODUCER_ID,
+ RecordBatch.NO_PRODUCER_ID, 0.toShort, RecordBatch.NO_PRODUCER_EPOCH,
+ transactionTimeoutMs, TransactionState.EMPTY, util.Set.of(), timestamp,
timestamp, TV_2)
+
+ // Mix invalid and valid records
+ val mixedRecords = MemoryRecords.withRecords(0L, Compression.NONE,
+ new SimpleRecord(invalidKeyBytes.array(), "dummy-value".getBytes()), //
Invalid key
+ new SimpleRecord(TransactionLog.keyToBytes(validTxnId),
TransactionLog.valueToBytes(validMetadata.prepareNoTransit(), TV_2)) // Valid
record
+ )
+
+ // Execute the code under test
+ var invalidKeySkipped = false
+ mixedRecords.batches.forEach { batch =>
+ for (record <- batch.asScala) {
+ require(record.hasKey, "Transaction state log's key should not be
null")
Review Comment:
Why we don't test the production code?
--
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]