use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project pravega by pravega.
the class ControllerImplTest method testKeepAliveNoServer.
@Test
public void testKeepAliveNoServer() throws Exception {
// Verify that keep-alive timeout less than permissible by the server results in a failure.
NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", serverPort).keepAliveTime(5, TimeUnit.SECONDS);
if (testSecure) {
builder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File(SecurityConfigDefaults.TLS_CA_CERT_PATH)).build());
} else {
builder = builder.usePlaintext();
}
@Cleanup final ControllerImpl controller = new ControllerImpl(builder, ControllerImplConfig.builder().clientConfig(ClientConfig.builder().trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build()).retryAttempts(1).initialBackoffMillis(1).timeoutMillis(500).build(), this.executor);
CompletableFuture<Boolean> createStreamStatus = controller.createStream("scope1", "streamdelayed", StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
AssertExtensions.assertFutureThrows("Should throw RetriesExhaustedException", createStreamStatus, throwable -> throwable instanceof RetriesExhaustedException);
}
use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project thingsboard by thingsboard.
the class EdgeGrpcClient method connect.
@Override
public void connect(String edgeKey, String edgeSecret, Consumer<UplinkResponseMsg> onUplinkResponse, Consumer<EdgeConfiguration> onEdgeUpdate, Consumer<DownlinkMsg> onDownlink, Consumer<Exception> onError) {
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(rpcHost, rpcPort).keepAliveTime(keepAliveTimeSec, TimeUnit.SECONDS);
if (sslEnabled) {
try {
SslContextBuilder sslContextBuilder = GrpcSslContexts.forClient();
if (StringUtils.isNotEmpty(certResource)) {
sslContextBuilder.trustManager(ResourceUtils.getInputStream(this, certResource));
}
builder.sslContext(sslContextBuilder.build());
} catch (SSLException e) {
log.error("Failed to initialize channel!", e);
throw new RuntimeException(e);
}
} else {
builder.usePlaintext();
}
channel = builder.build();
EdgeRpcServiceGrpc.EdgeRpcServiceStub stub = EdgeRpcServiceGrpc.newStub(channel);
log.info("[{}] Sending a connect request to the TB!", edgeKey);
this.inputStream = stub.withCompression("gzip").handleMsgs(initOutputStream(edgeKey, onUplinkResponse, onEdgeUpdate, onDownlink, onError));
this.inputStream.onNext(RequestMsg.newBuilder().setMsgType(RequestMsgType.CONNECT_RPC_MESSAGE).setConnectRequestMsg(ConnectRequestMsg.newBuilder().setEdgeRoutingKey(edgeKey).setEdgeSecret(edgeSecret).setEdgeVersion(EdgeVersion.V_3_3_3).build()).build());
}
use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project pravega by pravega.
the class ControllerImplTest method testKeepAliveWithServer.
@Test
public void testKeepAliveWithServer() throws Exception {
// Verify that the same RPC with permissible keepalive time succeeds.
int serverPort2 = TestUtils.getAvailableListenPort();
NettyServerBuilder testServerBuilder = NettyServerBuilder.forPort(serverPort2).addService(testServerImpl).permitKeepAliveTime(KEEP_ALIVE_TEST_PERMIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
if (testSecure) {
testServerBuilder = testServerBuilder.useTransportSecurity(new File(SecurityConfigDefaults.TLS_SERVER_CERT_PATH), new File(SecurityConfigDefaults.TLS_SERVER_PRIVATE_KEY_PATH));
}
@Cleanup("shutdownNow") Server testServer = testServerBuilder.build().start();
NettyChannelBuilder builder = NettyChannelBuilder.forAddress("localhost", serverPort2).keepAliveTime(KEEP_ALIVE_TEST_KEEP_ALIVE_MILLIS, TimeUnit.MILLISECONDS);
if (testSecure) {
builder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File(SecurityConfigDefaults.TLS_CA_CERT_PATH)).build());
} else {
builder = builder.usePlaintext();
}
@Cleanup final ControllerImpl controller1 = new ControllerImpl(builder, ControllerImplConfig.builder().clientConfig(ClientConfig.builder().trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build()).retryAttempts(1).initialBackoffMillis(1).build(), this.executor);
val createStreamStatus = controller1.createStream("scope1", "streamdelayed", StreamConfiguration.builder().scalingPolicy(ScalingPolicy.fixed(1)).build());
assertTrue(createStreamStatus.get());
testServer.shutdownNow();
}
use of io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder in project pravega by pravega.
the class ControllerImplTest method testCredPluginException.
@Test
public void testCredPluginException() throws Exception {
NettyChannelBuilder builder = spy(NettyChannelBuilder.forAddress("localhost", serverPort).keepAliveTime(10, TimeUnit.SECONDS));
final NettyChannelBuilder channelBuilder;
if (testSecure) {
channelBuilder = builder.sslContext(GrpcSslContexts.forClient().trustManager(new File(SecurityConfigDefaults.TLS_CA_CERT_PATH)).build());
} else {
channelBuilder = builder.usePlaintext();
}
// Setup mocks.
ClientConfig cfg = spy(ClientConfig.builder().credentials(new DefaultCredentials("pass", "user")).trustStore(SecurityConfigDefaults.TLS_CA_CERT_PATH).controllerURI(URI.create((testSecure ? "tls://" : "tcp://") + "localhost:" + serverPort)).build());
doThrow(new IllegalStateException("Exception thrown by cred plugin")).when(cfg).getCredentials();
ManagedChannel channel = mock(ManagedChannel.class);
doReturn(channel).when(builder).build();
ControllerImplConfig controllerCfg = new ControllerImplConfig(1, 1, 1, 1, 1000, cfg);
// Verify exception scenario.
assertThrows(IllegalStateException.class, () -> new ControllerImpl(channelBuilder, controllerCfg, this.executor));
verify(channel, times(1)).shutdownNow();
verify(channel, times(1)).awaitTermination(anyLong(), any(TimeUnit.class));
}
Aggregations