Best Testcontainers-java code snippet using org.testcontainers.containers.PulsarContainerTest.testTransactions
Source:PulsarContainerTest.java
...79 }80 }81 }82 @Test83 public void testTransactions() throws Exception {84 try (85 // constructorWithTransactions {86 PulsarContainer pulsar = new PulsarContainer(PULSAR_IMAGE).withTransactions();87 // }88 ) {89 pulsar.start();90 try (PulsarAdmin pulsarAdmin = PulsarAdmin.builder().serviceHttpUrl(pulsar.getHttpServiceUrl()).build()) {91 assertThat(92 pulsarAdmin93 .topics()94 .getList("pulsar/system")95 .contains("persistent://pulsar/system/transaction_coordinator_assign-partition-0")96 )97 .isTrue();98 }99 testTransactionFunctionality(pulsar.getPulsarBrokerUrl());100 }101 }102 @Test103 public void testTransactionsAndFunctionsWorker() throws Exception {104 try (PulsarContainer pulsar = new PulsarContainer(PULSAR_IMAGE).withTransactions().withFunctionsWorker()) {105 pulsar.start();106 try (PulsarAdmin pulsarAdmin = PulsarAdmin.builder().serviceHttpUrl(pulsar.getHttpServiceUrl()).build();) {107 assertThat(108 pulsarAdmin109 .topics()110 .getList("pulsar/system")111 .contains("persistent://pulsar/system/transaction_coordinator_assign-partition-0")112 )113 .isTrue();114 assertThat(pulsarAdmin.functions().getFunctions("public", "default")).hasSize(0);115 }116 testTransactionFunctionality(pulsar.getPulsarBrokerUrl());117 }...
testTransactions
Using AI Code Generation
1org.testcontainers.containers.PulsarContainerTest testTransactions = new org.testcontainers.containers.PulsarContainerTest();2testTransactions.testTransactions();3org.testcontainers.containers.PulsarContainerTest testTransactions = new org.testcontainers.containers.PulsarContainerTest();4testTransactions.testTransactions();5public static void testTransactions() throws Exception {6 try (PulsarContainer pulsarContainer = new PulsarContainer()) {7 pulsarContainer.start();8 try (PulsarClient client = PulsarClient.builder()9 .serviceUrl(pulsarContainer.getPlainTextServiceUrl())10 .build()) {11 final String subscription = "my-subscription";12 final Producer<byte[]> producer = client.newProducer()13 .topic(topic)14 .enableBatching(false)15 .create();16 final Consumer<byte[]> consumer = client.newConsumer()17 .topic(topic)18 .subscriptionName(subscription)19 .subscriptionType(SubscriptionType.Exclusive)20 .subscribe();21 final int numMessages = 100;22 final int numMessagesPerTxn = 10;23 final int numTxns = numMessages / numMessagesPerTxn;24 final List<CompletableFuture<Transaction>> beginTxnFutures = new ArrayList<>();25 for (int i = 0; i < numTxns; i++) {26 beginTxnFutures.add(client.newTransaction()27 .withTransactionTimeout(5, TimeUnit.SECONDS)28 .withTTL(30, TimeUnit.SECONDS)29 .buildAsync());30 }31 final List<Transaction> txns = beginTxnFutures.stream()32 .map(CompletableFuture::join)33 .collect(Collectors.toList());34 for (int i = 0; i < numMessages; i++) {35 final Transaction txn = txns.get(i / numMessagesPerTxn);36 producer.newMessage(txn)37 .value(("message-" + i).getBytes())38 .sendAsync();39 }40 for (Transaction txn : txns) {41 txn.commit().join();42 }43 for (int i = 0; i < numMessages; i++) {44 final Message<byte[]> message = consumer.receive();45 assertEquals("message-" + i, new String(message.getValue()));46 }47 }48 }49}
testTransactions
Using AI Code Generation
1@Tag("integration")2public class PulsarContainerTest extends AbstractContainerTest {3 private static final String TOPIC_NAME = "test-topic";4 private static final String SUBSCRIPTION_NAME = "test-sub";5 private static final String MESSAGE = "test-message";6 void testTransactions() throws Exception {7 PulsarContainer pulsarContainer = PulsarContainer.newContainer();8 pulsarContainer.start();9 PulsarClient client = PulsarClient.builder()10 .serviceUrl(pulsarContainer.getHttpServiceUrl())11 .build();12 try {13 TransactionCoordinatorClient transactionCoordinatorClient = TransactionCoordinatorClient.builder()14 .serviceUrl(pulsarContainer.getHttpServiceUrl())15 .build();16 String txnId = "test-txn";17 Transaction txn = transactionCoordinatorClient.newTransaction()18 .withTransactionTimeout(100, TimeUnit.MILLISECONDS)19 .withTransactionId(txnId)20 .build()21 .get();22 Producer<byte[]> producer = client.newProducer()23 .topic(TOPIC_NAME)24 .enableBatching(false)25 .create();26 Consumer<byte[]> consumer = client.newConsumer()27 .topic(TOPIC_NAME)28 .subscriptionName(SUBSCRIPTION_NAME)29 .subscribe();30 producer.newMessage(txn).value(MESSAGE.getBytes()).send();31 Message<byte[]> message = consumer.receive(1, TimeUnit.SECONDS);32 assertNull(message);33 transactionCoordinatorClient.abortTransaction(txnId).get();34 message = consumer.receive(1, TimeUnit.SECONDS);35 assertNotNull(message);36 assertEquals(MESSAGE, new String(message.getValue()));37 consumer.close();38 producer.close();39 } finally {40 client.close();41 pulsarContainer.close();42 }43 }44}
testTransactions
Using AI Code Generation
1PulsarContainer pulsarContainer = new PulsarContainer(DockerImageName.parse("apachepulsar/pulsar:2.7.3").asCompatibleSubstituteFor("apachepulsar/pulsar"));2pulsarContainer.start();3PulsarClient client = PulsarClient.builder().serviceUrl(pulsarContainer.getHttpServiceUrl()).build();4Producer<byte[]> producer = client.newProducer().topic("my-topic").create();5Consumer<byte[]> consumer = client.newConsumer().topic("my-topic").subscriptionName("my-subscription").subscribe();6producer.send("my-message".getBytes());7Message<byte[]> received = consumer.receive();8assertEquals("my-message", new String(received.getData()));9consumer.acknowledge(received);10pulsarContainer.close();
testTransactions
Using AI Code Generation
1import org.apache.pulsar.client.api.PulsarClient2import org.apache.pulsar.client.api.PulsarClientException3import org.apache.pulsar.client.api.PulsarClientException.TransactionConflictException4import org.apache.pulsar.client.api.PulsarClientException.TransactionNotSealedException5import org.apache.pulsar.client.api.PulsarClientException.TransactionNotFoundException6import org.apache.pulsar.client.api.Schema7import org.apache.pulsar.client.api.SubscriptionType8import org.apache.pulsar.client.api.SubscriptionInitialPosition9import org.apache.pulsar.client.api.transaction.Transaction10import org.apache.pulsar.client.api.transaction.TransactionCoordinatorClient11import org.apache.pulsar.client.api.transaction.TxnID12import org.apache.pulsar.client.impl.transaction.TransactionImpl13import org.apache.pulsar.common.naming.TopicName14import org.apache.pulsar.common.policies.data.TransactionCoordinatorStatus15import org.apache.pulsar.common.policies.data.TransactionInBufferStats16import org.apache.pulsar.common.policies.data.TransactionPendingAckStats17import org.apache.pulsar.common.policies.data.TransactionPendingAckStatsImpl18import org.apache.pulsar.common.policies.data.TransactionSubscriptionStats19import org.apache.pulsar.common.policies.data.TransactionSubscriptionStatsImpl20import org.apache.pulsar.common.policies.data.TransactionTopicStats21import org.apache.pulsar.common.policies.data.TransactionTopicStatsImpl22import org.apache.pulsar.common.policies.data.TransactionBufferStats23import org.apache.pulsar.common.policies.data.TransactionBufferStatsImpl24import org.apache.pulsar.common.policies.data.TransactionInPendingAckStats25import org.apache.pulsar.common.policies.data.TransactionInPendingAckStatsImpl26import org.apache.pulsar.common.policies.data.TransactionInTopicStats27import org.apache.pulsar.common.policies.data.TransactionInTopicStatsImpl28import org.apache.pulsar.common.policies.data.TransactionMetadataStats29import org.apache.pulsar.common.policies.data.TransactionMetadataStatsImpl30import org.apache.pulsar.common.policies.data.TransactionMetadataStatus31import org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap32import org.slf4j.Logger33import org.slf4j.LoggerFactory34import org.testcontainers.containers.PulsarContainer35import org.testcontainers.utility.D
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!