List of usage examples for io.netty.channel DefaultChannelPromise DefaultChannelPromise
public DefaultChannelPromise(Channel channel)
From source file:org.apache.bookkeeper.proto.WriteEntryProcessorV3Test.java
License:Apache License
@Test public void testNormalWritesOnWritableBookie() throws Exception { when(bookie.isReadOnly()).thenReturn(false); when(channel.voidPromise()).thenReturn(mock(ChannelPromise.class)); when(channel.writeAndFlush(any())).thenReturn(mock(ChannelPromise.class)); doAnswer(invocationOnMock -> {//w w w. j a va 2s .com WriteCallback wc = invocationOnMock.getArgument(2); wc.writeComplete(0, request.getAddRequest().getLedgerId(), request.getAddRequest().getEntryId(), null, null); return null; }).when(bookie).addEntry(any(ByteBuf.class), eq(false), any(WriteCallback.class), same(channel), eq(new byte[0])); ChannelPromise promise = new DefaultChannelPromise(channel); AtomicReference<Object> writtenObject = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); doAnswer(invocationOnMock -> { writtenObject.set(invocationOnMock.getArgument(0)); latch.countDown(); return promise; }).when(channel).writeAndFlush(any()); processor.run(); verify(bookie, times(1)).addEntry(any(ByteBuf.class), eq(false), any(WriteCallback.class), same(channel), eq(new byte[0])); verify(channel, times(1)).writeAndFlush(any(Response.class)); latch.await(); assertTrue(writtenObject.get() instanceof Response); Response response = (Response) writtenObject.get(); assertEquals(StatusCode.EOK, response.getStatus()); }
From source file:org.apache.drill.exec.server.rest.WebSessionResourcesTest.java
License:Apache License
/** * Validates {@link WebSessionResources#close()} throws NPE when closefuture passed to WebSessionResources doesn't * have a valid channel and EventExecutor associated with it. * @throws Exception/*from www. j a v a 2 s. c o m*/ */ @Test public void testChannelPromiseWithNullExecutor() throws Exception { try { ChannelPromise closeFuture = new DefaultChannelPromise(null); webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture); webSessionResources.close(); fail(); } catch (Exception e) { assertTrue(e instanceof NullPointerException); verify(webSessionResources.getAllocator()).close(); verify(webSessionResources.getSession()).close(); } }
From source file:org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.AsyncSshHandlerTest.java
License:Open Source License
private ChannelPromise getMockedPromise() { return spy(new DefaultChannelPromise(channel)); }
From source file:org.opendaylight.netconf.client.TcpClientChannelInitializer.java
License:Open Source License
@Override public void initialize(final Channel ch, final Promise<NetconfClientSession> promise) { final Future<NetconfClientSession> negotiationFuture = promise; //We have to add this channel outbound handler to channel pipeline, in order //to get notifications from netconf negotiatior. Set connection promise to //success only after successful negotiation. ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() { ChannelPromise connectPromise;//from w w w.j a v a 2 s .com GenericFutureListener<Future<NetconfClientSession>> negotiationFutureListener; @Override public void connect(final ChannelHandlerContext ctx, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise channelPromise) throws Exception { connectPromise = channelPromise; ChannelPromise tcpConnectFuture = new DefaultChannelPromise(ch); negotiationFutureListener = new GenericFutureListener<Future<NetconfClientSession>>() { @Override public void operationComplete(final Future<NetconfClientSession> future) throws Exception { if (future.isSuccess()) { connectPromise.setSuccess(); } } }; tcpConnectFuture.addListener(new GenericFutureListener<Future<? super Void>>() { @Override public void operationComplete(final Future<? super Void> future) throws Exception { if (future.isSuccess()) { //complete connection promise with netconf negotiation future negotiationFuture.addListener(negotiationFutureListener); } else { connectPromise.setFailure(future.cause()); } } }); ctx.connect(remoteAddress, localAddress, tcpConnectFuture); } @Override public void disconnect(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception { // If we have already succeeded and the session was dropped after, we need to fire inactive to notify reconnect logic if (connectPromise.isSuccess()) { ctx.fireChannelInactive(); } //If connection promise is not already set, it means negotiation failed //we must set connection promise to failure if (!connectPromise.isDone()) { connectPromise.setFailure(new IllegalStateException("Negotiation failed")); } //Remove listener from negotiation future, we don't want notifications //from negotiation anymore negotiationFuture.removeListener(negotiationFutureListener); super.disconnect(ctx, promise); promise.setSuccess(); } }); super.initialize(ch, promise); }
From source file:org.opendaylight.netconf.nettyutil.AbstractNetconfSession.java
License:Open Source License
@Override public ChannelFuture sendMessage(final NetconfMessage netconfMessage) { // From: https://github.com/netty/netty/issues/3887 // Netty can provide "ordering" in the following situations: // 1. You are doing all writes from the EventLoop thread; OR // 2. You are doing no writes from the EventLoop thread (i.e. all writes are being done in other thread(s)). ///*from ww w. j a v a 2 s . c o m*/ // Restconf writes to a netconf mountpoint execute multiple messages // and one of these was executed from a restconf thread thus breaking ordering so // we need to execute all messages from an EventLoop thread. final DefaultChannelPromise proxyFuture = new DefaultChannelPromise(channel); channel.eventLoop().execute(new Runnable() { @Override public void run() { final ChannelFuture future = channel.writeAndFlush(netconfMessage); future.addListener(new FutureListener<Void>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (future.isSuccess()) { proxyFuture.setSuccess(); } else { proxyFuture.setFailure(future.cause()); } } }); if (delayedEncoder != null) { replaceMessageEncoder(delayedEncoder); delayedEncoder = null; } } }); return proxyFuture; }
From source file:org.opendaylight.protocol.bgp.rib.impl.PeerTest.java
License:Open Source License
private void mockSession() { final EventLoop eventLoop = Mockito.mock(EventLoop.class); final Channel channel = Mockito.mock(Channel.class); final ChannelPipeline pipeline = Mockito.mock(ChannelPipeline.class); Mockito.doReturn(null).when(eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class)); Mockito.doReturn(eventLoop).when(channel).eventLoop(); Mockito.doReturn(Boolean.TRUE).when(channel).isWritable(); Mockito.doReturn(null).when(channel).close(); Mockito.doReturn(pipeline).when(channel).pipeline(); Mockito.doCallRealMethod().when(channel).toString(); Mockito.doReturn(pipeline).when(pipeline).addLast(Mockito.any(ChannelHandler.class)); Mockito.doReturn(new DefaultChannelPromise(channel)).when(channel).writeAndFlush(any(Notification.class)); Mockito.doReturn(new InetSocketAddress("localhost", 12345)).when(channel).remoteAddress(); Mockito.doReturn(new InetSocketAddress("localhost", 12345)).when(channel).localAddress(); final List<BgpParameters> params = Lists.newArrayList(new BgpParametersBuilder() .setOptionalCapabilities(Lists.newArrayList(new OptionalCapabilitiesBuilder() .setCParameters(new CParametersBuilder().addAugmentation(CParameters1.class, new CParameters1Builder() .setMultiprotocolCapability( new MultiprotocolCapabilityBuilder().setAfi(Ipv4AddressFamily.class) .setSafi(UnicastSubsequentAddressFamily.class).build()) .build()) .build())/* w w w. j a va 2 s . c o m*/ .build())) .build()); final Open openObj = new OpenBuilder().setBgpIdentifier(new Ipv4Address("1.1.1.1")).setHoldTimer(50) .setMyAsNumber(72).setBgpParameters(params).build(); this.session = new BGPSessionImpl(this.classic, channel, openObj, 30, null); this.session.setChannelExtMsgCoder(openObj); }
From source file:org.opendaylight.protocol.pcep.impl.AbstractPCEPSessionTest.java
License:Open Source License
@Before public final void setUp() { MockitoAnnotations.initMocks(this); final ChannelFuture future = new DefaultChannelPromise(this.channel); doAnswer(new Answer<Object>() { @Override/*from www . jav a 2s.c o m*/ public Object answer(final InvocationOnMock invocation) { final Object[] args = invocation.getArguments(); AbstractPCEPSessionTest.this.msgsSend.add((Notification) args[0]); return future; } }).when(this.channel).writeAndFlush(any(Notification.class)); doReturn(this.channelFuture).when(this.channel).closeFuture(); doReturn(this.channelFuture).when(this.channelFuture).addListener(any(GenericFutureListener.class)); doReturn("TestingChannel").when(this.channel).toString(); doReturn(this.pipeline).when(this.channel).pipeline(); doReturn(this.address).when(this.channel).localAddress(); doReturn(this.address).when(this.channel).remoteAddress(); doReturn(this.eventLoop).when(this.channel).eventLoop(); doReturn(true).when(this.future).cancel(false); doReturn(this.future).when(this.eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class)); doReturn(this.pipeline).when(this.pipeline).replace(any(ChannelHandler.class), any(String.class), any(ChannelHandler.class)); doReturn(this.pipeline).when(this.pipeline).addFirst(any(ChannelHandler.class)); doReturn(true).when(this.channel).isActive(); doReturn(mock(ChannelFuture.class)).when(this.channel).close(); doReturn(new InetSocketAddress(ipAddress, port)).when(this.channel).remoteAddress(); doReturn(new InetSocketAddress(ipAddress, port)).when(this.channel).localAddress(); this.openMsg = new OpenBuilder().setOpenMessage(new OpenMessageBuilder().setOpen( new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.open.object.OpenBuilder() .setDeadTimer(DEADTIMER).setKeepalive(KEEP_ALIVE).setSessionId((short) 0).build()) .build()).build(); this.kaMsg = new KeepaliveBuilder().setKeepaliveMessage(new KeepaliveMessageBuilder().build()).build(); this.startTlsMsg = new StarttlsBuilder().setStartTlsMessage(new StartTlsMessageBuilder().build()).build(); this.listener = new SimpleSessionListener(); }