diff --git a/extensions/narayana-jta/runtime/src/test/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorderTest.java b/extensions/narayana-jta/runtime/src/test/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorderTest.java new file mode 100644 index 00000000000000..694e810854ca7d --- /dev/null +++ b/extensions/narayana-jta/runtime/src/test/java/io/quarkus/narayana/jta/runtime/NarayanaJtaRecorderTest.java @@ -0,0 +1,62 @@ +package io.quarkus.narayana.jta.runtime; + +import static org.junit.jupiter.api.Assertions.fail; + +import java.nio.charset.StandardCharsets; +import java.util.SortedSet; +import java.util.TreeSet; +import java.util.UUID; + +import org.junit.jupiter.api.Test; + +public class NarayanaJtaRecorderTest { + + @Test + void testByteLength() { + + TransactionManagerConfiguration transactions = new TransactionManagerConfiguration(); + transactions.shortenNodeNameIfNecessary = true; + // create nodeNames larger than 28 bytes + for (int i = 1; i < 100; i++) { + String originalNodeName = UUID.randomUUID().toString(); + NarayanaJtaRecorder r = new NarayanaJtaRecorder(); + transactions.nodeName = originalNodeName; + r.setNodeName(transactions); + int numberOfBytes = transactions.nodeName.getBytes(StandardCharsets.UTF_8).length; + if (numberOfBytes > 28) { + fail("node name bytes still exceeded 28 bytes limit, number of bytes is " + numberOfBytes); + } + } + // for (int i = 1; i < 20000; i++) { + for (int i = 1; i < 2000; i++) { + byte[] data = new byte[i]; + NarayanaJtaRecorder r = new NarayanaJtaRecorder(); + transactions.nodeName = new String(data); + r.setNodeName(transactions); + int numberOfBytes = transactions.nodeName.getBytes(StandardCharsets.UTF_8).length; + if (numberOfBytes > 28) { + fail("node name bytes still exceeded 28 bytes limit, number of bytes is " + numberOfBytes); + } + } + } + + //commented because it is a stress test + // @Test + void testCollision() { + SortedSet set = new TreeSet<>(); + TransactionManagerConfiguration transactions = new TransactionManagerConfiguration(); + transactions.shortenNodeNameIfNecessary = true; + // create a nodeName larger than 28 bytes + for (int i = 1; i < 1000000; i++) { + String originalNodeName = UUID.randomUUID().toString(); + NarayanaJtaRecorder r = new NarayanaJtaRecorder(); + transactions.nodeName = originalNodeName; + r.setNodeName(transactions); + if (set.contains(transactions.nodeName)) { + fail("Collision of IDs for " + transactions.nodeName); + } + set.add(transactions.nodeName); + } + } + +}