|
| 1 | +"""Test transport.""" |
| 2 | +import os |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +from pymodbus.framer import ModbusFramer |
| 6 | +from pymodbus.transport.transport import BaseTransport |
| 7 | + |
| 8 | + |
| 9 | +class TestBaseTransport: |
| 10 | + """Test transport module, base part.""" |
| 11 | + |
| 12 | + base_comm_name = "test comm" |
| 13 | + base_reconnect_delay = 1 |
| 14 | + base_reconnect_delay_max = 3.5 |
| 15 | + base_timeout_connect = 2 |
| 16 | + base_framer = ModbusFramer |
| 17 | + base_host = "test host" |
| 18 | + base_port = 502 |
| 19 | + base_server_hostname = "server test host" |
| 20 | + base_baudrate = 9600 |
| 21 | + base_bytesize = 8 |
| 22 | + base_parity = "e" |
| 23 | + base_stopbits = 2 |
| 24 | + cwd = None |
| 25 | + |
| 26 | + class dummy_transport(BaseTransport): |
| 27 | + """Transport class for test.""" |
| 28 | + |
| 29 | + def __init__(self): |
| 30 | + """Initialize.""" |
| 31 | + super().__init__( |
| 32 | + TestBaseTransport.base_comm_name, |
| 33 | + [ |
| 34 | + TestBaseTransport.base_reconnect_delay * 1000, |
| 35 | + TestBaseTransport.base_reconnect_delay_max * 1000, |
| 36 | + ], |
| 37 | + TestBaseTransport.base_timeout_connect * 1000, |
| 38 | + TestBaseTransport.base_framer, |
| 39 | + None, |
| 40 | + None, |
| 41 | + None, |
| 42 | + ) |
| 43 | + self.abort = mock.MagicMock() |
| 44 | + self.close = mock.MagicMock() |
| 45 | + |
| 46 | + @classmethod |
| 47 | + async def setup_BaseTransport(cls): |
| 48 | + """Create base object.""" |
| 49 | + base = BaseTransport( |
| 50 | + cls.base_comm_name, |
| 51 | + (cls.base_reconnect_delay * 1000, cls.base_reconnect_delay_max * 1000), |
| 52 | + cls.base_timeout_connect * 1000, |
| 53 | + cls.base_framer, |
| 54 | + mock.MagicMock(), |
| 55 | + mock.MagicMock(), |
| 56 | + mock.MagicMock(), |
| 57 | + ) |
| 58 | + params = base.CommParamsClass( |
| 59 | + done=True, |
| 60 | + comm_name=cls.base_comm_name, |
| 61 | + reconnect_delay=cls.base_reconnect_delay, |
| 62 | + reconnect_delay_max=cls.base_reconnect_delay_max, |
| 63 | + timeout_connect=cls.base_timeout_connect, |
| 64 | + framer=cls.base_framer, |
| 65 | + ) |
| 66 | + cls.cwd = os.getcwd().split("/")[-1] |
| 67 | + if cls.cwd == "transport": |
| 68 | + cls.cwd = "../../" |
| 69 | + elif cls.cwd == "test": |
| 70 | + cls.cwd = "../" |
| 71 | + else: |
| 72 | + cls.cwd = "" |
| 73 | + cls.cwd = cls.cwd + "examples/certificates/pymodbus." |
| 74 | + return base, params |
| 75 | + |
| 76 | + async def test_connection_lost(self): |
| 77 | + """Test connection_lost().""" |
| 78 | + base, params = await self.setup_BaseTransport() |
| 79 | + transport = self.dummy_transport() |
| 80 | + base.connection_lost(transport) |
| 81 | + assert not base.transport |
| 82 | + assert not base.recv_buffer |
| 83 | + assert not base.reconnect_timer |
| 84 | + assert not base.reconnect_delay_current |
| 85 | + base.cb_connection_made.assert_not_called() |
| 86 | + base.cb_handle_data.assert_not_called() |
| 87 | + base.cb_connection_lost.assert_called_once() |
| 88 | + # reconnect is only after a successful connect |
| 89 | + base.connection_made(transport) |
| 90 | + base.connection_lost(transport) |
| 91 | + assert base.reconnect_timer |
| 92 | + assert not base.transport |
| 93 | + assert not base.recv_buffer |
| 94 | + assert base.reconnect_timer |
| 95 | + assert base.reconnect_delay_current == 2 * params.reconnect_delay |
| 96 | + base.cb_connection_lost.call_count == 2 |
| 97 | + base.close() |
| 98 | + assert not base.reconnect_timer |
| 99 | + |
| 100 | + async def test_close(self): |
| 101 | + """Test close().""" |
| 102 | + base, _params = await self.setup_BaseTransport() |
| 103 | + transport = self.dummy_transport() |
| 104 | + base.connection_made(transport) |
| 105 | + base.cb_connection_made.reset_mock() |
| 106 | + base.cb_connection_lost.reset_mock() |
| 107 | + base.cb_handle_data.reset_mock() |
| 108 | + base.recv_buffer = b"abc" |
| 109 | + base.reconnect_timer = mock.MagicMock() |
| 110 | + base.close() |
| 111 | + transport.abort.assert_called_once() |
| 112 | + transport.close.assert_called_once() |
| 113 | + base.cb_connection_made.assert_not_called() |
| 114 | + base.cb_connection_lost.assert_not_called() |
| 115 | + base.cb_handle_data.assert_not_called() |
| 116 | + assert not base.recv_buffer |
| 117 | + assert not base.reconnect_timer |
0 commit comments