Skip to content
Snippets Groups Projects
Commit 17f19999 authored by Jan David Mol's avatar Jan David Mol
Browse files

L2SS-412: Wrap parts of test in async function for cleaner code

parent 63342cf4
Branches
Tags
1 merge request!142L2SS-412: Use asyncio for opcua and other clients
...@@ -65,16 +65,20 @@ class TestOPCua(base.TestCase): ...@@ -65,16 +65,20 @@ class TestOPCua(base.TestCase):
m_opc_client_members.send_hello = asynctest.asynctest.CoroutineMock() m_opc_client_members.send_hello = asynctest.asynctest.CoroutineMock()
m_opc_client.return_value = m_opc_client_members m_opc_client.return_value = m_opc_client_members
async def run_test():
test_client = OPCUAConnection("opc.tcp://localhost:4874/freeopcua/server/", "http://lofar.eu", 5, mock.Mock(), self.event_loop) 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() 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 # this also implies test_client.connect() is called
m_opc_client_members.get_namespace_index.assert_called_once_with("http://lofar.eu") m_opc_client_members.get_namespace_index.assert_called_once_with("http://lofar.eu")
self.assertEqual(42, test_client.name_space_index) 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") @asynctest.patch.object(OPCUAConnection, "ping")
...@@ -99,6 +103,7 @@ class TestOPCua(base.TestCase): ...@@ -99,6 +103,7 @@ class TestOPCua(base.TestCase):
m_opc_client_members.get_objects_node = asynctest.Mock(return_value=m_objects_node) m_opc_client_members.get_objects_node = asynctest.Mock(return_value=m_objects_node)
m_opc_client.return_value = m_opc_client_members m_opc_client.return_value = m_opc_client_members
async def run_test():
for i in attr_test_types: for i in attr_test_types:
class mock_attr: class mock_attr:
def __init__(self, dtype, x, y): def __init__(self, dtype, x, y):
...@@ -121,15 +126,17 @@ class TestOPCua(base.TestCase): ...@@ -121,15 +126,17 @@ class TestOPCua(base.TestCase):
m_annotation = ["2:PCC", f"2:testNode_{str(i.numpy_type)}_{str(dim_x)}_{str(dim_y)}"] m_annotation = ["2:PCC", f"2:testNode_{str(i.numpy_type)}_{str(dim_x)}_{str(dim_y)}"]
test_client = OPCUAConnection("opc.tcp://localhost:4874/freeopcua/server/", "http://lofar.eu", 5, mock.Mock(), self.event_loop) 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() try:
await test_client.start()
asyncio.run_coroutine_threadsafe(test_client.setup_attribute(m_annotation, m_attribute), self.event_loop).result() await test_client.setup_attribute(m_annotation, m_attribute)
finally:
asyncio.run_coroutine_threadsafe(test_client.stop(), self.event_loop).result() await test_client.stop()
# success if there are no errors. # success if there are no errors.
asyncio.run_coroutine_threadsafe(test_client.stop(), self.event_loop).result() await test_client.stop()
asyncio.run_coroutine_threadsafe(run_test(), self.event_loop).result()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment