ServerSocketService extends AbstractExecutionThreadService { public ListenableFuture<InputStream> getInputStream(UUID id) { return Futures.transform(getSocket(id), INPUT_STREAM_FUNCTION, directExecutor()); } @Inject  ServerSocketService(CaliperOptions caliperOptions); int getPort(); ListenableFuture<OpenedSocket> getConnection(UUID id); ListenableFuture<InputStream> getInputStream(UUID id);  }
@Test public void getInputStream() throws Exception { UUID id = UUID.randomUUID(); ListenableFuture<InputStream> pendingServerConnection = service.getInputStream(id); assertFalse(pendingServerConnection.isDone()); Socket clientSocket = openConnectionAndIdentify(id); InputStream serverInputStream = pendingServerConnection.get(); OutputStream clientOutputStream = clientSocket.getOutputStream(); byte[] bytes = new byte[] {1, 2, 3, 4}; clientOutputStream.write(bytes); byte[] buf = new byte[bytes.length]; int bytesRead = 0; int r; while (bytesRead < buf.length && (r = serverInputStream.read(buf, bytesRead, buf.length - bytesRead)) != -1) { bytesRead += r; } assertThat(buf).isEqualTo(bytes); clientOutputStream.close(); serverInputStream.close(); }