private FutureTask<ExitCode> createFuture() { final FutureTask<ExitCode> newFuture = new FutureTask<>( new SocketReadingCallable(this.socket, this.sendInitialData, this.receive)); final Thread thread = new Thread(newFuture); thread.setDaemon(true); thread.setName("pit communication"); thread.start(); return newFuture; }
@Override public ExitCode call() throws Exception { try (Socket clientSocket = this.socket.accept()) { try (BufferedInputStream bif = new BufferedInputStream( clientSocket.getInputStream())) { sendDataToMinion(clientSocket); final SafeDataInputStream is = new SafeDataInputStream(bif); return receiveResults(is); } catch (final IOException e) { throw Unchecked.translateCheckedException(e); } } finally { try { this.socket.close(); } catch (final IOException e) { throw Unchecked.translateCheckedException(e); } } }
@Test public void shouldSendInitialDataToMinion() throws Exception { mockClientSocketToSendExitCode(ExitCode.TIMEOUT); this.testee.call(); verify(this.sendDataSideEffect).apply(any(SafeDataOutputStream.class)); }
@Test public void shouldPassNotPassDoneCommandToReceiver() throws Exception { mockClientSocketToSendExitCode(ExitCode.TIMEOUT); this.testee.call(); verify(this.receiveStrategy, never()).apply(anyByte(), any(SafeDataInputStream.class)); }
@Before public void setUp() throws IOException { MockitoAnnotations.initMocks(this); this.testee = new SocketReadingCallable(this.socket, this.sendDataSideEffect, this.receiveStrategy); when(this.socket.accept()).thenReturn(this.clientSocket); this.o = new ByteArrayOutputStream(); }
@Test public void shouldReportTheExitCodeSentByTheMinionProcess() throws Exception { mockClientSocketToSendExitCode(ExitCode.TIMEOUT); assertEquals(ExitCode.TIMEOUT, this.testee.call()); }
@Test public void shouldPassCommandsToReceiver() throws Exception { final SafeDataOutputStream dos = new SafeDataOutputStream(this.o); dos.writeByte(Id.DESCRIBE); dos.writeByte(Id.DONE); dos.writeInt(ExitCode.OK.getCode()); mockClientSocketInputStream(); this.testee.call(); verify(this.receiveStrategy, times(1)).apply(anyByte(), any(SafeDataInputStream.class)); }