diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/intercept/BrokerInterceptorTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/intercept/BrokerInterceptorTest.java index a26783cda65c7..b2c3b8d711f90 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/intercept/BrokerInterceptorTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/intercept/BrokerInterceptorTest.java @@ -32,6 +32,7 @@ import org.apache.pulsar.client.api.PulsarClientException; import org.apache.pulsar.client.api.Schema; import org.apache.pulsar.common.nar.NarClassLoader; +import org.awaitility.Awaitility; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; @@ -114,7 +115,7 @@ public void testWebserviceRequest() throws PulsarAdminException { BrokerInterceptor listener = pulsar.getBrokerInterceptor(); Assert.assertTrue(listener instanceof CounterBrokerInterceptor); admin.namespaces().createNamespace("public/test", 4); - Assert.assertTrue(((CounterBrokerInterceptor)listener).getCount() >= 1); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) listener).getCount() >= 1); } @Test @@ -123,7 +124,7 @@ public void testPulsarCommand() throws PulsarClientException { Assert.assertTrue(listener instanceof CounterBrokerInterceptor); pulsarClient.newProducer(Schema.BOOL).topic("test").create(); // CONNECT and PRODUCER - Assert.assertTrue(((CounterBrokerInterceptor)listener).getCount() >= 2); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) listener).getCount() >= 2); } @Test @@ -133,7 +134,7 @@ public void testConnectionCreation() throws PulsarClientException { pulsarClient.newProducer(Schema.BOOL).topic("test").create(); pulsarClient.newConsumer(Schema.STRING).topic("test1").subscriptionName("test-sub").subscribe(); // single connection for both producer and consumer - assertEquals(((CounterBrokerInterceptor) listener).getConnectionCreationCount(), 1); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) listener).getConnectionCreationCount() == 1); } @Test @@ -142,7 +143,7 @@ public void testProducerCreation() throws PulsarClientException { Assert.assertTrue(listener instanceof CounterBrokerInterceptor); assertEquals(((CounterBrokerInterceptor) listener).getProducerCount(), 0); pulsarClient.newProducer(Schema.BOOL).topic("test").create(); - assertEquals(((CounterBrokerInterceptor) listener).getProducerCount(), 1); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) listener).getProducerCount() == 1); } @Test @@ -151,7 +152,7 @@ public void testConsumerCreation() throws PulsarClientException { Assert.assertTrue(listener instanceof CounterBrokerInterceptor); assertEquals(((CounterBrokerInterceptor) listener).getConsumerCount(), 0); pulsarClient.newConsumer(Schema.STRING).topic("test1").subscriptionName("test-sub").subscribe(); - assertEquals(((CounterBrokerInterceptor) listener).getConsumerCount(), 1); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) listener).getConsumerCount() == 1); } @Test @@ -178,8 +179,8 @@ public void testBeforeSendMessage() throws PulsarClientException { assertEquals(msg.getValue(), "hello world"); - assertEquals(((CounterBrokerInterceptor) listener).getBeforeSendCount(), 1); - assertEquals(((CounterBrokerInterceptor)listener).getMessageDispatchCount(),1); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) listener).getBeforeSendCount() == 1); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) listener).getMessageDispatchCount() == 1); } @Test @@ -195,8 +196,7 @@ public void testInterceptAck() throws Exception { Message message = consumer.receive(); consumer.acknowledge(message); } - - Assert.assertEquals(((CounterBrokerInterceptor) interceptor).getHandleAckCount(), 1); + Awaitility.await().until(() -> ((CounterBrokerInterceptor) interceptor).getHandleAckCount() == 1); } @Test @@ -225,6 +225,7 @@ public void onResponse(Call call, Response response) throws IOException { } }); future.get(); + Awaitility.await().until(() -> !interceptor.getResponseList().isEmpty()); CounterBrokerInterceptor.ResponseEvent responseEvent = interceptor.getResponseList().get(0); Assert.assertEquals(responseEvent.getRequestUri(), "/admin/v3/test/asyncGet/my-topic/1000"); Assert.assertEquals(responseEvent.getResponseStatus(),