2013-10-17 17:40:50 -03:00
|
|
|
"""Tests for transports.py."""
|
|
|
|
|
|
|
|
import unittest
|
2014-02-26 05:25:02 -04:00
|
|
|
from unittest import mock
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2014-01-25 10:32:06 -04:00
|
|
|
import asyncio
|
2014-02-19 12:10:52 -04:00
|
|
|
from asyncio import transports
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
|
|
|
|
class TransportTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_ctor_extra_is_none(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
transport = asyncio.Transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual(transport._extra, {})
|
|
|
|
|
|
|
|
def test_get_extra_info(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
transport = asyncio.Transport({'extra': 'info'})
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertEqual('info', transport.get_extra_info('extra'))
|
|
|
|
self.assertIsNone(transport.get_extra_info('unknown'))
|
|
|
|
|
|
|
|
default = object()
|
|
|
|
self.assertIs(default, transport.get_extra_info('unknown', default))
|
|
|
|
|
|
|
|
def test_writelines(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
transport = asyncio.Transport()
|
2014-02-26 05:25:02 -04:00
|
|
|
transport.write = mock.Mock()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-12-02 22:36:30 -04:00
|
|
|
transport.writelines([b'line1',
|
|
|
|
bytearray(b'line2'),
|
|
|
|
memoryview(b'line3')])
|
|
|
|
self.assertEqual(1, transport.write.call_count)
|
|
|
|
transport.write.assert_called_with(b'line1line2line3')
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
def test_not_implemented(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
transport = asyncio.Transport()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
2013-12-02 22:36:30 -04:00
|
|
|
self.assertRaises(NotImplementedError,
|
|
|
|
transport.set_write_buffer_limits)
|
|
|
|
self.assertRaises(NotImplementedError, transport.get_write_buffer_size)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(NotImplementedError, transport.write, 'data')
|
|
|
|
self.assertRaises(NotImplementedError, transport.write_eof)
|
|
|
|
self.assertRaises(NotImplementedError, transport.can_write_eof)
|
2013-10-18 11:58:20 -03:00
|
|
|
self.assertRaises(NotImplementedError, transport.pause_reading)
|
|
|
|
self.assertRaises(NotImplementedError, transport.resume_reading)
|
2017-12-19 02:51:16 -04:00
|
|
|
self.assertRaises(NotImplementedError, transport.is_reading)
|
2013-10-17 17:40:50 -03:00
|
|
|
self.assertRaises(NotImplementedError, transport.close)
|
|
|
|
self.assertRaises(NotImplementedError, transport.abort)
|
|
|
|
|
|
|
|
def test_dgram_not_implemented(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
transport = asyncio.DatagramTransport()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertRaises(NotImplementedError, transport.sendto, 'data')
|
|
|
|
self.assertRaises(NotImplementedError, transport.abort)
|
|
|
|
|
|
|
|
def test_subprocess_transport_not_implemented(self):
|
2014-01-25 10:32:06 -04:00
|
|
|
transport = asyncio.SubprocessTransport()
|
2013-10-17 17:40:50 -03:00
|
|
|
|
|
|
|
self.assertRaises(NotImplementedError, transport.get_pid)
|
|
|
|
self.assertRaises(NotImplementedError, transport.get_returncode)
|
|
|
|
self.assertRaises(NotImplementedError, transport.get_pipe_transport, 1)
|
|
|
|
self.assertRaises(NotImplementedError, transport.send_signal, 1)
|
|
|
|
self.assertRaises(NotImplementedError, transport.terminate)
|
|
|
|
self.assertRaises(NotImplementedError, transport.kill)
|
2013-10-20 16:02:53 -03:00
|
|
|
|
2014-02-19 12:10:52 -04:00
|
|
|
def test_flowcontrol_mixin_set_write_limits(self):
|
|
|
|
|
|
|
|
class MyTransport(transports._FlowControlMixin,
|
|
|
|
transports.Transport):
|
|
|
|
|
|
|
|
def get_write_buffer_size(self):
|
|
|
|
return 512
|
|
|
|
|
2014-11-05 10:27:41 -04:00
|
|
|
loop = mock.Mock()
|
|
|
|
transport = MyTransport(loop=loop)
|
2014-02-26 05:25:02 -04:00
|
|
|
transport._protocol = mock.Mock()
|
2014-02-19 12:10:52 -04:00
|
|
|
|
|
|
|
self.assertFalse(transport._protocol_paused)
|
|
|
|
|
|
|
|
with self.assertRaisesRegex(ValueError, 'high.*must be >= low'):
|
|
|
|
transport.set_write_buffer_limits(high=0, low=1)
|
|
|
|
|
|
|
|
transport.set_write_buffer_limits(high=1024, low=128)
|
|
|
|
self.assertFalse(transport._protocol_paused)
|
2014-08-25 19:22:28 -03:00
|
|
|
self.assertEqual(transport.get_write_buffer_limits(), (128, 1024))
|
2014-02-19 12:10:52 -04:00
|
|
|
|
|
|
|
transport.set_write_buffer_limits(high=256, low=128)
|
|
|
|
self.assertTrue(transport._protocol_paused)
|
2014-08-25 19:22:28 -03:00
|
|
|
self.assertEqual(transport.get_write_buffer_limits(), (128, 256))
|
2014-02-19 12:10:52 -04:00
|
|
|
|
2013-10-20 16:02:53 -03:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|