|
1 | 1 | """Test transaction."""
|
2 | 2 | import asyncio
|
| 3 | +import copy |
3 | 4 | from unittest import mock
|
4 | 5 |
|
5 | 6 | import pytest
|
@@ -182,7 +183,7 @@ async def test_transaction_data_2(self, use_clc, test):
|
182 | 183 | transact.response_future.set_result((1, pdu))
|
183 | 184 | transact.callback_data(packet)
|
184 | 185 |
|
185 |
| - @pytest.mark.parametrize("scenario", range(8)) |
| 186 | + @pytest.mark.parametrize("scenario", range(10)) |
186 | 187 | async def test_transaction_execute(self, use_clc, scenario):
|
187 | 188 | """Test tracers in disconnect."""
|
188 | 189 | transact = TransactionManager(
|
@@ -240,13 +241,33 @@ async def test_transaction_execute(self, use_clc, scenario):
|
240 | 241 | await asyncio.sleep(0.1)
|
241 | 242 | with pytest.raises(ModbusIOException):
|
242 | 243 | await resp
|
243 |
| - else: # if scenario == 7: # response |
| 244 | + elif scenario == 7: # response |
244 | 245 | transact.comm_params.timeout_connect = 0.2
|
245 | 246 | resp = asyncio.create_task(transact.execute(False, request))
|
246 | 247 | await asyncio.sleep(0.1)
|
247 | 248 | transact.response_future.set_result(response)
|
248 | 249 | await asyncio.sleep(0.1)
|
249 | 250 | assert response == await resp
|
| 251 | + elif scenario == 8: # response wrong dev_id |
| 252 | + transact.comm_params.timeout_connect = 0.2 |
| 253 | + resp = asyncio.create_task(transact.execute(False, request)) |
| 254 | + await asyncio.sleep(0.1) |
| 255 | + new_resp = copy.deepcopy(response) |
| 256 | + new_resp.dev_id = 17 |
| 257 | + transact.response_future.set_result(new_resp) |
| 258 | + await asyncio.sleep(0.1) |
| 259 | + with pytest.raises(ModbusIOException): |
| 260 | + resp.result() |
| 261 | + else: # if scenario == 9: # response wrong tid |
| 262 | + transact.comm_params.timeout_connect = 0.2 |
| 263 | + resp = asyncio.create_task(transact.execute(False, request)) |
| 264 | + await asyncio.sleep(0.1) |
| 265 | + new_resp = copy.deepcopy(response) |
| 266 | + new_resp.transaction_id = 17 |
| 267 | + transact.response_future.set_result(new_resp) |
| 268 | + await asyncio.sleep(0.1) |
| 269 | + with pytest.raises(ModbusIOException): |
| 270 | + resp.result() |
250 | 271 |
|
251 | 272 | async def test_transaction_receiver(self, use_clc):
|
252 | 273 | """Test tracers in disconnect."""
|
@@ -423,7 +444,7 @@ def test_sync_transaction_instance(self, use_clc):
|
423 | 444 | )
|
424 | 445 |
|
425 | 446 |
|
426 |
| - @pytest.mark.parametrize("scenario", range(7)) |
| 447 | + @pytest.mark.parametrize("scenario", range(10)) |
427 | 448 | async def test_sync_transaction_execute(self, use_clc, scenario):
|
428 | 449 | """Test tracers in disconnect."""
|
429 | 450 | client = ModbusBaseSyncClient(
|
@@ -479,14 +500,40 @@ async def test_sync_transaction_execute(self, use_clc, scenario):
|
479 | 500 | transact.comm_params.timeout_connect = 0.1
|
480 | 501 | with pytest.raises(ModbusIOException):
|
481 | 502 | transact.sync_execute(False, request)
|
482 |
| - else: # if scenario == 6 # response |
| 503 | + elif scenario == 6: # response |
483 | 504 | transact.transport = 1
|
484 | 505 | resp_bytes = transact.framer.buildFrame(response)
|
485 | 506 | transact.sync_client.recv = mock.Mock(return_value=resp_bytes)
|
486 | 507 | transact.sync_client.send = mock.Mock()
|
487 | 508 | transact.comm_params.timeout_connect = 0.2
|
488 | 509 | resp = transact.sync_execute(False, request)
|
489 | 510 | assert response.bits == resp.bits
|
| 511 | + elif scenario == 7: # response wrong dev_id |
| 512 | + transact.transport = 1 |
| 513 | + pdu = copy.deepcopy(response) |
| 514 | + pdu.dev_id = 17 |
| 515 | + transact.sync_get_response = mock.Mock(return_value=pdu) |
| 516 | + transact.pdu_send = mock.Mock() |
| 517 | + transact.comm_params.timeout_connect = 0.2 |
| 518 | + with pytest.raises(ModbusIOException): |
| 519 | + transact.sync_execute(False, request) |
| 520 | + elif scenario == 8: # response wrong tid |
| 521 | + transact.transport = 1 |
| 522 | + pdu = copy.deepcopy(response) |
| 523 | + pdu.transaction_id = 17 |
| 524 | + transact.sync_get_response = mock.Mock(return_value=pdu) |
| 525 | + transact.pdu_send = mock.Mock() |
| 526 | + transact.comm_params.timeout_connect = 0.2 |
| 527 | + with pytest.raises(ModbusIOException): |
| 528 | + transact.sync_execute(False, request) |
| 529 | + else : # if scenario == 9 # pdu_send from client |
| 530 | + transact.transport = 1 |
| 531 | + transact.is_server = True |
| 532 | + resp_bytes = transact.framer.buildFrame(response) |
| 533 | + transact.sync_client.recv = mock.Mock(return_value=resp_bytes) |
| 534 | + transact.sync_client.send = mock.Mock() |
| 535 | + transact.comm_params.timeout_connect = 0.2 |
| 536 | + transact.pdu_send(response) |
490 | 537 |
|
491 | 538 | def test_sync_transaction_receiver(self, use_clc):
|
492 | 539 | """Test tracers in disconnect."""
|
|
0 commit comments