| Eduardo Sousa | a011781 | 2019-02-05 15:57:09 +0000 | [diff] [blame] | 1 | # Copyright 2018 Whitestack, LLC |
| 2 | # Copyright 2018 Telefonica S.A. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | # |
| 16 | # For those usages not covered by the Apache License, Version 2.0 please |
| 17 | # contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com |
| 18 | ## |
| 19 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 20 | import http |
| 21 | import logging |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 22 | import os |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame^] | 23 | import shutil |
| 24 | import tempfile |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 25 | import threading |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame^] | 26 | import time |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 27 | from unittest.mock import MagicMock |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame^] | 28 | import uuid |
| 29 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 30 | from osm_common.msgbase import MsgException |
| 31 | from osm_common.msglocal import MsgLocal |
| aticig | 3dd0db6 | 2022-03-04 19:35:45 +0300 | [diff] [blame^] | 32 | import pytest |
| 33 | import yaml |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 34 | |
| 35 | __author__ = "Eduardo Sousa <eduardosousa@av.it.pt>" |
| 36 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 37 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 38 | def valid_path(): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 39 | return tempfile.gettempdir() + "/" |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 40 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 41 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 42 | def invalid_path(): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 43 | return "/#tweeter/" |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 44 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 45 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 46 | @pytest.fixture(scope="function", params=[True, False]) |
| 47 | def msg_local(request): |
| 48 | msg = MsgLocal(lock=request.param) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 49 | yield msg |
| 50 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 51 | msg.disconnect() |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 52 | if msg.path and msg.path != invalid_path() and msg.path != valid_path(): |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 53 | shutil.rmtree(msg.path) |
| 54 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 55 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 56 | @pytest.fixture(scope="function", params=[True, False]) |
| 57 | def msg_local_config(request): |
| 58 | msg = MsgLocal(lock=request.param) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 59 | msg.connect({"path": valid_path() + str(uuid.uuid4())}) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 60 | yield msg |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 61 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 62 | msg.disconnect() |
| 63 | if msg.path != invalid_path(): |
| 64 | shutil.rmtree(msg.path) |
| 65 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 66 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 67 | @pytest.fixture(scope="function", params=[True, False]) |
| 68 | def msg_local_with_data(request): |
| 69 | msg = MsgLocal(lock=request.param) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 70 | msg.connect({"path": valid_path() + str(uuid.uuid4())}) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 71 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 72 | msg.write("topic1", "key1", "msg1") |
| 73 | msg.write("topic1", "key2", "msg1") |
| 74 | msg.write("topic2", "key1", "msg1") |
| 75 | msg.write("topic2", "key2", "msg1") |
| 76 | msg.write("topic1", "key1", "msg2") |
| 77 | msg.write("topic1", "key2", "msg2") |
| 78 | msg.write("topic2", "key1", "msg2") |
| 79 | msg.write("topic2", "key2", "msg2") |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 80 | yield msg |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 81 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 82 | msg.disconnect() |
| 83 | if msg.path != invalid_path(): |
| 84 | shutil.rmtree(msg.path) |
| 85 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 86 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 87 | def empty_exception_message(): |
| 88 | return "messaging exception " |
| 89 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 90 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 91 | def test_constructor(): |
| 92 | msg = MsgLocal() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 93 | assert msg.logger == logging.getLogger("msg") |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 94 | assert msg.path is None |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 95 | assert len(msg.files_read) == 0 |
| 96 | assert len(msg.files_write) == 0 |
| 97 | assert len(msg.buffer) == 0 |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 98 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 99 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 100 | def test_constructor_with_logger(): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 101 | logger_name = "msg_local" |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 102 | msg = MsgLocal(logger_name=logger_name) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 103 | assert msg.logger == logging.getLogger(logger_name) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 104 | assert msg.path is None |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 105 | assert len(msg.files_read) == 0 |
| 106 | assert len(msg.files_write) == 0 |
| 107 | assert len(msg.buffer) == 0 |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 108 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 109 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 110 | @pytest.mark.parametrize( |
| 111 | "config, logger_name, path", |
| 112 | [ |
| 113 | ({"logger_name": "msg_local", "path": valid_path()}, "msg_local", valid_path()), |
| 114 | ( |
| 115 | {"logger_name": "msg_local", "path": valid_path()[:-1]}, |
| 116 | "msg_local", |
| 117 | valid_path(), |
| 118 | ), |
| 119 | ( |
| 120 | {"logger_name": "msg_local", "path": valid_path() + "test_it/"}, |
| 121 | "msg_local", |
| 122 | valid_path() + "test_it/", |
| 123 | ), |
| 124 | ( |
| 125 | {"logger_name": "msg_local", "path": valid_path() + "test_it"}, |
| 126 | "msg_local", |
| 127 | valid_path() + "test_it/", |
| 128 | ), |
| 129 | ({"path": valid_path()}, "msg", valid_path()), |
| 130 | ({"path": valid_path()[:-1]}, "msg", valid_path()), |
| 131 | ({"path": valid_path() + "test_it/"}, "msg", valid_path() + "test_it/"), |
| 132 | ({"path": valid_path() + "test_it"}, "msg", valid_path() + "test_it/"), |
| 133 | ], |
| 134 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 135 | def test_connect(msg_local, config, logger_name, path): |
| 136 | msg_local.connect(config) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 137 | assert msg_local.logger == logging.getLogger(logger_name) |
| 138 | assert msg_local.path == path |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 139 | assert len(msg_local.files_read) == 0 |
| 140 | assert len(msg_local.files_write) == 0 |
| 141 | assert len(msg_local.buffer) == 0 |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 142 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 143 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 144 | @pytest.mark.parametrize( |
| 145 | "config", |
| 146 | [ |
| 147 | ({"logger_name": "msg_local", "path": invalid_path()}), |
| 148 | ({"path": invalid_path()}), |
| 149 | ], |
| 150 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 151 | def test_connect_with_exception(msg_local, config): |
| 152 | with pytest.raises(MsgException) as excinfo: |
| 153 | msg_local.connect(config) |
| 154 | assert str(excinfo.value).startswith(empty_exception_message()) |
| 155 | assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR |
| 156 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 157 | |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 158 | def test_disconnect(msg_local_config): |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 159 | files_read = msg_local_config.files_read.copy() |
| 160 | files_write = msg_local_config.files_write.copy() |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 161 | msg_local_config.disconnect() |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 162 | for f in files_read.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 163 | assert f.closed |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 164 | for f in files_write.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 165 | assert f.closed |
| 166 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 167 | |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 168 | def test_disconnect_with_read(msg_local_config): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 169 | msg_local_config.read("topic1", blocks=False) |
| 170 | msg_local_config.read("topic2", blocks=False) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 171 | files_read = msg_local_config.files_read.copy() |
| 172 | files_write = msg_local_config.files_write.copy() |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 173 | msg_local_config.disconnect() |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 174 | for f in files_read.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 175 | assert f.closed |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 176 | for f in files_write.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 177 | assert f.closed |
| 178 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 179 | |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 180 | def test_disconnect_with_write(msg_local_with_data): |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 181 | files_read = msg_local_with_data.files_read.copy() |
| 182 | files_write = msg_local_with_data.files_write.copy() |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 183 | msg_local_with_data.disconnect() |
| 184 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 185 | for f in files_read.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 186 | assert f.closed |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 187 | |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 188 | for f in files_write.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 189 | assert f.closed |
| 190 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 191 | |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 192 | def test_disconnect_with_read_and_write(msg_local_with_data): |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 193 | msg_local_with_data.read("topic1", blocks=False) |
| 194 | msg_local_with_data.read("topic2", blocks=False) |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 195 | files_read = msg_local_with_data.files_read.copy() |
| 196 | files_write = msg_local_with_data.files_write.copy() |
| 197 | |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 198 | msg_local_with_data.disconnect() |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 199 | for f in files_read.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 200 | assert f.closed |
| tierno | 1e9a329 | 2018-11-05 18:18:45 +0100 | [diff] [blame] | 201 | for f in files_write.values(): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 202 | assert f.closed |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 203 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 204 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 205 | @pytest.mark.parametrize( |
| 206 | "topic, key, msg", |
| 207 | [ |
| 208 | ("test_topic", "test_key", "test_msg"), |
| 209 | ("test", "test_key", "test_msg"), |
| 210 | ("test_topic", "test", "test_msg"), |
| 211 | ("test_topic", "test_key", "test"), |
| 212 | ("test_topic", "test_list", ["a", "b", "c"]), |
| 213 | ("test_topic", "test_tuple", ("c", "b", "a")), |
| 214 | ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}), |
| 215 | ("test_topic", "test_number", 123), |
| 216 | ("test_topic", "test_float", 1.23), |
| 217 | ("test_topic", "test_boolean", True), |
| 218 | ("test_topic", "test_none", None), |
| 219 | ], |
| 220 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 221 | def test_write(msg_local_config, topic, key, msg): |
| 222 | file_path = msg_local_config.path + topic |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 223 | msg_local_config.write(topic, key, msg) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 224 | assert os.path.exists(file_path) |
| 225 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 226 | with open(file_path, "r") as stream: |
| 227 | assert yaml.safe_load(stream) == { |
| 228 | key: msg if not isinstance(msg, tuple) else list(msg) |
| 229 | } |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 230 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 231 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 232 | @pytest.mark.parametrize( |
| 233 | "topic, key, msg, times", |
| 234 | [ |
| 235 | ("test_topic", "test_key", "test_msg", 2), |
| 236 | ("test", "test_key", "test_msg", 3), |
| 237 | ("test_topic", "test", "test_msg", 4), |
| 238 | ("test_topic", "test_key", "test", 2), |
| 239 | ("test_topic", "test_list", ["a", "b", "c"], 3), |
| 240 | ("test_topic", "test_tuple", ("c", "b", "a"), 4), |
| 241 | ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2), |
| 242 | ("test_topic", "test_number", 123, 3), |
| 243 | ("test_topic", "test_float", 1.23, 4), |
| 244 | ("test_topic", "test_boolean", True, 2), |
| 245 | ("test_topic", "test_none", None, 3), |
| 246 | ], |
| 247 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 248 | def test_write_with_multiple_calls(msg_local_config, topic, key, msg, times): |
| 249 | file_path = msg_local_config.path + topic |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 250 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 251 | for _ in range(times): |
| 252 | msg_local_config.write(topic, key, msg) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 253 | assert os.path.exists(file_path) |
| 254 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 255 | with open(file_path, "r") as stream: |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 256 | for _ in range(times): |
| 257 | data = stream.readline() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 258 | assert yaml.safe_load(data) == { |
| 259 | key: msg if not isinstance(msg, tuple) else list(msg) |
| 260 | } |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 261 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 262 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 263 | def test_write_exception(msg_local_config): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 264 | msg_local_config.files_write = MagicMock() |
| 265 | msg_local_config.files_write.__contains__.side_effect = Exception() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 266 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 267 | with pytest.raises(MsgException) as excinfo: |
| 268 | msg_local_config.write("test", "test", "test") |
| 269 | assert str(excinfo.value).startswith(empty_exception_message()) |
| 270 | assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR |
| 271 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 272 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 273 | @pytest.mark.parametrize( |
| 274 | "topics, datas", |
| 275 | [ |
| 276 | (["topic"], [{"key": "value"}]), |
| 277 | (["topic1"], [{"key": "value"}]), |
| 278 | (["topic2"], [{"key": "value"}]), |
| 279 | (["topic", "topic1"], [{"key": "value"}]), |
| 280 | (["topic", "topic2"], [{"key": "value"}]), |
| 281 | (["topic1", "topic2"], [{"key": "value"}]), |
| 282 | (["topic", "topic1", "topic2"], [{"key": "value"}]), |
| 283 | (["topic"], [{"key": "value"}, {"key1": "value1"}]), |
| 284 | (["topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 285 | (["topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 286 | (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 287 | (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 288 | (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 289 | (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 290 | ], |
| 291 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 292 | def test_read(msg_local_with_data, topics, datas): |
| 293 | def write_to_topic(topics, datas): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 294 | # Allow msglocal to block while waiting |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 295 | time.sleep(2) |
| 296 | for topic in topics: |
| 297 | for data in datas: |
| 298 | with open(msg_local_with_data.path + topic, "a+") as fp: |
| 299 | yaml.safe_dump(data, fp, default_flow_style=True, width=20000) |
| 300 | fp.flush() |
| 301 | |
| 302 | # If file is not opened first, the messages written won't be seen |
| 303 | for topic in topics: |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 304 | if topic not in msg_local_with_data.files_read: |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 305 | msg_local_with_data.read(topic, blocks=False) |
| 306 | |
| 307 | t = threading.Thread(target=write_to_topic, args=(topics, datas)) |
| 308 | t.start() |
| 309 | |
| 310 | for topic in topics: |
| 311 | for data in datas: |
| 312 | recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 313 | key = list(data.keys())[0] |
| 314 | val = data[key] |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 315 | assert recv_topic == topic |
| 316 | assert recv_key == key |
| 317 | assert recv_msg == val |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 318 | t.join() |
| 319 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 320 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 321 | @pytest.mark.parametrize( |
| 322 | "topics, datas", |
| 323 | [ |
| 324 | (["topic"], [{"key": "value"}]), |
| 325 | (["topic1"], [{"key": "value"}]), |
| 326 | (["topic2"], [{"key": "value"}]), |
| 327 | (["topic", "topic1"], [{"key": "value"}]), |
| 328 | (["topic", "topic2"], [{"key": "value"}]), |
| 329 | (["topic1", "topic2"], [{"key": "value"}]), |
| 330 | (["topic", "topic1", "topic2"], [{"key": "value"}]), |
| 331 | (["topic"], [{"key": "value"}, {"key1": "value1"}]), |
| 332 | (["topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 333 | (["topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 334 | (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 335 | (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 336 | (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 337 | (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 338 | ], |
| 339 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 340 | def test_read_non_block(msg_local_with_data, topics, datas): |
| 341 | def write_to_topic(topics, datas): |
| 342 | for topic in topics: |
| 343 | for data in datas: |
| 344 | with open(msg_local_with_data.path + topic, "a+") as fp: |
| 345 | yaml.safe_dump(data, fp, default_flow_style=True, width=20000) |
| 346 | fp.flush() |
| 347 | |
| 348 | # If file is not opened first, the messages written won't be seen |
| 349 | for topic in topics: |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 350 | if topic not in msg_local_with_data.files_read: |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 351 | msg_local_with_data.read(topic, blocks=False) |
| 352 | |
| 353 | t = threading.Thread(target=write_to_topic, args=(topics, datas)) |
| 354 | t.start() |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 355 | t.join() |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 356 | |
| 357 | for topic in topics: |
| 358 | for data in datas: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 359 | recv_topic, recv_key, recv_msg = msg_local_with_data.read( |
| 360 | topic, blocks=False |
| 361 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 362 | key = list(data.keys())[0] |
| 363 | val = data[key] |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 364 | assert recv_topic == topic |
| 365 | assert recv_key == key |
| 366 | assert recv_msg == val |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 367 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 368 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 369 | @pytest.mark.parametrize( |
| 370 | "topics, datas", |
| 371 | [ |
| 372 | (["topic"], [{"key": "value"}]), |
| 373 | (["topic1"], [{"key": "value"}]), |
| 374 | (["topic2"], [{"key": "value"}]), |
| 375 | (["topic", "topic1"], [{"key": "value"}]), |
| 376 | (["topic", "topic2"], [{"key": "value"}]), |
| 377 | (["topic1", "topic2"], [{"key": "value"}]), |
| 378 | (["topic", "topic1", "topic2"], [{"key": "value"}]), |
| 379 | (["topic"], [{"key": "value"}, {"key1": "value1"}]), |
| 380 | (["topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 381 | (["topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 382 | (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 383 | (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 384 | (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 385 | (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 386 | ], |
| 387 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 388 | def test_read_non_block_none(msg_local_with_data, topics, datas): |
| 389 | def write_to_topic(topics, datas): |
| 390 | time.sleep(2) |
| 391 | for topic in topics: |
| 392 | for data in datas: |
| 393 | with open(msg_local_with_data.path + topic, "a+") as fp: |
| 394 | yaml.safe_dump(data, fp, default_flow_style=True, width=20000) |
| 395 | fp.flush() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 396 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 397 | # If file is not opened first, the messages written won't be seen |
| 398 | for topic in topics: |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 399 | if topic not in msg_local_with_data.files_read: |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 400 | msg_local_with_data.read(topic, blocks=False) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 401 | t = threading.Thread(target=write_to_topic, args=(topics, datas)) |
| 402 | t.start() |
| 403 | |
| 404 | for topic in topics: |
| 405 | recv_data = msg_local_with_data.read(topic, blocks=False) |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 406 | assert recv_data is None |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 407 | t.join() |
| 408 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 409 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 410 | @pytest.mark.parametrize("blocks", [(True), (False)]) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 411 | def test_read_exception(msg_local_with_data, blocks): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 412 | msg_local_with_data.files_read = MagicMock() |
| 413 | msg_local_with_data.files_read.__contains__.side_effect = Exception() |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 414 | |
| 415 | with pytest.raises(MsgException) as excinfo: |
| 416 | msg_local_with_data.read("topic1", blocks=blocks) |
| 417 | assert str(excinfo.value).startswith(empty_exception_message()) |
| 418 | assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR |
| 419 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 420 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 421 | @pytest.mark.parametrize( |
| 422 | "topics, datas", |
| 423 | [ |
| 424 | (["topic"], [{"key": "value"}]), |
| 425 | (["topic1"], [{"key": "value"}]), |
| 426 | (["topic2"], [{"key": "value"}]), |
| 427 | (["topic", "topic1"], [{"key": "value"}]), |
| 428 | (["topic", "topic2"], [{"key": "value"}]), |
| 429 | (["topic1", "topic2"], [{"key": "value"}]), |
| 430 | (["topic", "topic1", "topic2"], [{"key": "value"}]), |
| 431 | (["topic"], [{"key": "value"}, {"key1": "value1"}]), |
| 432 | (["topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 433 | (["topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 434 | (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]), |
| 435 | (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 436 | (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 437 | (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]), |
| 438 | ], |
| 439 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 440 | def test_aioread(msg_local_with_data, event_loop, topics, datas): |
| 441 | def write_to_topic(topics, datas): |
| 442 | time.sleep(2) |
| 443 | for topic in topics: |
| 444 | for data in datas: |
| 445 | with open(msg_local_with_data.path + topic, "a+") as fp: |
| 446 | yaml.safe_dump(data, fp, default_flow_style=True, width=20000) |
| 447 | fp.flush() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 448 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 449 | # If file is not opened first, the messages written won't be seen |
| 450 | for topic in topics: |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 451 | if topic not in msg_local_with_data.files_read: |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 452 | msg_local_with_data.read(topic, blocks=False) |
| 453 | |
| 454 | t = threading.Thread(target=write_to_topic, args=(topics, datas)) |
| 455 | t.start() |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 456 | for topic in topics: |
| 457 | for data in datas: |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 458 | recv = event_loop.run_until_complete( |
| 459 | msg_local_with_data.aioread(topic, event_loop) |
| 460 | ) |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 461 | recv_topic, recv_key, recv_msg = recv |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 462 | key = list(data.keys())[0] |
| 463 | val = data[key] |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 464 | assert recv_topic == topic |
| 465 | assert recv_key == key |
| 466 | assert recv_msg == val |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 467 | t.join() |
| 468 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 469 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 470 | def test_aioread_exception(msg_local_with_data, event_loop): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 471 | msg_local_with_data.files_read = MagicMock() |
| 472 | msg_local_with_data.files_read.__contains__.side_effect = Exception() |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 473 | |
| 474 | with pytest.raises(MsgException) as excinfo: |
| 475 | event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop)) |
| 476 | assert str(excinfo.value).startswith(empty_exception_message()) |
| 477 | assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR |
| 478 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 479 | |
| Eduardo Sousa | acbbdf2 | 2018-05-03 15:47:41 +0100 | [diff] [blame] | 480 | def test_aioread_general_exception(msg_local_with_data, event_loop): |
| 481 | msg_local_with_data.read = MagicMock() |
| 482 | msg_local_with_data.read.side_effect = Exception() |
| 483 | |
| 484 | with pytest.raises(MsgException) as excinfo: |
| 485 | event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop)) |
| 486 | assert str(excinfo.value).startswith(empty_exception_message()) |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 487 | assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR |
| 488 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 489 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 490 | @pytest.mark.parametrize( |
| 491 | "topic, key, msg", |
| 492 | [ |
| 493 | ("test_topic", "test_key", "test_msg"), |
| 494 | ("test", "test_key", "test_msg"), |
| 495 | ("test_topic", "test", "test_msg"), |
| 496 | ("test_topic", "test_key", "test"), |
| 497 | ("test_topic", "test_list", ["a", "b", "c"]), |
| 498 | ("test_topic", "test_tuple", ("c", "b", "a")), |
| 499 | ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}), |
| 500 | ("test_topic", "test_number", 123), |
| 501 | ("test_topic", "test_float", 1.23), |
| 502 | ("test_topic", "test_boolean", True), |
| 503 | ("test_topic", "test_none", None), |
| 504 | ], |
| 505 | ) |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 506 | def test_aiowrite(msg_local_config, event_loop, topic, key, msg): |
| 507 | file_path = msg_local_config.path + topic |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 508 | event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg)) |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 509 | assert os.path.exists(file_path) |
| 510 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 511 | with open(file_path, "r") as stream: |
| 512 | assert yaml.safe_load(stream) == { |
| 513 | key: msg if not isinstance(msg, tuple) else list(msg) |
| 514 | } |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 515 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 516 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 517 | @pytest.mark.parametrize( |
| 518 | "topic, key, msg, times", |
| 519 | [ |
| 520 | ("test_topic", "test_key", "test_msg", 2), |
| 521 | ("test", "test_key", "test_msg", 3), |
| 522 | ("test_topic", "test", "test_msg", 4), |
| 523 | ("test_topic", "test_key", "test", 2), |
| 524 | ("test_topic", "test_list", ["a", "b", "c"], 3), |
| 525 | ("test_topic", "test_tuple", ("c", "b", "a"), 4), |
| 526 | ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2), |
| 527 | ("test_topic", "test_number", 123, 3), |
| 528 | ("test_topic", "test_float", 1.23, 4), |
| 529 | ("test_topic", "test_boolean", True, 2), |
| 530 | ("test_topic", "test_none", None, 3), |
| 531 | ], |
| 532 | ) |
| 533 | def test_aiowrite_with_multiple_calls( |
| 534 | msg_local_config, event_loop, topic, key, msg, times |
| 535 | ): |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 536 | file_path = msg_local_config.path + topic |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 537 | for _ in range(times): |
| 538 | event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg)) |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 539 | assert os.path.exists(file_path) |
| 540 | |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 541 | with open(file_path, "r") as stream: |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 542 | for _ in range(times): |
| 543 | data = stream.readline() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 544 | assert yaml.safe_load(data) == { |
| 545 | key: msg if not isinstance(msg, tuple) else list(msg) |
| 546 | } |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 547 | |
| tierno | b20a902 | 2018-05-22 12:07:05 +0200 | [diff] [blame] | 548 | |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 549 | def test_aiowrite_exception(msg_local_config, event_loop): |
| 550 | msg_local_config.files_write = MagicMock() |
| 551 | msg_local_config.files_write.__contains__.side_effect = Exception() |
| garciadeblas | 2644b76 | 2021-03-24 09:21:01 +0100 | [diff] [blame] | 552 | |
| Eduardo Sousa | a7f8a6d | 2018-05-09 13:57:22 +0100 | [diff] [blame] | 553 | with pytest.raises(MsgException) as excinfo: |
| 554 | event_loop.run_until_complete(msg_local_config.aiowrite("test", "test", "test")) |
| 555 | assert str(excinfo.value).startswith(empty_exception_message()) |
| 556 | assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR |