Search in sources :

Example 6 with NoopRegistry

use of com.netflix.spectator.api.NoopRegistry in project zuul by Netflix.

the class ServerTest method getListeningSockets.

@Test
public void getListeningSockets() throws Exception {
    ServerStatusManager ssm = mock(ServerStatusManager.class);
    Map<NamedSocketAddress, ChannelInitializer<?>> initializers = new HashMap<>();
    final List<NioSocketChannel> nioChannels = Collections.synchronizedList(new ArrayList<NioSocketChannel>());
    ChannelInitializer<Channel> init = new ChannelInitializer<Channel>() {

        @Override
        protected void initChannel(final Channel ch) {
            LOGGER.info("Channel: " + ch.getClass().getName() + ", isActive=" + ch.isActive() + ", isOpen=" + ch.isOpen());
            if (ch instanceof NioSocketChannel) {
                nioChannels.add((NioSocketChannel) ch);
            }
        }
    };
    initializers.put(new NamedSocketAddress("test", new InetSocketAddress(0)), init);
    // The port to channel map keys on the port, post bind. This should be unique even if InetAddress is same
    initializers.put(new NamedSocketAddress("test2", new InetSocketAddress(0)), init);
    ClientConnectionsShutdown ccs = new ClientConnectionsShutdown(new DefaultChannelGroup(GlobalEventExecutor.INSTANCE), GlobalEventExecutor.INSTANCE, /* discoveryClient= */
    null);
    EventLoopGroupMetrics elgm = new EventLoopGroupMetrics(Spectator.globalRegistry());
    EventLoopConfig elc = new EventLoopConfig() {

        @Override
        public int eventLoopCount() {
            return 1;
        }

        @Override
        public int acceptorCount() {
            return 1;
        }
    };
    Server s = new Server(new NoopRegistry(), ssm, initializers, ccs, elgm, elc);
    s.start();
    List<NamedSocketAddress> addrs = s.getListeningAddresses();
    assertEquals(2, addrs.size());
    for (NamedSocketAddress address : addrs) {
        assertTrue(address.unwrap() instanceof InetSocketAddress);
        final int port = ((InetSocketAddress) address.unwrap()).getPort();
        assertNotEquals(port, 0);
        checkConnection(port);
    }
    await().atMost(1, SECONDS).until(() -> nioChannels.size() == 2);
    s.stop();
    assertEquals(2, nioChannels.size());
    for (NioSocketChannel ch : nioChannels) {
        assertTrue("isShutdown", ch.isShutdown());
    }
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ServerStatusManager(com.netflix.netty.common.status.ServerStatusManager) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroupMetrics(com.netflix.netty.common.metrics.EventLoopGroupMetrics) NoopRegistry(com.netflix.spectator.api.NoopRegistry) ChannelInitializer(io.netty.channel.ChannelInitializer) Test(org.junit.Test)

Aggregations

NoopRegistry (com.netflix.spectator.api.NoopRegistry)6 Channel (io.netty.channel.Channel)5 DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)5 Test (org.junit.Test)5 ChannelConfig (com.netflix.netty.common.channel.config.ChannelConfig)4 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)4 NullChannelHandlerProvider (com.netflix.zuul.netty.ratelimiting.NullChannelHandlerProvider)3 ChannelGroup (io.netty.channel.group.ChannelGroup)3 SourceAddressChannelHandler (com.netflix.netty.common.SourceAddressChannelHandler)2 EventLoopGroupMetrics (com.netflix.netty.common.metrics.EventLoopGroupMetrics)2 ServerStatusManager (com.netflix.netty.common.status.ServerStatusManager)2 MaxInboundConnectionsHandler (com.netflix.netty.common.throttle.MaxInboundConnectionsHandler)2 ChannelInitializer (io.netty.channel.ChannelInitializer)2 InetSocketAddress (java.net.InetSocketAddress)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 Http2ConnectionCloseHandler (com.netflix.netty.common.Http2ConnectionCloseHandler)1 Http2ConnectionExpiryHandler (com.netflix.netty.common.Http2ConnectionExpiryHandler)1 ChannelConfigValue (com.netflix.netty.common.channel.config.ChannelConfigValue)1 CommonChannelConfigKeys (com.netflix.netty.common.channel.config.CommonChannelConfigKeys)1 Http2MetricsChannelHandlers (com.netflix.netty.common.metrics.Http2MetricsChannelHandlers)1