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

FixOfConnectionStateListenerTest #17481

Merged
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 @@ -24,16 +24,18 @@
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ExecutionException;

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

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

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

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

TcpServer server = TcpServerFactory.startNewRntbdServer(port);
// using a random generated server port
int serverPort = port + random.nextInt(1000);
TcpServer server = TcpServerFactory.startNewRntbdServer(serverPort);
// Inject fake response
server.injectServerResponse(responseType);

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

Uri targetUri = new Uri(serverUriString);
Uri targetUri = new Uri(serverAddressPrefix + serverPort);
try {
client.invokeStoreAsync(targetUri, req).block();
} catch (Exception e) {
logger.info("expected failed request with reason {}", e);
}
finally {
Mockito.verify(addressResolver, Mockito.times(times)).updateAddresses(Mockito.any(), Mockito.any());
TcpServerFactory.shutdownRntbdServer(server);
}

TcpServerFactory.shutdownRntbdServer(server);
Mockito.verify(addressResolver, Mockito.times(times)).updateAddresses(Mockito.any(), Mockito.any());
}

private Document getDocumentDefinition() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public void initChannel(SocketChannel channel) throws Exception {
logger.info("Server channel closed.");
});

} catch (Exception e) {
promise.setFailure(e);
} finally {
parent.shutdownGracefully().sync();
child.shutdownGracefully().sync();
Expand Down