NettyServer implements InternalServer { @Override public int getPort() { if (channel == null) { return -1; } SocketAddress localAddr = channel.localAddress(); if (!(localAddr instanceof InetSocketAddress)) { return -1; } return ((InetSocketAddress) localAddr).getPort(); } NettyServer(SocketAddress address, Class<? extends ServerChannel> channelType,
              @Nullable EventLoopGroup bossGroup, @Nullable EventLoopGroup workerGroup,
              ProtocolNegotiator protocolNegotiator, int maxStreamsPerConnection,
              int flowControlWindow, int maxMessageSize, int maxHeaderListSize); @Override int getPort(); @Override void start(ServerListener serverListener); @Override void shutdown();  }
@Test public void getPort() throws Exception { InetSocketAddress addr = new InetSocketAddress(0); NettyServer ns = new NettyServer( addr, NioServerSocketChannel.class, null, null, new ProtocolNegotiators.PlaintextNegotiator(), 1, 1, 1, 1); ns.start(new ServerListener() { @Override public ServerTransportListener transportCreated(ServerTransport transport) { return null; } @Override public void serverShutdown() {} }); assertThat(ns.getPort()).isGreaterThan(0); ns.shutdown(); }
@Test public void getPort_notStarted() throws Exception { InetSocketAddress addr = new InetSocketAddress(0); NettyServer ns = new NettyServer( addr, NioServerSocketChannel.class, null, null, new ProtocolNegotiators.PlaintextNegotiator(), 1, 1, 1, 1); assertThat(ns.getPort()).isEqualTo(-1); }