Skip to content

Commit f3676ac

Browse files
xinlian12Annie Liang
andauthored
FixOfConnectionStateListenerTest (Azure#17481)
* fix flakyness of connectionStateListener test Co-authored-by: Annie Liang <[email protected]>
1 parent 9d29bbb commit f3676ac

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/implementation/directconnectivity/ConnectionStateListenerTest.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@
2424
import org.testng.annotations.Test;
2525

2626
import java.util.HashMap;
27+
import java.util.Random;
2728
import java.util.UUID;
2829
import java.util.concurrent.ExecutionException;
2930

3031
import static com.azure.cosmos.implementation.TestUtils.mockDiagnosticsClientContext;
3132

3233
public class ConnectionStateListenerTest {
34+
private static final Logger logger = LoggerFactory.getLogger(ConnectionStateListenerTest.class);
3335

3436
private static int port = 8082;
35-
private static String serverUriString = "rntbd://localhost:" + port;
36-
private static final Logger logger = LoggerFactory.getLogger(ConnectionStateListenerTest.class);
37+
private static String serverAddressPrefix = "rntbd://localhost:";
38+
private static Random random = new Random();
3739

3840
@DataProvider(name = "connectionStateListenerConfigProvider")
3941
public Object[][] connectionStateListenerConfigProvider() {
@@ -52,7 +54,9 @@ public void connectionStateListener_OnConnectionEvent(
5254
RequestResponseType responseType,
5355
int times) throws ExecutionException, InterruptedException {
5456

55-
TcpServer server = TcpServerFactory.startNewRntbdServer(port);
57+
// using a random generated server port
58+
int serverPort = port + random.nextInt(1000);
59+
TcpServer server = TcpServerFactory.startNewRntbdServer(serverPort);
5660
// Inject fake response
5761
server.injectServerResponse(responseType);
5862

@@ -78,17 +82,17 @@ public void connectionStateListener_OnConnectionEvent(
7882
getDocumentDefinition(), new HashMap<>());
7983
req.setPartitionKeyRangeIdentity(new PartitionKeyRangeIdentity("fakeCollectionId","fakePartitionKeyRangeId"));
8084

81-
Uri targetUri = new Uri(serverUriString);
85+
Uri targetUri = new Uri(serverAddressPrefix + serverPort);
8286
try {
8387
client.invokeStoreAsync(targetUri, req).block();
8488
} catch (Exception e) {
8589
logger.info("expected failed request with reason {}", e);
8690
}
8791
finally {
88-
Mockito.verify(addressResolver, Mockito.times(times)).updateAddresses(Mockito.any(), Mockito.any());
92+
TcpServerFactory.shutdownRntbdServer(server);
8993
}
9094

91-
TcpServerFactory.shutdownRntbdServer(server);
95+
Mockito.verify(addressResolver, Mockito.times(times)).updateAddresses(Mockito.any(), Mockito.any());
9296
}
9397

9498
private Document getDocumentDefinition() {

sdk/cosmos/azure-cosmos/src/test/java/com/azure/cosmos/implementation/directconnectivity/TcpServerMock/TcpServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ public void initChannel(SocketChannel channel) throws Exception {
121121
logger.info("Server channel closed.");
122122
});
123123

124+
} catch (Exception e) {
125+
promise.setFailure(e);
124126
} finally {
125127
parent.shutdownGracefully().sync();
126128
child.shutdownGracefully().sync();

0 commit comments

Comments
 (0)