ServerSocketService extends AbstractExecutionThreadService { public ListenableFuture<OpenedSocket> getConnection(UUID id) { return Futures.transform(getSocket(id), OPENED_SOCKET_FUNCTION, directExecutor()); } @Inject  ServerSocketService(CaliperOptions caliperOptions); int getPort(); ListenableFuture<OpenedSocket> getConnection(UUID id); ListenableFuture<InputStream> getInputStream(UUID id);  }
@Test public void getConnectionId_requestComesInFirst() throws Exception { UUID id = UUID.randomUUID(); ListenableFuture<OpenedSocket> pendingServerConnection = service.getConnection(id); assertFalse(pendingServerConnection.isDone()); Future<OpenedSocket> clientSocketFuture = createOpenedSocket(openConnectionAndIdentify(id)); assertEndsConnected(clientSocketFuture.get(), pendingServerConnection.get()); }
@Test public void getConnectionIdTwice_acceptComesFirst() throws Exception { UUID id = UUID.randomUUID(); Future<OpenedSocket> clientSocketFuture = createOpenedSocket(openConnectionAndIdentify(id)); ListenableFuture<OpenedSocket> pendingServerConnection = service.getConnection(id); OpenedSocket clientSocket = clientSocketFuture.get(); OpenedSocket serverSocket = pendingServerConnection.get(); assertEndsConnected(clientSocket, serverSocket); try { service.getConnection(id).get(); fail(); } catch (IllegalStateException expected) { } }
@Test public void getConnectionStoppedService() throws Exception { UUID id = UUID.randomUUID(); ListenableFuture<OpenedSocket> pendingServerConnection = service.getConnection(id); assertFalse(pendingServerConnection.isDone()); service.stopAsync().awaitTerminated(); assertTrue(pendingServerConnection.isDone()); try { pendingServerConnection.get(); fail(); } catch (ExecutionException e) { assertEquals("The socket has been closed", e.getCause().getMessage()); } try { service.getConnection(UUID.randomUUID()); fail(); } catch (IllegalStateException expected) { } }