class TestNailgunStreamReader(unittest.TestCase): def setUp(self): self.in_fd = FakeFile() self.mock_socket = mock.Mock() self.reader = NailgunStreamReader(in_fd=self.in_fd, sock=self.mock_socket) def test_stop(self): self.assertFalse(self.reader.is_stopped) self.reader.stop() self.assertTrue(self.reader.is_stopped) self.reader.run() def test_startable(self): self.assertTrue(inspect.ismethod(self.reader.start)) @mock.patch('select.select') def test_run_stop_on_error(self, mock_select): mock_select.return_value = ([], [], [self.in_fd]) self.reader.run() self.assertTrue(self.reader.is_stopped) self.assertEquals(mock_select.call_count, 1) @mock.patch('os.read') @mock.patch('select.select') @mock.patch.object(NailgunProtocol, 'write_chunk') def test_run_read_write(self, mock_writer, mock_select, mock_read): mock_select.side_effect = [([self.in_fd], [], []), ([self.in_fd], [], [])] mock_read.side_effect = [ b'A' * 300, b'' # Simulate EOF. ] # Exercise NailgunStreamReader.running() and .run() simultaneously. with self.reader.running(): while not self.reader.is_stopped: time.sleep(0.01) self.assertTrue(self.reader.is_stopped) mock_read.assert_called_with(-1, io.DEFAULT_BUFFER_SIZE) self.assertEquals(mock_read.call_count, 2) self.mock_socket.shutdown.assert_called_once_with(socket.SHUT_WR) mock_writer.assert_has_calls([ mock.call(mock.ANY, ChunkType.STDIN, b'A' * 300), mock.call(mock.ANY, ChunkType.STDIN_EOF) ])
def _nailgunned_stdio(self, sock): """Redirects stdio to the connected socket speaking the nailgun protocol.""" # Determine output tty capabilities from the environment. _, stdout_isatty, stderr_isatty = NailgunProtocol.isatty_from_env(self._env) # Construct a StreamReader for stdin. stdin_reader = NailgunStreamReader(sys.stdin, sock) # Construct StreamWriters for stdout, stderr. streams = ( NailgunStreamWriter(sock, ChunkType.STDOUT, isatty=stdout_isatty), NailgunStreamWriter(sock, ChunkType.STDERR, isatty=stderr_isatty), stdin_reader ) # Launch the stdin StreamReader and redirect stdio. with stdin_reader.running(), stdio_as(*streams): yield
class TestNailgunStreamReader(unittest.TestCase): def setUp(self): self.in_fd = FakeFile() self.mock_socket = mock.Mock() self.reader = NailgunStreamReader(in_fd=self.in_fd, sock=self.mock_socket) def test_stop(self): self.assertFalse(self.reader.is_stopped) self.reader.stop() self.assertTrue(self.reader.is_stopped) self.reader.run() def test_startable(self): self.assertTrue(inspect.ismethod(self.reader.start)) @mock.patch("select.select") def test_run_stop_on_error(self, mock_select): mock_select.return_value = ([], [], [self.in_fd]) self.reader.run() self.assertTrue(self.reader.is_stopped) self.assertEquals(mock_select.call_count, 1) @mock.patch("os.read") @mock.patch("select.select") @mock.patch.object(NailgunProtocol, "write_chunk") def test_run_read_write(self, mock_writer, mock_select, mock_read): mock_select.side_effect = [([self.in_fd], [], []), ([self.in_fd], [], [])] mock_read.side_effect = [b"A" * 300, b""] # Simulate EOF. # Exercise NailgunStreamReader.running() and .run() simultaneously. with self.reader.running(): while not self.reader.is_stopped: time.sleep(0.01) self.assertTrue(self.reader.is_stopped) mock_read.assert_called_with(-1, io.DEFAULT_BUFFER_SIZE) self.assertEquals(mock_read.call_count, 2) self.mock_socket.shutdown.assert_called_once_with(socket.SHUT_WR) mock_writer.assert_has_calls( [mock.call(mock.ANY, ChunkType.STDIN, b"A" * 300), mock.call(mock.ANY, ChunkType.STDIN_EOF)] )
def _nailgunned_stdio(self, sock): """Redirects stdio to the connected socket speaking the nailgun protocol.""" # Determine output tty capabilities from the environment. _, stdout_isatty, stderr_isatty = NailgunProtocol.isatty_from_env( self._env) # Construct a StreamReader for stdin. stdin_reader = NailgunStreamReader(sys.stdin, sock) # Construct StreamWriters for stdout, stderr. streams = (NailgunStreamWriter(sock, ChunkType.STDOUT, isatty=stdout_isatty), NailgunStreamWriter(sock, ChunkType.STDERR, isatty=stderr_isatty), stdin_reader) # Launch the stdin StreamReader and redirect stdio. with stdin_reader.running(), stdio_as(*streams): yield