From a803fdc388b749021718806e469b2a32d33ae163 Mon Sep 17 00:00:00 2001 From: JesusPoderoso Date: Thu, 23 May 2024 15:00:15 +0200 Subject: [PATCH] Refs #20650: Add example tests Signed-off-by: JesusPoderoso --- test/examples/delivery_mechanisms.compose.yml | 34 ++++++++ test/examples/test_examples.py | 80 +++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 test/examples/delivery_mechanisms.compose.yml diff --git a/test/examples/delivery_mechanisms.compose.yml b/test/examples/delivery_mechanisms.compose.yml new file mode 100644 index 00000000000..16600dd916e --- /dev/null +++ b/test/examples/delivery_mechanisms.compose.yml @@ -0,0 +1,34 @@ +# FASTDDS_TODO_BEFORE(3, 0, "This compose file should be used for the future configuration example"); +version: "3" + +services: + subscriber: + image: @DOCKER_IMAGE_NAME@ + volumes: + - @PROJECT_BINARY_DIR@:@PROJECT_BINARY_DIR@ + - @fastcdr_LIB_DIR@:@fastcdr_LIB_DIR@ + @TINYXML2_LIB_DIR_COMPOSE_VOLUME@ + environment: + # TODO(eduponz): LD_LIBRARY_PATH is not the correct variable for Windows + LD_LIBRARY_PATH: @PROJECT_BINARY_DIR@/src/cpp:@fastcdr_LIB_DIR@@TINYXML2_LIB_DIR_COMPOSE_LD_LIBRARY_PATH@ + EXAMPLE_DIR: @PROJECT_BINARY_DIR@/examples/cpp/delivery_mechanisms + SUBSCRIBER_ADDITIONAL_ARGUMENTS: ${SUB_ARGS} + network_mode: host + ipc: host + command: @SHELL_EXECUTABLE@ -c "$${EXAMPLE_DIR}/delivery_mechanisms@FILE_EXTENSION@ subscriber --samples 10 $${SUBSCRIBER_ADDITIONAL_ARGUMENTS}" + + publisher-subscriber: + image: @DOCKER_IMAGE_NAME@ + volumes: + - @PROJECT_BINARY_DIR@:@PROJECT_BINARY_DIR@ + - @fastcdr_LIB_DIR@:@fastcdr_LIB_DIR@ + @TINYXML2_LIB_DIR_COMPOSE_VOLUME@ + environment: + # TODO(eduponz): LD_LIBRARY_PATH is not the correct variable for Windows + LD_LIBRARY_PATH: @PROJECT_BINARY_DIR@/src/cpp:@fastcdr_LIB_DIR@@TINYXML2_LIB_DIR_COMPOSE_LD_LIBRARY_PATH@ + EXAMPLE_DIR: @PROJECT_BINARY_DIR@/examples/cpp/delivery_mechanisms + PUBLISHER_ADDITIONAL_ARGUMENTS: ${PUB_ARGS} + SUBSCRIBER_ADDITIONAL_ARGUMENTS: ${SUB_ARGS} + network_mode: host + ipc: host + command: @SHELL_EXECUTABLE@ -c "$${EXAMPLE_DIR}/delivery_mechanisms@FILE_EXTENSION@ subscriber --samples 10 $${SUBSCRIBER_ADDITIONAL_ARGUMENTS} & $${EXAMPLE_DIR}/delivery_mechanisms@FILE_EXTENSION@ pubsub --samples 10 $${PUBLISHER_ADDITIONAL_ARGUMENTS}" diff --git a/test/examples/test_examples.py b/test/examples/test_examples.py index 6ed4ebbfaca..ffdb5688c01 100644 --- a/test/examples/test_examples.py +++ b/test/examples/test_examples.py @@ -1,5 +1,6 @@ """.""" import subprocess +import pytest def test_basic_configuration(): """.""" @@ -78,3 +79,82 @@ def test_hello_world(): print(out) assert(ret) + +delivery_test_cases = [ + ('--mechanism shm', '--mechanism shm', 3), + ('--mechanism shm --ignore-local-endpoints', '--mechanism shm', 2), + ('--mechanism udp', '--mechanism udp', 3), + ('--mechanism udp --ignore-local-endpoints', '--mechanism udp', 2), + ('--mechanism tcp', '--mechanism tcp', 3), + ('--mechanism tcp --ignore-local-endpoints', '--mechanism tcp', 2), + ('--mechanism data-sharing', '--mechanism data-sharing', 3), + ('--mechanism data-sharing --ignore-local-endpoints', '--mechanism data-sharing', 2), + ('--mechanism intra-process', '--unknown-argument', 1) # force subscribers to fail +] + +@pytest.mark.parametrize("pub_args, sub_args, repetitions", delivery_test_cases) +def test_delivery_mechanisms(pub_args, sub_args, repetitions): + """.""" + ret = False + out = '' + + command_prerequisites = 'PUB_ARGS="' + pub_args + '" SUB_ARGS="' + sub_args + '" ' + try: + out = subprocess.check_output(command_prerequisites + '@DOCKER_EXECUTABLE@ compose -f delivery_mechanisms.compose.yml up', + stderr=subprocess.STDOUT, + shell=True, + timeout=60 # TCP requires higher timeout + ).decode().split('\n') + + sent = 0 + received = 0 + for line in out: + if 'SENT' in line: + sent += 1 + continue + + if 'RECEIVED' in line: + received += 1 + continue + if sent != 0 and received != 0 and sent * repetitions == received: + ret = True + else: + print('ERROR: sent: ' + str(sent) + ', but received: ' + str(received) + + ' (expected: ' + str(sent * repetitions) + ')') + raise subprocess.CalledProcessError(1, '') + + except subprocess.CalledProcessError: + for l in out: + print(l) + except subprocess.TimeoutExpired: + print('TIMEOUT') + print(out) + +delivery_timeout_test_cases = [ + ('--mechanism shm --ignore-local-endpoints', '--mechanism udp'), + ('--mechanism shm --ignore-local-endpoints', '--mechanism tcp'), + ('--mechanism udp --ignore-local-endpoints', '--mechanism tcp'), + ('--mechanism udp --ignore-local-endpoints', '--mechanism data-sharing'), + ('--mechanism tcp --ignore-local-endpoints', '--mechanism data-sharing'), + ('--mechanism intra-process --ignore-local-endpoints', '--unknown-argument') # force subscribers to fail +] + +@pytest.mark.parametrize("pub_args, sub_args", delivery_timeout_test_cases) +def test_delivery_mechanisms_timeout(pub_args, sub_args): + """.""" + ret = False + out = '' + + command_prerequisites = 'PUB_ARGS="' + pub_args + '" SUB_ARGS="' + sub_args + '" ' + try: + out = subprocess.check_output(command_prerequisites + '@DOCKER_EXECUTABLE@ compose -f delivery_mechanisms.compose.yml up', + stderr=subprocess.STDOUT, + shell=True, + timeout=5 + ) + except subprocess.CalledProcessError as e: + print (e.output) + except subprocess.TimeoutExpired: + ret = True + + assert(ret)