Best Testcontainers-java code snippet using org.testcontainers.containers.PulsarContainerTest.testTransactionFunctionality
Source:PulsarContainerTest.java
...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 }118 }119 @Test120 public void testClusterFullyInitialized() throws Exception {121 try (PulsarContainer pulsar = new PulsarContainer(PULSAR_IMAGE)) {122 pulsar.start();123 try (PulsarAdmin pulsarAdmin = PulsarAdmin.builder().serviceHttpUrl(pulsar.getHttpServiceUrl()).build()) {124 assertThat(pulsarAdmin.clusters().getClusters()).hasSize(1).contains("standalone");125 }126 }127 }128 @Test129 public void testStartupTimeoutIsHonored() {130 try (PulsarContainer pulsar = new PulsarContainer(PULSAR_IMAGE).withStartupTimeout(Duration.ZERO)) {131 assertThatThrownBy(pulsar::start)132 .hasRootCauseMessage("Precondition failed: timeout must be greater than zero");133 }134 }135 protected void testPulsarFunctionality(String pulsarBrokerUrl) throws Exception {136 try (137 PulsarClient client = PulsarClient.builder().serviceUrl(pulsarBrokerUrl).build();138 Consumer consumer = client.newConsumer().topic(TEST_TOPIC).subscriptionName("test-subs").subscribe();139 Producer<byte[]> producer = client.newProducer().topic(TEST_TOPIC).create()140 ) {141 producer.send("test containers".getBytes());142 CompletableFuture<Message> future = consumer.receiveAsync();143 Message message = future.get(5, TimeUnit.SECONDS);144 assertThat(new String(message.getData())).isEqualTo("test containers");145 }146 }147 protected void testTransactionFunctionality(String pulsarBrokerUrl) throws Exception {148 try (149 PulsarClient client = PulsarClient.builder().serviceUrl(pulsarBrokerUrl).enableTransaction(true).build();150 Consumer<String> consumer = client151 .newConsumer(Schema.STRING)152 .topic("transaction-topic")153 .subscriptionInitialPosition(SubscriptionInitialPosition.Earliest)154 .subscriptionName("test-transaction-sub")155 .subscribe();156 Producer<String> producer = client157 .newProducer(Schema.STRING)158 .sendTimeout(0, TimeUnit.SECONDS)159 .topic("transaction-topic")160 .create()161 ) {...
testTransactionFunctionality
Using AI Code Generation
1public void testTransactionFunctionality() throws Exception {2 PulsarContainer pulsarContainer = new PulsarContainer();3 pulsarContainer.start();4 String serviceUrl = pulsarContainer.getContainerIpAddress() + ":" + pulsarContainer.getMappedPort(6650);5 String adminUrl = pulsarContainer.getContainerIpAddress() + ":" + pulsarContainer.getMappedPort(8080);6 PulsarClient client = PulsarClient.builder()7 .serviceUrl(serviceUrl)8 .build();9 PulsarAdmin admin = PulsarAdmin.builder()10 .serviceHttpUrl(adminUrl)11 .build();12 admin.topics().createNonPartitionedTopic(topicName);13 Producer<byte[]> producer = client.newProducer()14 .topic(topicName)15 .enableBatching(false)16 .create();17 Transaction txn = client.newTransaction()18 .withTransactionTimeout(30, TimeUnit.SECONDS)19 .build()20 .get();21 producer.newMessage(txn).value("test".getBytes()).send();22 txn.commit().get();23 try (Consumer<byte[]> consumer = client.newConsumer()24 .topic(topicName)25 .subscriptionName("sub")26 .subscriptionType(SubscriptionType.Exclusive)27 .subscribe()) {28 Message<byte[]> message = consumer.receive();29 assertEquals("test", new String(message.getValue()));30 }31 client.close();32 admin.close();33 pulsarContainer.close();34}
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!!