Ejemplo n.º 1
0
 def test_write_more(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._write_fut = unittest.mock.Mock()
     tr._loop_writing = unittest.mock.Mock()
     tr.write(b'data')
     self.assertEqual(tr._buffer, [b'data'])
     self.assertFalse(tr._loop_writing.called)
Ejemplo n.º 2
0
    def test_fatal_error_2(self):
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._buffer = [b'data']
        tr._force_close(None)

        self.loop.call_soon.assert_called_with(tr._call_connection_lost, None)
        self.assertEqual([], tr._buffer)
Ejemplo n.º 3
0
 def test_pause_resume(self):
     tr = _ProactorSocketTransport(
         self.loop, self.sock, self.protocol)
     futures = []
     for msg in [b'data1', b'data2', b'data3', b'data4', b'']:
         f = tulip.Future(loop=self.loop)
         f.set_result(msg)
         futures.append(f)
     self.loop._proactor.recv.side_effect = futures
     self.loop._run_once()
     self.assertFalse(tr._paused)
     self.loop._run_once()
     self.protocol.data_received.assert_called_with(b'data1')
     self.loop._run_once()
     self.protocol.data_received.assert_called_with(b'data2')
     tr.pause()
     self.assertTrue(tr._paused)
     for i in range(10):
         self.loop._run_once()
     self.protocol.data_received.assert_called_with(b'data2')
     tr.resume()
     self.assertFalse(tr._paused)
     self.loop._run_once()
     self.protocol.data_received.assert_called_with(b'data3')
     self.loop._run_once()
     self.protocol.data_received.assert_called_with(b'data4')
     tr.close()
Ejemplo n.º 4
0
 def test_loop_writing(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._buffer = [b'da', b'ta']
     tr._loop_writing()
     self.loop._proactor.send.assert_called_with(self.sock, b'data')
     self.loop._proactor.send.return_value.add_done_callback.\
         assert_called_with(tr._loop_writing)
Ejemplo n.º 5
0
    def test_loop_reading_aborted(self):
        err = self.loop._proactor.recv.side_effect = ConnectionAbortedError()

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._fatal_error = unittest.mock.Mock()
        tr._loop_reading()
        tr._fatal_error.assert_called_with(err)
Ejemplo n.º 6
0
 def test_loop_reading_aborted_is_fatal(self):
     self.loop._proactor.recv.side_effect = ConnectionAbortedError()
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._closing = False
     tr._fatal_error = unittest.mock.Mock()
     tr._loop_reading()
     self.assertTrue(tr._fatal_error.called)
Ejemplo n.º 7
0
    def test_loop_writing_stop(self):
        fut = tulip.Future(loop=self.loop)
        fut.set_result(b'data')

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._write_fut = fut
        tr._loop_writing(fut)
        self.assertIsNone(tr._write_fut)
Ejemplo n.º 8
0
 def test_ctor(self):
     fut = tulip.Future(loop=self.loop)
     tr = _ProactorSocketTransport(
         self.loop, self.sock, self.protocol, fut)
     test_utils.run_briefly(self.loop)
     self.assertIsNone(fut.result())
     self.protocol.connection_made(tr)
     self.proactor.recv.assert_called_with(self.sock, 4096)
Ejemplo n.º 9
0
    def test_loop_reading_aborted_closing(self):
        self.loop._proactor.recv.side_effect = ConnectionAbortedError()

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._closing = True
        tr._fatal_error = unittest.mock.Mock()
        tr._loop_reading()
        self.assertFalse(tr._fatal_error.called)
Ejemplo n.º 10
0
    def test_fatal_error_2(self):
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._buffer = [b'data']
        tr._force_close(None)

        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertEqual([], tr._buffer)
Ejemplo n.º 11
0
 def test_write_eof(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     self.assertTrue(tr.can_write_eof())
     tr.write_eof()
     self.sock.shutdown.assert_called_with(socket.SHUT_WR)
     tr.write_eof()
     self.assertEqual(self.sock.shutdown.call_count, 1)
     tr.close()
Ejemplo n.º 12
0
    def test_fatal_error_2(self):
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._buffer = [b'data']
        tr._force_close(None)

        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertEqual([], tr._buffer)
Ejemplo n.º 13
0
    def test_loop_writing_stop(self):
        fut = tulip.Future(loop=self.loop)
        fut.set_result(b'data')

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._write_fut = fut
        tr._loop_writing(fut)
        self.assertIsNone(tr._write_fut)
Ejemplo n.º 14
0
 def test_write_eof(self):
     tr = _ProactorSocketTransport(
         self.loop, self.sock, self.protocol)
     self.assertTrue(tr.can_write_eof())
     tr.write_eof()
     self.sock.shutdown.assert_called_with(socket.SHUT_WR)
     tr.write_eof()
     self.assertEqual(self.sock.shutdown.call_count, 1)
     tr.close()
Ejemplo n.º 15
0
 def test_ctor(self):
     fut = tulip.Future(loop=self.loop)
     tr = _ProactorSocketTransport(
         self.loop, self.sock, self.protocol, fut)
     self.loop.call_soon.mock_calls[0].assert_called_with(tr._loop_reading)
     self.loop.call_soon.mock_calls[1].assert_called_with(
         self.protocol.connection_made, tr)
     self.loop.call_soon.mock_calls[2].assert_called_with(
         fut.set_result, None)
Ejemplo n.º 16
0
    def test_loop_reading_conn_reset_lost(self):
        err = self.loop._proactor.recv.side_effect = ConnectionResetError()

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._closing = False
        tr._fatal_error = unittest.mock.Mock()
        tr._force_close = unittest.mock.Mock()
        tr._loop_reading()
        self.assertFalse(tr._fatal_error.called)
        tr._force_close.assert_called_with(err)
Ejemplo n.º 17
0
    def test_loop_reading_conn_reset_lost(self):
        err = self.loop._proactor.recv.side_effect = ConnectionResetError()

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._closing = False
        tr._fatal_error = unittest.mock.Mock()
        tr._force_close = unittest.mock.Mock()
        tr._loop_reading()
        self.assertFalse(tr._fatal_error.called)
        tr._force_close.assert_called_with(err)
Ejemplo n.º 18
0
    def test_loop_reading_data(self):
        res = tulip.Future(loop=self.loop)
        res.set_result(b'data')

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)

        tr._read_fut = res
        tr._loop_reading(res)
        self.loop._proactor.recv.assert_called_with(self.sock, 4096)
        self.protocol.data_received.assert_called_with(b'data')
Ejemplo n.º 19
0
    def test_loop_reading_data(self):
        res = tulip.Future(loop=self.loop)
        res.set_result(b'data')

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)

        tr._read_fut = res
        tr._loop_reading(res)
        self.loop._proactor.recv.assert_called_with(self.sock, 4096)
        self.protocol.data_received.assert_called_with(b'data')
Ejemplo n.º 20
0
    def test_loop_writing_closing(self):
        fut = tulip.Future(loop=self.loop)
        fut.set_result(1)

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._write_fut = fut
        tr.close()
        tr._loop_writing(fut)
        self.assertIsNone(tr._write_fut)
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
Ejemplo n.º 21
0
    def test_loop_writing_closing(self):
        fut = tulip.Future(loop=self.loop)
        fut.set_result(1)

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._write_fut = fut
        tr.close()
        tr._loop_writing(fut)
        self.assertIsNone(tr._write_fut)
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
Ejemplo n.º 22
0
    def test_loop_writing_closing(self):
        fut = tulip.Future(loop=self.loop)
        fut.set_result(1)

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        self.loop.reset_mock()
        tr._write_fut = fut
        tr.close()
        tr._loop_writing(fut)
        self.assertIsNone(tr._write_fut)
        self.loop.call_soon.assert_called_with(tr._call_connection_lost, None)
Ejemplo n.º 23
0
    def test_close(self):
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        self.loop.reset_mock()
        tr.close()
        self.loop.call_soon.assert_called_with(tr._call_connection_lost, None)
        self.assertTrue(tr._closing)
        self.assertEqual(tr._conn_lost, 1)

        self.loop.reset_mock()
        tr.close()
        self.assertFalse(self.loop.call_soon.called)
Ejemplo n.º 24
0
    def test_close(self):
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr.close()
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertTrue(tr._closing)
        self.assertEqual(tr._conn_lost, 1)

        self.protocol.connection_lost.reset_mock()
        tr.close()
        test_utils.run_briefly(self.loop)
        self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 25
0
    def test_force_close(self):
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._buffer = [b'data']
        read_fut = tr._read_fut = unittest.mock.Mock()
        write_fut = tr._write_fut = unittest.mock.Mock()
        tr._force_close(None)

        read_fut.cancel.assert_called_with()
        write_fut.cancel.assert_called_with()
        self.loop.call_soon.assert_called_with(tr._call_connection_lost, None)
        self.assertEqual([], tr._buffer)
        self.assertEqual(tr._conn_lost, 1)
Ejemplo n.º 26
0
    def test_close(self):
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr.close()
        test_utils.run_briefly(self.loop)
        self.protocol.connection_lost.assert_called_with(None)
        self.assertTrue(tr._closing)
        self.assertEqual(tr._conn_lost, 1)

        self.protocol.connection_lost.reset_mock()
        tr.close()
        test_utils.run_briefly(self.loop)
        self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 27
0
 def test_write_eof_buffer(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     f = tulip.Future(loop=self.loop)
     tr._loop._proactor.send.return_value = f
     tr.write(b'data')
     tr.write_eof()
     self.assertTrue(tr._eof_written)
     self.assertFalse(self.sock.shutdown.called)
     tr._loop._proactor.send.assert_called_with(self.sock, b'data')
     f.set_result(4)
     self.loop._run_once()
     self.sock.shutdown.assert_called_with(socket.SHUT_WR)
     tr.close()
Ejemplo n.º 28
0
 def test_write_eof_buffer(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     f = tulip.Future(loop=self.loop)
     tr._loop._proactor.send.return_value = f
     tr.write(b'data')
     tr.write_eof()
     self.assertTrue(tr._eof_written)
     self.assertFalse(self.sock.shutdown.called)
     tr._loop._proactor.send.assert_called_with(self.sock, b'data')
     f.set_result(4)
     self.loop._run_once()
     self.sock.shutdown.assert_called_with(socket.SHUT_WR)
     tr.close()
Ejemplo n.º 29
0
    def test_loop_reading_no_data(self):
        res = tulip.Future(loop=self.loop)
        res.set_result(b'')

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)

        self.assertRaises(AssertionError, tr._loop_reading, res)

        tr.close = unittest.mock.Mock()
        tr._read_fut = res
        tr._loop_reading(res)
        self.assertFalse(self.loop._proactor.recv.called)
        self.assertTrue(self.protocol.eof_received.called)
        self.assertTrue(tr.close.called)
Ejemplo n.º 30
0
    def test_loop_reading_no_data(self):
        res = tulip.Future(loop=self.loop)
        res.set_result(b'')

        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)

        self.assertRaises(AssertionError, tr._loop_reading, res)

        tr.close = unittest.mock.Mock()
        tr._read_fut = res
        tr._loop_reading(res)
        self.assertFalse(self.loop._proactor.recv.called)
        self.assertTrue(self.protocol.eof_received.called)
        self.assertTrue(tr.close.called)
Ejemplo n.º 31
0
    def test_loop_writing_err(self, m_log):
        err = self.loop._proactor.send.side_effect = OSError()
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._fatal_error = unittest.mock.Mock()
        tr._buffer = [b'da', b'ta']
        tr._loop_writing()
        tr._fatal_error.assert_called_with(err)
        tr._conn_lost = 1

        tr.write(b'data')
        tr.write(b'data')
        tr.write(b'data')
        tr.write(b'data')
        tr.write(b'data')
        self.assertEqual(tr._buffer, [])
        m_log.warning.assert_called_with('socket.send() raised exception.')
Ejemplo n.º 32
0
    def test_loop_writing_err(self, m_log):
        err = self.loop._proactor.send.side_effect = OSError()
        tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
        tr._fatal_error = unittest.mock.Mock()
        tr._buffer = [b'da', b'ta']
        tr._loop_writing()
        tr._fatal_error.assert_called_with(err)
        tr._conn_lost = 1

        tr.write(b'data')
        tr.write(b'data')
        tr.write(b'data')
        tr.write(b'data')
        tr.write(b'data')
        self.assertEqual(tr._buffer, [])
        m_log.warning.assert_called_with('socket.send() raised exception.')
Ejemplo n.º 33
0
 def test_force_close_idempotent(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._closing = True
     tr._force_close(None)
     test_utils.run_briefly(self.loop)
     self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 34
0
 def test_call_connection_lost(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._call_connection_lost(None)
     self.assertTrue(self.protocol.connection_lost.called)
     self.assertTrue(self.sock.close.called)
Ejemplo n.º 35
0
 def test_abort(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._force_close = unittest.mock.Mock()
     tr.abort()
     tr._force_close.assert_called_with(None)
Ejemplo n.º 36
0
 def test_close_buffer(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._buffer = [b'data']
     self.loop.reset_mock()
     tr.close()
     self.assertFalse(self.loop.call_soon.called)
Ejemplo n.º 37
0
 def test_loop_reading(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._loop_reading()
     self.loop._proactor.recv.assert_called_with(self.sock, 4096)
     self.assertFalse(self.protocol.data_received.called)
     self.assertFalse(self.protocol.eof_received.called)
Ejemplo n.º 38
0
 def test_close_write_fut(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._write_fut = unittest.mock.Mock()
     tr.close()
     test_utils.run_briefly(self.loop)
     self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 39
0
 def test_call_connection_lost(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._call_connection_lost(None)
     self.assertTrue(self.protocol.connection_lost.called)
     self.assertTrue(self.sock.close.called)
Ejemplo n.º 40
0
 def test_close_write_fut(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._write_fut = unittest.mock.Mock()
     self.loop.reset_mock()
     tr.close()
     self.assertFalse(self.loop.call_soon.called)
Ejemplo n.º 41
0
 def test_loop_reading(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._loop_reading()
     self.loop._proactor.recv.assert_called_with(self.sock, 4096)
     self.assertFalse(self.protocol.data_received.called)
     self.assertFalse(self.protocol.eof_received.called)
Ejemplo n.º 42
0
 def test_write_no_data(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr.write(b'')
     self.assertFalse(tr._buffer)
Ejemplo n.º 43
0
 def test_force_close_idempotent(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._closing = True
     self.loop.reset_mock()
     tr._force_close(None)
     self.assertFalse(self.loop.call_soon.called)
Ejemplo n.º 44
0
 def test_close_buffer(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._buffer = [b'data']
     tr.close()
     test_utils.run_briefly(self.loop)
     self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 45
0
 def test_close_buffer(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._buffer = [b'data']
     tr.close()
     test_utils.run_briefly(self.loop)
     self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 46
0
 def test_fatal_error(self, m_logging):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._force_close = unittest.mock.Mock()
     tr._fatal_error(None)
     self.assertTrue(tr._force_close.called)
     self.assertTrue(m_logging.exception.called)
Ejemplo n.º 47
0
 def test_fatal_error(self, m_logging):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._force_close = unittest.mock.Mock()
     tr._fatal_error(None)
     self.assertTrue(tr._force_close.called)
     self.assertTrue(m_logging.exception.called)
Ejemplo n.º 48
0
 def test_write(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._loop_writing = unittest.mock.Mock()
     tr.write(b'data')
     self.assertEqual(tr._buffer, [b'data'])
     self.assertTrue(tr._loop_writing.called)
Ejemplo n.º 49
0
 def test_close_write_fut(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._write_fut = unittest.mock.Mock()
     tr.close()
     test_utils.run_briefly(self.loop)
     self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 50
0
 def test_force_close_idempotent(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._closing = True
     tr._force_close(None)
     test_utils.run_briefly(self.loop)
     self.assertFalse(self.protocol.connection_lost.called)
Ejemplo n.º 51
0
 def test_abort(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr._force_close = unittest.mock.Mock()
     tr.abort()
     tr._force_close.assert_called_with(None)
Ejemplo n.º 52
0
 def test_write_no_data(self):
     tr = _ProactorSocketTransport(self.loop, self.sock, self.protocol)
     tr.write(b'')
     self.assertFalse(tr._buffer)