This is an automated email from the ASF dual-hosted git repository. penghui pushed a commit to branch branch-2.9 in repository https://gitbox.apache.org/repos/asf/pulsar.git
commit 624bf12934e8e0fccb411321c693b2f601a853b7 Author: Lari Hotari <[email protected]> AuthorDate: Tue Jan 4 19:32:09 2022 +0200 [Tests] Fix flakiness issue when spying ServerCnx (#13608) Fixes #13570 (cherry picked from commit de99c2c9c3c54b31096e6d734de8132e96d50c79) --- .../service/PersistentDispatcherFailoverConsumerTest.java | 10 ++++++++-- .../broker/service/PersistentTopicConcurrentTest.java | 6 +++++- .../apache/pulsar/broker/service/PersistentTopicTest.java | 14 +++++++++++--- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentDispatcherFailoverConsumerTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentDispatcherFailoverConsumerTest.java index bc5e05c..84cf739 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentDispatcherFailoverConsumerTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentDispatcherFailoverConsumerTest.java @@ -21,6 +21,7 @@ package org.apache.pulsar.broker.service; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.matches; import static org.mockito.ArgumentMatchers.same; +import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -28,6 +29,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNull; import static org.testng.AssertJUnit.assertEquals; @@ -165,7 +167,9 @@ public class PersistentDispatcherFailoverConsumerTest { return null; }).when(channelCtx).writeAndFlush(any(), any()); - serverCnx = spy(new ServerCnx(pulsar)); + serverCnx = mock(ServerCnx.class, withSettings() + .useConstructor(pulsar) + .defaultAnswer(CALLS_REAL_METHODS)); doReturn(true).when(serverCnx).isActive(); doReturn(true).when(serverCnx).isWritable(); doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnx).clientAddress(); @@ -174,7 +178,9 @@ public class PersistentDispatcherFailoverConsumerTest { doReturn(new PulsarCommandSenderImpl(null, serverCnx)) .when(serverCnx).getCommandSender(); - serverCnxWithOldVersion = spy(new ServerCnx(pulsar)); + serverCnxWithOldVersion = mock(ServerCnx.class, withSettings() + .useConstructor(pulsar) + .defaultAnswer(CALLS_REAL_METHODS)); doReturn(true).when(serverCnxWithOldVersion).isActive(); doReturn(true).when(serverCnxWithOldVersion).isWritable(); doReturn(new InetSocketAddress("localhost", 1234)) diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicConcurrentTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicConcurrentTest.java index ef81ed2..78c7e25 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicConcurrentTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicConcurrentTest.java @@ -18,10 +18,12 @@ */ package org.apache.pulsar.broker.service; +import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.withSettings; import static org.testng.Assert.assertFalse; import java.lang.reflect.Method; @@ -101,7 +103,9 @@ public class PersistentTopicConcurrentTest extends MockedBookKeeperTestCase { brokerService = spy(new BrokerService(pulsar, eventLoopGroup)); doReturn(brokerService).when(pulsar).getBrokerService(); - serverCnx = spy(new ServerCnx(pulsar)); + serverCnx = mock(ServerCnx.class, withSettings() + .useConstructor(pulsar) + .defaultAnswer(CALLS_REAL_METHODS)); doReturn(true).when(serverCnx).isActive(); NamespaceService nsSvc = mock(NamespaceService.class); diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java index f1a11ad..d94320f 100644 --- a/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java +++ b/pulsar-broker/src/test/java/org/apache/pulsar/broker/service/PersistentTopicTest.java @@ -22,6 +22,7 @@ import static org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMo import static org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest.createMockZooKeeper; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.doAnswer; @@ -34,6 +35,7 @@ import static org.mockito.Mockito.spy; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNull; @@ -207,7 +209,9 @@ public class PersistentTopicTest extends MockedBookKeeperTestCase { brokerService = spy(new BrokerService(pulsar, eventLoopGroup)); doReturn(brokerService).when(pulsar).getBrokerService(); - serverCnx = spy(new ServerCnx(pulsar)); + serverCnx = mock(ServerCnx.class, withSettings() + .useConstructor(pulsar) + .defaultAnswer(CALLS_REAL_METHODS)); doReturn(true).when(serverCnx).isActive(); doReturn(true).when(serverCnx).isWritable(); doReturn(new InetSocketAddress("localhost", 1234)).when(serverCnx).clientAddress(); @@ -584,7 +588,9 @@ public class PersistentTopicTest extends MockedBookKeeperTestCase { final String producerNameBase = "producer"; final String role = "appid1"; - ServerCnx cnx = spy(new ServerCnx(pulsar)); + ServerCnx cnx = mock(ServerCnx.class, withSettings() + .useConstructor(pulsar) + .defaultAnswer(CALLS_REAL_METHODS)); doReturn(true).when(cnx).isActive(); doReturn(true).when(cnx).isWritable(); doReturn(new InetSocketAddress(address, 1234)).when(cnx).clientAddress(); @@ -1022,7 +1028,9 @@ public class PersistentTopicTest extends MockedBookKeeperTestCase { final String consumerNameBase = "consumer"; final String role = "appid1"; - ServerCnx cnx = spy(new ServerCnx(pulsar)); + ServerCnx cnx = mock(ServerCnx.class, withSettings() + .useConstructor(pulsar) + .defaultAnswer(CALLS_REAL_METHODS)); doReturn(true).when(cnx).isActive(); doReturn(true).when(cnx).isWritable(); doReturn(new InetSocketAddress(address, 1234)).when(cnx).clientAddress();
