Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tests] Fix flakiness issue when spying ServerCnx #13608

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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