Source:
public void testSocketSink() throws Exception { final CountDownLatch serverCreated = new CountDownLatch(1); final CountDownLatch endOfReceiving = new CountDownLatch(1); final ExecutorService serverExecutor = Executors.newSingleThreadExecutor(); final List<String> inputStream = Arrays.asList( "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "In in leo nec erat fringilla mattis eu non massa.", "Cras quis diam suscipit, commodo enim id, pulvinar nunc."); final List<String> result = new LinkedList<>(); serverExecutor.submit(() -> { try { serverCreated.countDown(); final Socket socket = serverSocket.accept(); System.out.println("Socket is connected to " + socket); final BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (result.size() < 3) { final String input = bf.readLine(); if (input != null) { result.add(input); } } bf.close(); socket.close(); endOfReceiving.countDown(); } catch (final IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }); serverCreated.await(); final JavaConfigurationBuilder jcb = Tang.Factory.getTang().newConfigurationBuilder(); jcb.bindNamedParameter(SocketServerIp.class, "127.0.0.1"); jcb.bindNamedParameter(SocketServerPort.class, "8030"); jcb.bindImplementation(Sink.class, TextSocketSink.class); final Injector injector = Tang.Factory.getTang().newInjector(jcb.build()); final Sink<String> sink = injector.getInstance(Sink.class); inputStream.stream().forEach(sink::handle); endOfReceiving.await(); sink.close(); Assert.assertEquals("TextSocketSink should send " + inputStream, inputStream, result); }

Target:
public void testSocketSink() throws Exception { final CountDownLatch serverCreated = new CountDownLatch(1); final CountDownLatch endOfReceiving = new CountDownLatch(1); final ExecutorService serverExecutor = Executors.newSingleThreadExecutor(); final List<String> inputStream = Arrays.asList( "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "In in leo nec erat fringilla mattis eu non massa.", "Cras quis diam suscipit, commodo enim id, pulvinar nunc."); final List<String> result = new LinkedList<>(); serverExecutor.submit(() -> { try { serverCreated.countDown(); final Socket socket = serverSocket.accept(); LOG.info("Socket is connected to " + socket); final BufferedReader bf = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (result.size() < inputStream.size()) { final String input = bf.readLine(); if (input != null) { result.add(input); } } bf.close(); socket.close(); endOfReceiving.countDown(); } catch (final IOException e) { e.printStackTrace(); throw new RuntimeException(e); } }); serverCreated.await(); final JavaConfigurationBuilder jcb = Tang.Factory.getTang().newConfigurationBuilder(); jcb.bindNamedParameter(SocketServerIp.class, serverIpAddress); jcb.bindNamedParameter(SocketServerPort.class, Integer.toString(port)); jcb.bindImplementation(Sink.class, TextSocketSink.class); final Injector injector = Tang.Factory.getTang().newInjector(jcb.build()); try (final Sink<String> sink = injector.getInstance(Sink.class)) { inputStream.stream().forEach(sink::handle); endOfReceiving.await(); } finally { Assert.assertEquals("TextSocketSink should send " + inputStream, inputStream, result); } }