use of software.amazon.awssdk.awstest.EchoTestRPC in project aws-iot-device-sdk-java-v2 by aws.
the class EchoTestServiceRunner method runLocalEchoTestServer.
/**
* Executes testClientLogic in a runnable.
* * If the connection encountered an error before the clientTestLogic completes this method will throw that error.
* * If the client completes normally first, this will complete normally.
* * If the client throws an exception first, this method will throw that exception
*
* Note: Use the returned CompletableFuture to check if any errors are occurring AFTER the testClientLogic runs. This
* may be needed/worth checking
*
* @param testClientLogic return
* @return A CompletableFuture of any connection level error that may have occurred after the testClientLogic completes
* @throws Exception throws an exception either from the test client logic having thrown, or the connection itself
* encountering an error before test client logic completes
*/
public static CompletableFuture<Void> runLocalEchoTestServer(final BiConsumer<EventStreamRPCConnection, EchoTestRPC> testClientLogic) throws Exception {
final int port = randomPort();
final String hostname = "127.0.0.1";
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
final EchoTestServiceRunner runner = new EchoTestServiceRunner(elGroup, hostname, port);
final HostResolver resolver = new HostResolver(elGroup, 64);
final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, resolver);
final SocketOptions socketOptions = new SocketOptions()) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.IPv4;
socketOptions.type = SocketOptions.SocketType.STREAM;
runner.runService();
final EventStreamRPCConnectionConfig config = new EventStreamRPCConnectionConfig(clientBootstrap, elGroup, socketOptions, null, hostname, port, () -> TestAuthNZHandlers.getClientAuth("accepted.foo"));
try (EventStreamRPCConnection connection = new EventStreamRPCConnection(config)) {
final CompletableFuture<Void> connectFuture = new CompletableFuture<>();
// only completes exceptionally if there's an error
final CompletableFuture<Void> clientErrorFuture = new CompletableFuture<>();
connection.connect(new EventStreamRPCConnection.LifecycleHandler() {
@Override
public void onConnect() {
connectFuture.complete(null);
}
@Override
public void onDisconnect(int errorCode) {
if (!connectFuture.isDone()) {
connectFuture.completeExceptionally(new RuntimeException("Client initial connection failed due to: " + CRT.awsErrorName(errorCode)));
} else if (errorCode != CRT.AWS_CRT_SUCCESS) {
clientErrorFuture.completeExceptionally(new RuntimeException("Client disconnected due to: " + CRT.awsErrorName(errorCode)));
} else // don't care if it normal closure/disconnect
{
}
}
@Override
public boolean onError(Throwable t) {
if (!connectFuture.isDone()) {
connectFuture.completeExceptionally(t);
} else {
clientErrorFuture.completeExceptionally(t);
}
return false;
}
});
// wait for connection to move forward
connectFuture.get(480, TimeUnit.SECONDS);
final EchoTestRPC client = new EchoTestRPCClient(connection);
final CompletableFuture<Object> runClientOrError = CompletableFuture.anyOf(clientErrorFuture, CompletableFuture.runAsync(() -> testClientLogic.accept(connection, client), Executors.newSingleThreadExecutor()));
runClientOrError.get(240, TimeUnit.SECONDS);
return clientErrorFuture;
}
}
}
use of software.amazon.awssdk.awstest.EchoTestRPC in project aws-iot-device-sdk-java-v2 by aws.
the class EchoTestServiceRunner method runLocalEchoTestServerClientLoopUnixDomain.
/**
* Enables a bit of a performance/load test by reconnecting the client to the same running server multiple times
* and each time the client connects, it runs the test logic for the number of times.
*
* !!! WARNING SocketOptions.SocketDomain is LOCAL for this server. Test client must match
*/
public static void runLocalEchoTestServerClientLoopUnixDomain(final String domainSocket, final BiConsumer<EventStreamRPCConnection, EchoTestRPC> testClientLogic, int nCount) throws Exception {
final int port = randomPort();
Files.deleteIfExists(Paths.get(domainSocket));
try (final EventLoopGroup elGroup = new EventLoopGroup(1);
final EchoTestServiceRunner runner = new EchoTestServiceRunner(elGroup, SocketOptions.SocketDomain.LOCAL, domainSocket, port);
final SocketOptions socketOptions = new SocketOptions();
final HostResolver hostResolver = new HostResolver(elGroup, 64);
final ClientBootstrap clientBootstrap = new ClientBootstrap(elGroup, hostResolver)) {
socketOptions.connectTimeoutMs = 3000;
socketOptions.domain = SocketOptions.SocketDomain.LOCAL;
socketOptions.type = SocketOptions.SocketType.STREAM;
runner.runService();
for (int i = 0; i < nCount; ++i) {
final EventStreamRPCConnectionConfig config = new EventStreamRPCConnectionConfig(clientBootstrap, elGroup, socketOptions, null, domainSocket, port, () -> TestAuthNZHandlers.getClientAuth("accepted.foo"));
try (EventStreamRPCConnection connection = new EventStreamRPCConnection(config)) {
final CompletableFuture<Void> connectFuture = new CompletableFuture<>();
// only completes exceptionally if there's an error
final CompletableFuture<Void> clientErrorFuture = new CompletableFuture<>();
connection.connect(new EventStreamRPCConnection.LifecycleHandler() {
@Override
public void onConnect() {
connectFuture.complete(null);
}
@Override
public void onDisconnect(int errorCode) {
if (!connectFuture.isDone()) {
connectFuture.completeExceptionally(new RuntimeException("Client initial connection failed due to: " + CRT.awsErrorName(errorCode)));
} else if (errorCode != CRT.AWS_CRT_SUCCESS) {
clientErrorFuture.completeExceptionally(new RuntimeException("Client disconnected due to: " + CRT.awsErrorName(errorCode)));
} else {
}
// don't care if it normal closure/disconnect
}
@Override
public boolean onError(Throwable t) {
if (!connectFuture.isDone()) {
connectFuture.completeExceptionally(t);
} else {
clientErrorFuture.completeExceptionally(t);
}
return false;
}
});
// wait for connection to move forward
connectFuture.get(30, TimeUnit.SECONDS);
final EchoTestRPC client = new EchoTestRPCClient(connection);
final CompletableFuture<Object> runClientOrError = CompletableFuture.anyOf(clientErrorFuture, CompletableFuture.runAsync(() -> testClientLogic.accept(connection, client), Executors.newSingleThreadExecutor()));
runClientOrError.get(240, TimeUnit.SECONDS);
}
}
}
// given this method is rather explicitly meant to be used in JUnit tests and intends to check per iteration
// calling waitForNoResources() is necessary to call here, and appropriate
CrtResource.waitForNoResources();
}
Aggregations