RpcStreamConnection extends RpcConnection { public RpcPacket getRpcPacket() throws ConnectionException { return getRpcPacket(null, null); } RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset, boolean secure);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset, Socket socket);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset, RpcSocketPool pool);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset, Socket socket, boolean secure);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset, RpcSocketPool pool, boolean secure);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset, Socket socket, RpcSocketPool pool, boolean secure);  RpcStreamConnection(String serverHost, int serverPort, Properties props,
            ServerStats stats, Charset charset, Socket socket, RpcSocketPool pool, boolean secure,
            String rsh); String getServerIpPort(); String getClientIpPort(); void disconnect(final RpcPacketDispatcher dispatcher); RpcPacket getRpcPacket(); RpcPacket getRpcPacket(final RpcPacketFieldRule fieldRule,
            final IFilterCallback filterCallback); int getSystemRecvBufferSize(); int getSystemSendBufferSize(); long putRpcPackets(@Nonnull RpcPacket[] packets); long putRpcPacket(@Nonnull RpcPacket packet); @Override void useConnectionCompression(); static final String TRACE_PREFIX; }
@Test public void given_topInputStream_read_return_lessThanZero_when_getRpcPacket() throws Exception { RpcPacketFieldRule fieldRule = mock(RpcPacketFieldRule.class); IFilterCallback filterCallback = mock(IFilterCallback.class); when(topInputStream.read(any((byte[].class)))).thenReturn(-1); expectThrows(ConnectionException.class, () -> mockConnection.getRpcPacket(fieldRule, filterCallback)); }
@Test public void getRpcPacket() throws IOException, ConnectionException { RpcPacketFieldRule fieldRule = mock(RpcPacketFieldRule.class); IFilterCallback filterCallback = mock(IFilterCallback.class); reset(topInputStream); byte[] preambleBytes = { -116, -116, 0, 0, 0 }; byte[] payloadBytes = { 120, 102, 105, 108, 101, 115, 0, 1, 0, 0, 0, 55, 0, 115, 101, 114, 118, 101, 114, 0, 1, 0, 0, 0, 51, 0, 115, 101, 114, 118, 101, 114, 50, 0, 2, 0, 0, 0, 52, 49, 0, 115, 101, 114, 118, 101, 114, 73, 68, 0, 0, 0, 0, 0, 0, 114, 101, 118, 118, 101, 114, 0, 1, 0, 0, 0, 57, 0, 116, 122, 111, 102, 102, 115, 101, 116, 0, 4, 0, 0, 0, 55, 50, 48, 48, 0, 115, 110, 100, 98, 117, 102, 0, 6, 0, 0, 0, 51, 49, 57, 52, 56, 55, 0, 114, 99, 118, 98, 117, 102, 0, 6, 0, 0, 0, 51, 49, 57, 52, 56, 56, 0, 102, 117, 110, 99, 0, 8, 0, 0, 0, 112, 114, 111, 116, 111, 99, 111, 108, 0 }; byte[] buf = new byte[preambleBytes.length + payloadBytes.length]; System.arraycopy(preambleBytes, 0, buf, 0, preambleBytes.length); System.arraycopy(payloadBytes, 0, buf, preambleBytes.length, payloadBytes.length); topInputStream = new ByteArrayInputStream(buf); mockConnection.topInputStream(topInputStream); RpcPacket rpcPacket = mockConnection.getRpcPacket(fieldRule, filterCallback); assertNotNull(rpcPacket); assertThat(rpcPacket.getPacketLength(), is(payloadBytes.length)); }
@Test public void getRpcPacket_with_exceptions() throws IOException, ConnectionException { RpcPacketFieldRule fieldRule = mock(RpcPacketFieldRule.class); IFilterCallback filterCallback = mock(IFilterCallback.class); doThrow(IOException.class).when(topInputStream).read(any()); expectThrows(ConnectionException.class, () -> mockConnection.getRpcPacket(fieldRule, filterCallback)); reset(topInputStream); doThrow(ConnectionException.class).when(topInputStream).read(any()); expectThrows(ConnectionException.class, () -> mockConnection.getRpcPacket(fieldRule, filterCallback)); reset(topInputStream); doThrow(P4JavaError.class).when(topInputStream).read(any()); expectThrows(P4JavaError.class, () -> mockConnection.getRpcPacket(fieldRule, filterCallback)); reset(topInputStream); doThrow(Throwable.class).when(topInputStream).read(any()); expectThrows(P4JavaError.class, () -> mockConnection.getRpcPacket(fieldRule, filterCallback)); }