### Question:
NettyServer implements InternalServer, InternalWithLogId { @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,
      Map<ChannelOption<?>, ?> channelOptions,
      @Nullable EventLoopGroup bossGroup, @Nullable EventLoopGroup workerGroup,
      ProtocolNegotiator protocolNegotiator, List<ServerStreamTracer.Factory> streamTracerFactories,
      TransportTracer.Factory transportTracerFactory,
      int maxStreamsPerConnection, int flowControlWindow, int maxMessageSize, int maxHeaderListSize,
      long keepAliveTimeInNanos, long keepAliveTimeoutInNanos,
      long maxConnectionIdleInNanos,
      long maxConnectionAgeInNanos, long maxConnectionAgeGraceInNanos,
      boolean permitKeepAliveWithoutCalls, long permitKeepAliveTimeInNanos,
      InternalChannelz channelz); @Override int getPort(); @Override List<InternalInstrumented<SocketStats>> getListenSockets(); @Override void start(ServerListener serverListener); @Override void shutdown(); @Override InternalLogId getLogId(); @Override String toString();  }### Answer:
@Test public void getPort() throws Exception { InetSocketAddress addr = new InetSocketAddress(0); NettyServer ns = new NettyServer( addr, NioServerSocketChannel.class, new HashMap<ChannelOption<?>, Object>(), null, null, new ProtocolNegotiators.PlaintextNegotiator(), Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), 1, 1, 1, 1, 1, 1, 1, 1, 1, true, 0, channelz); ns.start(new ServerListener() { @Override public ServerTransportListener transportCreated(ServerTransport transport) { return new NoopServerTransportListener(); } @Override public void serverShutdown() {} @Override public int getServerTransportCount() { return 0; } @Override public Collection<ServerTransport> getTransports() {return null;} }); 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, new HashMap<ChannelOption<?>, Object>(), null, null, new ProtocolNegotiators.PlaintextNegotiator(), Collections.<ServerStreamTracer.Factory>emptyList(), TransportTracer.getDefaultFactory(), 1, 1, 1, 1, 1, 1, 1, 1, 1, true, 0, channelz); assertThat(ns.getPort()).isEqualTo(-1); }