From 5b803ba0c3400e0f1a0c3b8e02eebbef72a5cc1e Mon Sep 17 00:00:00 2001 From: fbarbu15 Date: Wed, 15 Nov 2023 16:58:24 +0200 Subject: [PATCH] add more tests --- src/node/api_clients/base_client.py | 6 +++--- src/node/waku_node.py | 1 + src/steps/relay.py | 8 ++------ src/test_data.py | 14 +++++++++++++ tests/relay/test_publish.py | 31 +++++++++++++++++++++++++++-- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/src/node/api_clients/base_client.py b/src/node/api_clients/base_client.py index 3e994749..7992cb03 100644 --- a/src/node/api_clients/base_client.py +++ b/src/node/api_clients/base_client.py @@ -18,13 +18,13 @@ class BaseClient(ABC): try: response.raise_for_status() except requests.HTTPError as http_err: - logger.error("HTTP error occurred: %s", http_err) + logger.error("HTTP error occurred: %s. Response content: %s", http_err, response.content) raise except Exception as err: - logger.error("An error occurred: %s", err) + logger.error("An error occurred: %s. Response content: %s", err, response.content) raise else: - logger.info("Response status code: %s", response.status_code) + logger.info("Response status code: %s. Response content: %s", response.status_code, response.content) return response @abstractmethod diff --git a/src/node/waku_node.py b/src/node/waku_node.py index 1867d01d..c97c02c9 100644 --- a/src/node/waku_node.py +++ b/src/node/waku_node.py @@ -44,6 +44,7 @@ class WakuNode: "rest-admin": "true", "websocket-support": "true", "log-level": "TRACE", + "rest-relay-cache-capacity": "100", "websocket-port": str(self._ports[3]), "rpc-port": self._rpc_port, "rest-port": self._rest_port, diff --git a/src/steps/relay.py b/src/steps/relay.py index a21cec5e..37050e9a 100644 --- a/src/steps/relay.py +++ b/src/steps/relay.py @@ -52,12 +52,8 @@ class StepsRelay: def assert_fail_message(field_name): return f"Incorrect field: {field_name}. Published: {sent_message[field_name]} Received: {getattr(received_message, field_name)}" - assert ( - received_message.payload == sent_message["payload"] - ), f'Incorrect payload. Published {sent_message["payload"]} Received {received_message.payload}' - assert ( - received_message.contentTopic == sent_message["contentTopic"] - ), f'Incorrect contentTopic. Published {sent_message["contentTopic"]} Received {received_message.contentTopic}' + assert received_message.payload == sent_message["payload"], assert_fail_message("payload") + assert received_message.contentTopic == sent_message["contentTopic"], assert_fail_message("contentTopic") if "timestamp" in sent_message and sent_message["timestamp"]: if isinstance(sent_message["timestamp"], float): assert math.isclose(float(received_message.timestamp), sent_message["timestamp"], rel_tol=1e-9), assert_fail_message("timestamp") diff --git a/src/test_data.py b/src/test_data.py index 9cda6ef7..c4c72846 100644 --- a/src/test_data.py +++ b/src/test_data.py @@ -1,6 +1,8 @@ from time import time from datetime import datetime, timedelta +from src.env_vars import DEFAULT_PUBSUBTOPIC + NOW = datetime.now() SAMPLE_INPUTS = [ @@ -63,6 +65,18 @@ INVALID_CONTENT_TOPICS = [ {"description": "A bool", "value": True}, ] +VALID_PUBSUB_TOPICS = [ + DEFAULT_PUBSUBTOPIC, + "/waku/2/rs/18/1", + "/test/2/rs/18/1", + "/waku/3/rs/18/1", + "/waku/2/test/18/1", + "/waku/2/rs/66/1", + "/waku/2/rs/18/50", + "/waku/18/50", + "test", +] + SAMPLE_TIMESTAMPS = [ {"description": "Now", "value": int(time() * 1e9), "valid_for": ["nwaku", "gowaku"]}, diff --git a/tests/relay/test_publish.py b/tests/relay/test_publish.py index 3e0e0f1e..c40012a4 100644 --- a/tests/relay/test_publish.py +++ b/tests/relay/test_publish.py @@ -1,8 +1,8 @@ from src.libs.custom_logger import get_custom_logger from time import time -from src.libs.common import to_base64 +from src.libs.common import delay, to_base64 from src.steps.relay import StepsRelay -from src.test_data import INVALID_CONTENT_TOPICS, INVALID_PAYLOADS, SAMPLE_INPUTS, SAMPLE_TIMESTAMPS +from src.test_data import INVALID_CONTENT_TOPICS, INVALID_PAYLOADS, SAMPLE_INPUTS, SAMPLE_TIMESTAMPS, VALID_PUBSUB_TOPICS logger = get_custom_logger(__name__) @@ -89,6 +89,20 @@ class TestRelayPublish(StepsRelay): except Exception as ex: assert "Bad Request" in str(ex) or "Internal Server Error" in str(ex) + def test_publish_on_multiple_pubsub_topics(self): + self.node1.set_subscriptions(VALID_PUBSUB_TOPICS) + self.node2.set_subscriptions(VALID_PUBSUB_TOPICS) + failed_pubsub_topics = [] + for pubsub_topic in VALID_PUBSUB_TOPICS: + logger.debug("Running test with pubsub topic %s", pubsub_topic) + first_message = {"payload": to_base64("M1"), "contentTopic": self.test_content_topic, "timestamp": int(time() * 1e9)} + try: + self.check_published_message_reaches_peer(first_message, pubsub_topic=pubsub_topic) + except Exception as e: + logger.error("PubusubTopic %s failed: %s", pubsub_topic, str(e)) + failed_pubsub_topics.append(pubsub_topic) + assert not failed_pubsub_topics, f"PubusubTopic failed: {failed_pubsub_topics}" + def test_publish_on_unsubscribed_pubsub_topic(self): try: self.check_published_message_reaches_peer(self.test_message, pubsub_topic="/waku/2/rs/19/1") @@ -206,3 +220,16 @@ class TestRelayPublish(StepsRelay): self.node1.set_subscriptions([self.test_pubsub_topic]) self.node2.set_subscriptions([self.test_pubsub_topic]) self.wait_for_published_message_to_reach_peer(20) + + def test_publish_and_retrieve_100_messages(self): + num_messages = 100 # if increase this number make sure to also increase rest-relay-cache-capacity flag + for index in range(num_messages): + message = {"payload": to_base64(f"M_{index}"), "contentTopic": self.test_content_topic, "timestamp": int(time() * 1e9)} + self.node1.send_message(message, self.test_pubsub_topic) + delay(1) + messages = self.node2.get_messages(self.test_pubsub_topic) + assert len(messages) == num_messages + for index, message in enumerate(messages): + assert message["payload"] == to_base64( + f"M_{index}" + ), f'Incorrect payload at index: {index}. Published {to_base64(f"M_{index}")} Received {message["payload"]}'