List of usage examples for io.netty.channel ChannelFuture sync
@Override
ChannelFuture sync() throws InterruptedException;
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void waitUntilActiveHandler_channelActive() throws Exception { final CountDownLatch latch = new CountDownLatch(1); WaitUntilActiveHandler handler = new WaitUntilActiveHandler(new ChannelHandlerAdapter() { @Override/*from www . j a v a2 s .c om*/ public void handlerAdded(ChannelHandlerContext ctx) throws Exception { assertTrue(ctx.channel().isActive()); latch.countDown(); super.handlerAdded(ctx); } }); LocalAddress addr = new LocalAddress("local"); ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register(); chan = cf.channel(); ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class) .childHandler(new ChannelHandlerAdapter() { }).group(group).bind(addr); server = sf.channel(); sf.sync(); assertEquals(1, latch.getCount()); chan.connect(addr).sync(); chan.pipeline().fireUserEventTriggered(ProtocolNegotiationEvent.DEFAULT); assertTrue(latch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)); assertNull(chan.pipeline().context(WaitUntilActiveHandler.class)); }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void httpProxy_completes() throws Exception { DefaultEventLoopGroup elg = new DefaultEventLoopGroup(1); // ProxyHandler is incompatible with EmbeddedChannel because when channelRegistered() is called // the channel is already active. LocalAddress proxy = new LocalAddress("httpProxy_completes"); SocketAddress host = InetSocketAddress.createUnresolved("specialHost", 314); ChannelInboundHandler mockHandler = mock(ChannelInboundHandler.class); Channel serverChannel = new ServerBootstrap().group(elg).channel(LocalServerChannel.class) .childHandler(mockHandler).bind(proxy).sync().channel(); ProtocolNegotiator nego = ProtocolNegotiators.httpProxy(proxy, null, null, ProtocolNegotiators.plaintext()); // normally NettyClientTransport will add WBAEH which kick start the ProtocolNegotiation, // mocking the behavior using KickStartHandler. ChannelHandler handler = new KickStartHandler( nego.newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler())); Channel channel = new Bootstrap().group(elg).channel(LocalChannel.class).handler(handler).register().sync() .channel();/*www . j ava2s . c o m*/ pipeline = channel.pipeline(); // Wait for initialization to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); channel.connect(host).sync(); serverChannel.close(); ArgumentCaptor<ChannelHandlerContext> contextCaptor = ArgumentCaptor.forClass(ChannelHandlerContext.class); Mockito.verify(mockHandler).channelActive(contextCaptor.capture()); ChannelHandlerContext serverContext = contextCaptor.getValue(); final String golden = "isThisThingOn?"; ChannelFuture negotiationFuture = channel.writeAndFlush(bb(golden, channel)); // Wait for sending initial request to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler).channelRead(ArgumentMatchers.<ChannelHandlerContext>any(), objectCaptor.capture()); ByteBuf b = (ByteBuf) objectCaptor.getValue(); String request = b.toString(UTF_8); b.release(); assertTrue("No trailing newline: " + request, request.endsWith("\r\n\r\n")); assertTrue("No CONNECT: " + request, request.startsWith("CONNECT specialHost:314 ")); assertTrue("No host header: " + request, request.contains("host: specialHost:314")); assertFalse(negotiationFuture.isDone()); serverContext.writeAndFlush(bb("HTTP/1.1 200 OK\r\n\r\n", serverContext.channel())).sync(); negotiationFuture.sync(); channel.eventLoop().submit(NOOP_RUNNABLE).sync(); objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler, times(2)).channelRead(ArgumentMatchers.<ChannelHandlerContext>any(), objectCaptor.capture()); b = (ByteBuf) objectCaptor.getAllValues().get(1); // If we were using the real grpcHandler, this would have been the HTTP/2 preface String preface = b.toString(UTF_8); b.release(); assertEquals(golden, preface); channel.close(); }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void httpProxy_500() throws Exception { DefaultEventLoopGroup elg = new DefaultEventLoopGroup(1); // ProxyHandler is incompatible with EmbeddedChannel because when channelRegistered() is called // the channel is already active. LocalAddress proxy = new LocalAddress("httpProxy_500"); SocketAddress host = InetSocketAddress.createUnresolved("specialHost", 314); ChannelInboundHandler mockHandler = mock(ChannelInboundHandler.class); Channel serverChannel = new ServerBootstrap().group(elg).channel(LocalServerChannel.class) .childHandler(mockHandler).bind(proxy).sync().channel(); ProtocolNegotiator nego = ProtocolNegotiators.httpProxy(proxy, null, null, ProtocolNegotiators.plaintext()); // normally NettyClientTransport will add WBAEH which kick start the ProtocolNegotiation, // mocking the behavior using KickStartHandler. ChannelHandler handler = new KickStartHandler( nego.newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler())); Channel channel = new Bootstrap().group(elg).channel(LocalChannel.class).handler(handler).register().sync() .channel();/* w ww . jav a2 s. c o m*/ pipeline = channel.pipeline(); // Wait for initialization to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); channel.connect(host).sync(); serverChannel.close(); ArgumentCaptor<ChannelHandlerContext> contextCaptor = ArgumentCaptor.forClass(ChannelHandlerContext.class); Mockito.verify(mockHandler).channelActive(contextCaptor.capture()); ChannelHandlerContext serverContext = contextCaptor.getValue(); final String golden = "isThisThingOn?"; ChannelFuture negotiationFuture = channel.writeAndFlush(bb(golden, channel)); // Wait for sending initial request to complete channel.eventLoop().submit(NOOP_RUNNABLE).sync(); ArgumentCaptor<Object> objectCaptor = ArgumentCaptor.forClass(Object.class); Mockito.verify(mockHandler).channelRead(any(ChannelHandlerContext.class), objectCaptor.capture()); ByteBuf request = (ByteBuf) objectCaptor.getValue(); request.release(); assertFalse(negotiationFuture.isDone()); String response = "HTTP/1.1 500 OMG\r\nContent-Length: 4\r\n\r\noops"; serverContext.writeAndFlush(bb(response, serverContext.channel())).sync(); thrown.expect(ProxyConnectException.class); try { negotiationFuture.sync(); } finally { channel.close(); } }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void clientTlsHandler_firesNegotiation() throws Exception { SelfSignedCertificate cert = new SelfSignedCertificate("authority"); SslContext clientSslContext = GrpcSslContexts .configure(SslContextBuilder.forClient().trustManager(cert.cert())).build(); SslContext serverSslContext = GrpcSslContexts .configure(SslContextBuilder.forServer(cert.key(), cert.cert())).build(); FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler(); ClientTlsProtocolNegotiator pn = new ClientTlsProtocolNegotiator(clientSslContext); WriteBufferingAndExceptionHandler clientWbaeh = new WriteBufferingAndExceptionHandler(pn.newHandler(gh)); SocketAddress addr = new LocalAddress("addr"); ChannelHandler sh = ProtocolNegotiators.serverTls(serverSslContext) .newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler()); WriteBufferingAndExceptionHandler serverWbaeh = new WriteBufferingAndExceptionHandler(sh); Channel s = new ServerBootstrap().childHandler(serverWbaeh).group(group).channel(LocalServerChannel.class) .bind(addr).sync().channel(); Channel c = new Bootstrap().handler(clientWbaeh).channel(LocalChannel.class).group(group).register().sync() .channel();/*from ww w .j a va2s. com*/ ChannelFuture write = c.writeAndFlush(NettyClientHandler.NOOP_MESSAGE); c.connect(addr).sync(); write.sync(); boolean completed = gh.negotiated.await(TIMEOUT_SECONDS, TimeUnit.SECONDS); if (!completed) { assertTrue("failed to negotiated", write.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)); // sync should fail if we are in this block. write.sync(); throw new AssertionError("neither wrote nor negotiated"); } c.close(); s.close(); assertThat(gh.securityInfo).isNotNull(); assertThat(gh.securityInfo.tls).isNotNull(); assertThat(gh.attrs.get(GrpcAttributes.ATTR_SECURITY_LEVEL)).isEqualTo(SecurityLevel.PRIVACY_AND_INTEGRITY); assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_SSL_SESSION)).isInstanceOf(SSLSession.class); // This is not part of the ClientTls negotiation, but shows that the negotiation event happens // in the right order. assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)).isEqualTo(addr); }
From source file:io.grpc.netty.ProtocolNegotiatorsTest.java
License:Apache License
@Test public void plaintextUpgradeNegotiator() throws Exception { LocalAddress addr = new LocalAddress("plaintextUpgradeNegotiator"); UpgradeCodecFactory ucf = new UpgradeCodecFactory() { @Override/*www . ja va 2 s .c o m*/ public UpgradeCodec newUpgradeCodec(CharSequence protocol) { return new Http2ServerUpgradeCodec(FakeGrpcHttp2ConnectionHandler.newHandler()); } }; final HttpServerCodec serverCodec = new HttpServerCodec(); final HttpServerUpgradeHandler serverUpgradeHandler = new HttpServerUpgradeHandler(serverCodec, ucf); Channel serverChannel = new ServerBootstrap().group(group).channel(LocalServerChannel.class) .childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(serverCodec, serverUpgradeHandler); } }).bind(addr).sync().channel(); FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler(); ProtocolNegotiator nego = ProtocolNegotiators.plaintextUpgrade(); ChannelHandler ch = nego.newHandler(gh); WriteBufferingAndExceptionHandler wbaeh = new WriteBufferingAndExceptionHandler(ch); Channel channel = new Bootstrap().group(group).channel(LocalChannel.class).handler(wbaeh).register().sync() .channel(); ChannelFuture write = channel.writeAndFlush(NettyClientHandler.NOOP_MESSAGE); channel.connect(serverChannel.localAddress()); boolean completed = gh.negotiated.await(TIMEOUT_SECONDS, TimeUnit.SECONDS); if (!completed) { assertTrue("failed to negotiated", write.await(TIMEOUT_SECONDS, TimeUnit.SECONDS)); // sync should fail if we are in this block. write.sync(); throw new AssertionError("neither wrote nor negotiated"); } channel.close().sync(); serverChannel.close(); assertThat(gh.securityInfo).isNull(); assertThat(gh.attrs.get(GrpcAttributes.ATTR_SECURITY_LEVEL)).isEqualTo(SecurityLevel.NONE); assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)).isEqualTo(addr); }
From source file:io.grpc.netty.WriteBufferingAndExceptionHandlerTest.java
License:Apache License
@Test public void connectionFailuresPropagated() throws Exception { WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler( new ChannelHandlerAdapter() { });//from w w w . j a va 2s . com ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register(); chan = cf.channel(); cf.sync(); // Write before connect. In the event connect fails, the pipeline is torn down and the handler // won't be able to fail the writes with the correct exception. ChannelFuture wf = chan.writeAndFlush(new Object()); chan.connect(new LocalAddress("bogus")); try { wf.sync(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(ConnectException.class); assertThat(e).hasMessageThat().contains("connection refused"); } }
From source file:io.grpc.netty.WriteBufferingAndExceptionHandlerTest.java
License:Apache License
@Test public void channelInactiveFailuresPropagated() throws Exception { WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler( new ChannelHandlerAdapter() { });/*www. j av a 2 s.c om*/ LocalAddress addr = new LocalAddress("local"); ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register(); chan = cf.channel(); cf.sync(); ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class) .childHandler(new ChannelHandlerAdapter() { }).group(group).bind(addr); server = sf.channel(); sf.sync(); ChannelFuture wf = chan.writeAndFlush(new Object()); chan.connect(addr); chan.pipeline().fireChannelInactive(); try { wf.sync(); fail(); } catch (Exception e) { Status status = Status.fromThrowable(e); assertThat(status.getCode()).isEqualTo(Code.UNAVAILABLE); assertThat(status.getDescription()).contains("Connection closed while performing protocol negotiation"); } }
From source file:io.grpc.netty.WriteBufferingAndExceptionHandlerTest.java
License:Apache License
@Test public void channelCloseFailuresPropagated() throws Exception { WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler( new ChannelHandlerAdapter() { });//from ww w .j a va2 s . c o m LocalAddress addr = new LocalAddress("local"); ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register(); chan = cf.channel(); cf.sync(); ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class) .childHandler(new ChannelHandlerAdapter() { }).group(group).bind(addr); server = sf.channel(); sf.sync(); ChannelFuture wf = chan.writeAndFlush(new Object()); chan.connect(addr); chan.close(); try { wf.sync(); fail(); } catch (Exception e) { Status status = Status.fromThrowable(e); assertThat(status.getCode()).isEqualTo(Code.UNAVAILABLE); assertThat(status.getDescription()) .contains("Connection closing while performing protocol negotiation"); } }
From source file:io.grpc.netty.WriteBufferingAndExceptionHandlerTest.java
License:Apache License
@Test public void uncaughtExceptionFailuresPropagated() throws Exception { WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler( new ChannelHandlerAdapter() { });/* ww w.j av a 2 s . co m*/ LocalAddress addr = new LocalAddress("local"); ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register(); chan = cf.channel(); cf.sync(); ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class) .childHandler(new ChannelHandlerAdapter() { }).group(group).bind(addr); server = sf.channel(); sf.sync(); ChannelFuture wf = chan.writeAndFlush(new Object()); chan.connect(addr); chan.pipeline().fireExceptionCaught(Status.ABORTED.withDescription("zap").asRuntimeException()); try { wf.sync(); fail(); } catch (Exception e) { Status status = Status.fromThrowable(e); assertThat(status.getCode()).isEqualTo(Code.ABORTED); assertThat(status.getDescription()).contains("zap"); } }
From source file:io.grpc.netty.WriteBufferingAndExceptionHandlerTest.java
License:Apache License
@Test public void uncaughtException_closeAtMostOnce() throws Exception { final AtomicInteger closes = new AtomicInteger(); WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler( new ChannelDuplexHandler() { @Override//from w w w . ja v a 2s. c o m public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception { closes.getAndIncrement(); // Simulates a loop between this handler and the WriteBufferingAndExceptionHandler. ctx.fireExceptionCaught(Status.ABORTED.withDescription("zap").asRuntimeException()); super.close(ctx, promise); } }); LocalAddress addr = new LocalAddress("local"); ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register(); chan = cf.channel(); cf.sync(); ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class) .childHandler(new ChannelHandlerAdapter() { }).group(group).bind(addr); server = sf.channel(); sf.sync(); chan.connect(addr).sync(); chan.close().sync(); assertEquals(1, closes.get()); }