Search in sources :

Example 1 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise in project bgpcep by opendaylight.

the class PeerTest method mockSession.

private void mockSession() {
    final EventLoop eventLoop = mock(EventLoop.class);
    final Channel channel = mock(Channel.class);
    final ChannelPipeline pipeline = mock(ChannelPipeline.class);
    doReturn(null).when(eventLoop).schedule(any(Runnable.class), any(long.class), any(TimeUnit.class));
    doReturn(eventLoop).when(channel).eventLoop();
    doReturn(Boolean.TRUE).when(channel).isWritable();
    doReturn(null).when(channel).close();
    doReturn(pipeline).when(channel).pipeline();
    doCallRealMethod().when(channel).toString();
    doReturn(pipeline).when(pipeline).addLast(any(ChannelHandler.class));
    doReturn(new DefaultChannelPromise(channel)).when(channel).writeAndFlush(any(Notification.class));
    doReturn(new InetSocketAddress("localhost", 12345)).when(channel).remoteAddress();
    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()).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);
}
Also used : OptionalCapabilitiesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.OptionalCapabilitiesBuilder) MultiprotocolCapabilityBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.mp.capabilities.MultiprotocolCapabilityBuilder) OpenBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.OpenBuilder) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ChannelHandler(io.netty.channel.ChannelHandler) BgpParameters(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParameters) BgpParametersBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.BgpParametersBuilder) CParameters1(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.CParameters1) ChannelPipeline(io.netty.channel.ChannelPipeline) Notification(org.opendaylight.yangtools.yang.binding.Notification) Open(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.Open) CParametersBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.open.message.bgp.parameters.optional.capabilities.CParametersBuilder) CParameters1Builder(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev171207.CParameters1Builder) EventLoop(io.netty.channel.EventLoop) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) TimeUnit(java.util.concurrent.TimeUnit) UnicastSubsequentAddressFamily(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily) Ipv4Address(org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)

Example 2 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise in project drill by axbaretto.

the class WebSessionResourcesTest method testChannelPromiseWithValidExecutor.

/**
 * Validates successful {@link WebSessionResources#close()} with valid CloseFuture and other parameters.
 * @throws Exception
 */
@Test
public void testChannelPromiseWithValidExecutor() throws Exception {
    try {
        EventExecutor mockExecutor = mock(EventExecutor.class);
        ChannelPromise closeFuture = new DefaultChannelPromise(null, mockExecutor);
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture);
        webSessionResources.close();
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        verify(mockExecutor).inEventLoop();
        verify(mockExecutor).execute(any(Runnable.class));
        assertTrue(webSessionResources.getCloseFuture() == null);
        assertTrue(!listenerComplete);
    } catch (Exception e) {
        fail();
    }
}
Also used : EventExecutor(io.netty.util.concurrent.EventExecutor) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Test(org.junit.Test)

Example 3 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise in project drill by axbaretto.

the class WebSessionResourcesTest method testCloseWithListener.

/**
 * Validates successful {@link WebSessionResources#close()} with valid CloseFuture and {@link TestClosedListener}
 * getting invoked which is added to the close future.
 * @throws Exception
 */
@Test
public void testCloseWithListener() throws Exception {
    try {
        // Assign latch, executor and closeListener for this test case
        GenericFutureListener<Future<Void>> closeListener = new TestClosedListener();
        latch = new CountDownLatch(1);
        executor = TransportCheck.createEventLoopGroup(1, "Test-Thread").next();
        ChannelPromise closeFuture = new DefaultChannelPromise(null, executor);
        // create WebSessionResources with above ChannelPromise to notify listener
        webSessionResources = new WebSessionResources(mock(BufferAllocator.class), mock(SocketAddress.class), mock(UserSession.class), closeFuture);
        // Add the Test Listener to close future
        assertTrue(!listenerComplete);
        closeFuture.addListener(closeListener);
        // Close the WebSessionResources
        webSessionResources.close();
        // Verify the states
        verify(webSessionResources.getAllocator()).close();
        verify(webSessionResources.getSession()).close();
        assertTrue(webSessionResources.getCloseFuture() == null);
        // Since listener will be invoked so test should not wait forever
        latch.await();
        assertTrue(listenerComplete);
    } catch (Exception e) {
        fail();
    } finally {
        listenerComplete = false;
        executor.shutdownGracefully();
    }
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Future(io.netty.util.concurrent.Future) ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise in project x-pipe by ctripcorp.

the class DefaultNettyClient method sendRequest.

@Override
public void sendRequest(ByteBuf byteBuf, final ByteBufReceiver byteBufReceiver) {
    logger.debug("[sendRequest][begin]{}, {}", byteBufReceiver, this);
    DefaultChannelPromise future = new DefaultChannelPromise(channel);
    future.addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                logger.debug("[operationComplete][add receiver]{}, {}", byteBufReceiver, this);
                receivers.offer(byteBufReceiver);
            } else {
                logger.error("[sendRequest][fail]" + channel, future.cause());
            }
        }
    });
    logger.debug("[sendRequest][ end ]{}, {}", byteBufReceiver, this);
    channel.writeAndFlush(byteBuf, future);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ChannelFutureListener(io.netty.channel.ChannelFutureListener)

Example 5 with DefaultChannelPromise

use of io.netty.channel.DefaultChannelPromise in project x-pipe by ctripcorp.

the class DefaultRedisClientTest method beforeDefaultRedisClientTest.

@Before
public void beforeDefaultRedisClientTest() {
    when(channel.closeFuture()).thenReturn(new DefaultChannelPromise(channel));
    redisClient = new DefaultRedisClient(channel, redisKeeperServer);
}
Also used : DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Before(org.junit.Before)

Aggregations

DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)22 ChannelPromise (io.netty.channel.ChannelPromise)13 Test (org.junit.Test)8 ChannelFuture (io.netty.channel.ChannelFuture)6 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 QueuedCommand (io.grpc.netty.WriteQueue.QueuedCommand)4 ByteBuf (io.netty.buffer.ByteBuf)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)4 Before (org.junit.Before)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 Channel (io.netty.channel.Channel)3 GenericFutureListener (io.netty.util.concurrent.GenericFutureListener)3 ChannelHandler (io.netty.channel.ChannelHandler)2 EventExecutor (io.netty.util.concurrent.EventExecutor)2 Future (io.netty.util.concurrent.Future)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 TimeUnit (java.util.concurrent.TimeUnit)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AssertionFailedError (junit.framework.AssertionFailedError)2