Пример #1
0
    def setUp(self):
        self.in_fd = FakeFile()
        self.mock_socket = mock.Mock()
        self.mock_writer = mock.Mock()
        self.buf_size = NailgunClientSession.BUF_SIZE

        self.input_reader = InputReader(in_fd=self.in_fd,
                                        sock=self.mock_socket,
                                        chunk_writer=self.mock_writer,
                                        buf_size=self.buf_size)
Пример #2
0
  def setUp(self):
    self.in_fd = FakeFile()
    self.mock_socket = mock.Mock()
    self.mock_writer = mock.Mock()
    self.buf_size = NailgunClientSession.BUF_SIZE

    self.input_reader = InputReader(in_fd=self.in_fd,
                                    sock=self.mock_socket,
                                    chunk_writer=self.mock_writer,
                                    buf_size=self.buf_size)
Пример #3
0
class TestInputReader(unittest.TestCase):
  def setUp(self):
    self.in_fd = FakeFile()
    self.mock_socket = mock.Mock()
    self.mock_writer = mock.Mock()
    self.buf_size = NailgunClientSession.BUF_SIZE

    self.input_reader = InputReader(in_fd=self.in_fd,
                                    sock=self.mock_socket,
                                    chunk_writer=self.mock_writer,
                                    buf_size=self.buf_size)

  def test_stop(self):
    self.assertFalse(self.input_reader.is_stopped)
    self.input_reader.stop()
    self.assertTrue(self.input_reader.is_stopped)
    self.input_reader.run()

  def test_startable(self):
    self.assertTrue(inspect.ismethod(self.input_reader.start))

  @mock.patch('select.select')
  def test_run_stop_on_error(self, mock_select):
    mock_select.return_value = ([], [], [self.in_fd])
    self.input_reader.run()
    self.assertTrue(self.input_reader.is_stopped)
    self.assertEquals(mock_select.call_count, 1)

  @mock.patch('os.read')
  @mock.patch('select.select')
  def test_run_read_write(self, mock_select, mock_read):
    mock_select.side_effect = [
      ([self.in_fd], [], []),
      ([self.in_fd], [], [])
    ]
    mock_read.side_effect = [
      b'A' * 300,
      b''          # Simulate EOF.
    ]

    self.input_reader.run()

    self.assertTrue(self.input_reader.is_stopped)

    mock_read.assert_called_with(-1, self.buf_size)
    self.assertEquals(mock_read.call_count, 2)

    self.mock_socket.shutdown.assert_called_once_with(socket.SHUT_WR)

    self.mock_writer.assert_has_calls([
      mock.call(mock.ANY, ChunkType.STDIN, b'A' * 300),
      mock.call(mock.ANY, ChunkType.STDIN_EOF)
    ])
Пример #4
0
class TestInputReader(unittest.TestCase):
    def setUp(self):
        self.in_fd = FakeFile()
        self.mock_socket = mock.Mock()
        self.mock_writer = mock.Mock()
        self.buf_size = NailgunClientSession.BUF_SIZE

        self.input_reader = InputReader(in_fd=self.in_fd,
                                        sock=self.mock_socket,
                                        chunk_writer=self.mock_writer,
                                        buf_size=self.buf_size)

    def test_stop(self):
        self.assertFalse(self.input_reader.is_stopped)
        self.input_reader.stop()
        self.assertTrue(self.input_reader.is_stopped)
        self.input_reader.run()

    def test_startable(self):
        self.assertTrue(inspect.ismethod(self.input_reader.start))

    @mock.patch('select.select')
    def test_run_stop_on_error(self, mock_select):
        mock_select.return_value = ([], [], [self.in_fd])
        self.input_reader.run()
        self.assertTrue(self.input_reader.is_stopped)
        self.assertEquals(mock_select.call_count, 1)

    @mock.patch('os.read')
    @mock.patch('select.select')
    def test_run_read_write(self, mock_select, mock_read):
        mock_select.side_effect = [([self.in_fd], [], []),
                                   ([self.in_fd], [], [])]
        mock_read.side_effect = [
            b'A' * 300,
            b''  # Simulate EOF.
        ]

        self.input_reader.run()

        self.assertTrue(self.input_reader.is_stopped)

        mock_read.assert_called_with(-1, self.buf_size)
        self.assertEquals(mock_read.call_count, 2)

        self.mock_socket.shutdown.assert_called_once_with(socket.SHUT_WR)

        self.mock_writer.assert_has_calls([
            mock.call(mock.ANY, ChunkType.STDIN, b'A' * 300),
            mock.call(mock.ANY, ChunkType.STDIN_EOF)
        ])