diff --git a/devices/test/clients/test_opcua_client.py b/devices/test/clients/test_opcua_client.py index 5daa68840ad9a0dfcec464e55f60a08a02ce7133..62ddd4a20d804df99cfbfb9a69c166ec322c931b 100644 --- a/devices/test/clients/test_opcua_client.py +++ b/devices/test/clients/test_opcua_client.py @@ -65,16 +65,20 @@ class TestOPCua(base.TestCase): m_opc_client_members.send_hello = asynctest.asynctest.CoroutineMock() m_opc_client.return_value = m_opc_client_members - test_client = OPCUAConnection("opc.tcp://localhost:4874/freeopcua/server/", "http://lofar.eu", 5, mock.Mock(), self.event_loop) - asyncio.run_coroutine_threadsafe(test_client.start(), self.event_loop).result() + async def run_test(): + test_client = OPCUAConnection("opc.tcp://localhost:4874/freeopcua/server/", "http://lofar.eu", 5, mock.Mock(), self.event_loop) + try: + await test_client.start() - m_opc_client.assert_called_once() # makes sure the actual freeOPCua client object is created only once + m_opc_client.assert_called_once() # makes sure the actual freeOPCua client object is created only once - # this also implies test_client.connect() is called - m_opc_client_members.get_namespace_index.assert_called_once_with("http://lofar.eu") - self.assertEqual(42, test_client.name_space_index) + # this also implies test_client.connect() is called + m_opc_client_members.get_namespace_index.assert_called_once_with("http://lofar.eu") + self.assertEqual(42, test_client.name_space_index) + finally: + await test_client.stop() - asyncio.run_coroutine_threadsafe(test_client.stop(), self.event_loop).result() + asyncio.run_coroutine_threadsafe(run_test(), self.event_loop).result() @asynctest.patch.object(OPCUAConnection, "ping") @@ -99,37 +103,40 @@ class TestOPCua(base.TestCase): m_opc_client_members.get_objects_node = asynctest.Mock(return_value=m_objects_node) m_opc_client.return_value = m_opc_client_members - for i in attr_test_types: - class mock_attr: - def __init__(self, dtype, x, y): - self.numpy_type = dtype - self.dim_x = x - self.dim_y = y - - for j in dimension_tests: - if len(j) == 1: - dim_x = j[0] - dim_y = 0 - else: - dim_x = j[1] - dim_y = j[0] - - # create a fake attribute with only the required variables in it. - m_attribute = mock_attr(i.numpy_type, dim_x, dim_y) + async def run_test(): + for i in attr_test_types: + class mock_attr: + def __init__(self, dtype, x, y): + self.numpy_type = dtype + self.dim_x = x + self.dim_y = y + + for j in dimension_tests: + if len(j) == 1: + dim_x = j[0] + dim_y = 0 + else: + dim_x = j[1] + dim_y = j[0] - # pretend like there is a running OPCua server with a node that has this name - m_annotation = ["2:PCC", f"2:testNode_{str(i.numpy_type)}_{str(dim_x)}_{str(dim_y)}"] + # create a fake attribute with only the required variables in it. + m_attribute = mock_attr(i.numpy_type, dim_x, dim_y) - test_client = OPCUAConnection("opc.tcp://localhost:4874/freeopcua/server/", "http://lofar.eu", 5, mock.Mock(), self.event_loop) - asyncio.run_coroutine_threadsafe(test_client.start(), self.event_loop).result() + # pretend like there is a running OPCua server with a node that has this name + m_annotation = ["2:PCC", f"2:testNode_{str(i.numpy_type)}_{str(dim_x)}_{str(dim_y)}"] - asyncio.run_coroutine_threadsafe(test_client.setup_attribute(m_annotation, m_attribute), self.event_loop).result() + test_client = OPCUAConnection("opc.tcp://localhost:4874/freeopcua/server/", "http://lofar.eu", 5, mock.Mock(), self.event_loop) + try: + await test_client.start() + await test_client.setup_attribute(m_annotation, m_attribute) + finally: + await test_client.stop() - asyncio.run_coroutine_threadsafe(test_client.stop(), self.event_loop).result() + # success if there are no errors. - # success if there are no errors. + await test_client.stop() - asyncio.run_coroutine_threadsafe(test_client.stop(), self.event_loop).result() + asyncio.run_coroutine_threadsafe(run_test(), self.event_loop).result()