Skip to content

Commit

Permalink
[Tests] Fix flakiness issue when spying ServerCnx (apache#13608)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari authored and wuzhanpeng committed Jan 5, 2022
1 parent ae4bd52 commit 46e1b8d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
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;
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;
Expand Down Expand Up @@ -157,7 +159,9 @@ public void setup() throws Exception {
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();
Expand All @@ -166,7 +170,9 @@ public void setup() throws Exception {
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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -99,7 +101,9 @@ public void setup(Method m) throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -34,6 +35,7 @@
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;
Expand Down Expand Up @@ -206,7 +208,9 @@ public void setup() throws Exception {
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();
Expand Down Expand Up @@ -587,7 +591,9 @@ private Producer getMockedProducerWithSpecificAddress(Topic topic, long producer
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();
Expand Down Expand Up @@ -1042,7 +1048,9 @@ private Consumer getMockedConsumerWithSpecificAddress(Topic topic, Subscription
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();
Expand Down

0 comments on commit 46e1b8d

Please sign in to comment.