|
| 1 | +import asyncio |
| 2 | +import pytest |
| 3 | +import sys |
| 4 | + |
| 5 | + |
| 6 | +IDL_FILE = 'test_idlc_hierarchy.idl' |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def hierarchy_library(): |
| 11 | + ''' |
| 12 | + Generates the Hierarchy library from the test_idlc_hierarchy.idl file. |
| 13 | + ''' |
| 14 | + import os |
| 15 | + import shutil |
| 16 | + import subprocess |
| 17 | + assert 'CYCLONEDDS_HOME' in os.environ, 'CYCLONEDDS_HOME is not set' |
| 18 | + idlc_path = os.path.join(os.environ['CYCLONEDDS_HOME'], 'bin', 'idlc') |
| 19 | + subprocess.run([idlc_path, '-l', 'py', IDL_FILE]) |
| 20 | + yield |
| 21 | + shutil.rmtree('Hierarchy') |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.asyncio |
| 25 | +async def test_base_topic(hierarchy_library): |
| 26 | + ''' |
| 27 | + Creates a publisher and a subscriber of the Base topic and checks if the |
| 28 | + subscriber receives the update sent by the publisher. |
| 29 | + ''' |
| 30 | + from Hierarchy import Base |
| 31 | + base = Base(fieldA="Hakuna") |
| 32 | + tasks = [ |
| 33 | + _subscriber(Base), |
| 34 | + _publisher(Base, base), |
| 35 | + ] |
| 36 | + results = await asyncio.gather(*tasks) |
| 37 | + assert results[0] == results[1] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.asyncio |
| 41 | +async def test_derived_topic(hierarchy_library): |
| 42 | + ''' |
| 43 | + Creates a publisher and a subscriber of the Derived topic and checks if the |
| 44 | + subscriber receives the update sent by the publisher. |
| 45 | + ''' |
| 46 | + from Hierarchy import Derived |
| 47 | + derived = Derived( |
| 48 | + fieldA="Hakuna", |
| 49 | + fieldB="Matata", |
| 50 | + ) |
| 51 | + tasks = [ |
| 52 | + _subscriber(Derived), |
| 53 | + _publisher(Derived, derived), |
| 54 | + ] |
| 55 | + results = await asyncio.gather(*tasks) |
| 56 | + assert results[0] == results[1] |
| 57 | + |
| 58 | + |
| 59 | +@pytest.mark.asyncio |
| 60 | +@pytest.mark.skipif(sys.platform == 'darwin', reason='I do not know why this fails on macOS') |
| 61 | +async def test_cyclonedds_typeof_command(hierarchy_library): |
| 62 | + ''' |
| 63 | + Executes the command "cyclonedds typeof" and compares the result with the |
| 64 | + IDL file. |
| 65 | + ''' |
| 66 | + from Hierarchy import Derived |
| 67 | + tasks = [ |
| 68 | + _subscriber(Derived), |
| 69 | + _type_checker(Derived, IDL_FILE), |
| 70 | + ] |
| 71 | + await asyncio.gather(*tasks) |
| 72 | + |
| 73 | + |
| 74 | +async def _publisher(topic_class, value, timeout=2): |
| 75 | + ''' |
| 76 | + Sends a given value update to the subscriber. |
| 77 | + ''' |
| 78 | + from cyclonedds.domain import DomainParticipant |
| 79 | + from cyclonedds.topic import Topic |
| 80 | + from cyclonedds.pub import DataWriter |
| 81 | + |
| 82 | + participant = DomainParticipant(0) |
| 83 | + topic = Topic(participant, topic_class.__name__, topic_class) |
| 84 | + writer = DataWriter(participant, topic) |
| 85 | + writer.write(value) |
| 86 | + await asyncio.sleep(timeout) |
| 87 | + return value |
| 88 | + |
| 89 | + |
| 90 | +async def _subscriber(topic_class, timeout=2): |
| 91 | + ''' |
| 92 | + Receives an update. Returns None if it times out. |
| 93 | + ''' |
| 94 | + from cyclonedds.domain import DomainParticipant |
| 95 | + from cyclonedds.topic import Topic |
| 96 | + from cyclonedds.sub import DataReader |
| 97 | + from cyclonedds.util import duration |
| 98 | + |
| 99 | + participant = DomainParticipant(0) |
| 100 | + topic = Topic(participant, topic_class.__name__, topic_class) |
| 101 | + reader = DataReader(participant, topic) |
| 102 | + async for update in reader.read_aiter(timeout=duration(seconds=timeout)): |
| 103 | + return update |
| 104 | + return None |
| 105 | + |
| 106 | + |
| 107 | +async def _type_checker(topic_class, idl_file): |
| 108 | + ''' |
| 109 | + Executes the command "cyclonedds typeof" and compares the result with a |
| 110 | + given IDL file. |
| 111 | + ''' |
| 112 | + def _normalise(text): |
| 113 | + text = text.replace('\n', ' ') |
| 114 | + text = ' '.join(text.split()) |
| 115 | + return text |
| 116 | + |
| 117 | + # loads the IDL file |
| 118 | + with open(idl_file) as file: |
| 119 | + expected_idl = file.read().rstrip() |
| 120 | + |
| 121 | + # executes the command |
| 122 | + import subprocess |
| 123 | + result = subprocess.run( |
| 124 | + ['cyclonedds', 'typeof', topic_class.__name__, '--suppress-progress-bar'], |
| 125 | + stdout=subprocess.PIPE, |
| 126 | + check=True,) |
| 127 | + command_output = result.stdout.decode().splitlines() |
| 128 | + |
| 129 | + # skips the first lines of the output (As defined in participant...) |
| 130 | + first_idl_line = 0 |
| 131 | + while first_idl_line < len(command_output) and not command_output[first_idl_line].startswith('module'): |
| 132 | + first_idl_line += 1 |
| 133 | + actual_idl = '\n'.join(command_output[first_idl_line:]) |
| 134 | + |
| 135 | + # compares the command output with the IDL file |
| 136 | + print(actual_idl) |
| 137 | + assert _normalise(actual_idl) == _normalise(expected_idl) |
0 commit comments