blob: b40b75c1e5a120217a101bebcbc10e7a611a2195 [file] [log] [blame]
Eduardo Sousaa0117812019-02-05 15:57:09 +00001# 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##
Gulsum Aticia06b8542023-05-09 13:42:13 +030019import asyncio
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010020import http
21import logging
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010022import os
aticig3dd0db62022-03-04 19:35:45 +030023import shutil
24import tempfile
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010025import threading
aticig3dd0db62022-03-04 19:35:45 +030026import time
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010027from unittest.mock import MagicMock
aticig3dd0db62022-03-04 19:35:45 +030028import uuid
29
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010030from osm_common.msgbase import MsgException
31from osm_common.msglocal import MsgLocal
aticig3dd0db62022-03-04 19:35:45 +030032import pytest
33import yaml
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010034
35__author__ = "Eduardo Sousa <eduardosousa@av.it.pt>"
36
tiernob20a9022018-05-22 12:07:05 +020037
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010038def valid_path():
garciadeblas2644b762021-03-24 09:21:01 +010039 return tempfile.gettempdir() + "/"
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010040
tiernob20a9022018-05-22 12:07:05 +020041
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010042def invalid_path():
garciadeblas2644b762021-03-24 09:21:01 +010043 return "/#tweeter/"
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010044
tiernob20a9022018-05-22 12:07:05 +020045
tierno1e9a3292018-11-05 18:18:45 +010046@pytest.fixture(scope="function", params=[True, False])
47def msg_local(request):
48 msg = MsgLocal(lock=request.param)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010049 yield msg
50
tierno1e9a3292018-11-05 18:18:45 +010051 msg.disconnect()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010052 if msg.path and msg.path != invalid_path() and msg.path != valid_path():
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010053 shutil.rmtree(msg.path)
54
tiernob20a9022018-05-22 12:07:05 +020055
tierno1e9a3292018-11-05 18:18:45 +010056@pytest.fixture(scope="function", params=[True, False])
57def msg_local_config(request):
58 msg = MsgLocal(lock=request.param)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010059 msg.connect({"path": valid_path() + str(uuid.uuid4())})
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010060 yield msg
tiernob20a9022018-05-22 12:07:05 +020061
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010062 msg.disconnect()
63 if msg.path != invalid_path():
64 shutil.rmtree(msg.path)
65
tiernob20a9022018-05-22 12:07:05 +020066
tierno1e9a3292018-11-05 18:18:45 +010067@pytest.fixture(scope="function", params=[True, False])
68def msg_local_with_data(request):
69 msg = MsgLocal(lock=request.param)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010070 msg.connect({"path": valid_path() + str(uuid.uuid4())})
tiernob20a9022018-05-22 12:07:05 +020071
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010072 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 Sousaacbbdf22018-05-03 15:47:41 +010080 yield msg
tiernob20a9022018-05-22 12:07:05 +020081
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010082 msg.disconnect()
83 if msg.path != invalid_path():
84 shutil.rmtree(msg.path)
85
tiernob20a9022018-05-22 12:07:05 +020086
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010087def empty_exception_message():
88 return "messaging exception "
89
tiernob20a9022018-05-22 12:07:05 +020090
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010091def test_constructor():
92 msg = MsgLocal()
garciadeblas2644b762021-03-24 09:21:01 +010093 assert msg.logger == logging.getLogger("msg")
tiernob20a9022018-05-22 12:07:05 +020094 assert msg.path is None
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +010095 assert len(msg.files_read) == 0
96 assert len(msg.files_write) == 0
97 assert len(msg.buffer) == 0
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010098
tiernob20a9022018-05-22 12:07:05 +020099
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100100def test_constructor_with_logger():
garciadeblas2644b762021-03-24 09:21:01 +0100101 logger_name = "msg_local"
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100102 msg = MsgLocal(logger_name=logger_name)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100103 assert msg.logger == logging.getLogger(logger_name)
tiernob20a9022018-05-22 12:07:05 +0200104 assert msg.path is None
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100105 assert len(msg.files_read) == 0
106 assert len(msg.files_write) == 0
107 assert len(msg.buffer) == 0
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100108
tiernob20a9022018-05-22 12:07:05 +0200109
garciadeblas2644b762021-03-24 09:21:01 +0100110@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 Sousaacbbdf22018-05-03 15:47:41 +0100135def test_connect(msg_local, config, logger_name, path):
136 msg_local.connect(config)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100137 assert msg_local.logger == logging.getLogger(logger_name)
138 assert msg_local.path == path
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100139 assert len(msg_local.files_read) == 0
140 assert len(msg_local.files_write) == 0
141 assert len(msg_local.buffer) == 0
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100142
tiernob20a9022018-05-22 12:07:05 +0200143
garciadeblas2644b762021-03-24 09:21:01 +0100144@pytest.mark.parametrize(
145 "config",
146 [
147 ({"logger_name": "msg_local", "path": invalid_path()}),
148 ({"path": invalid_path()}),
149 ],
150)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100151def 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
tiernob20a9022018-05-22 12:07:05 +0200157
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100158def test_disconnect(msg_local_config):
tierno1e9a3292018-11-05 18:18:45 +0100159 files_read = msg_local_config.files_read.copy()
160 files_write = msg_local_config.files_write.copy()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100161 msg_local_config.disconnect()
tierno1e9a3292018-11-05 18:18:45 +0100162 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100163 assert f.closed
tierno1e9a3292018-11-05 18:18:45 +0100164 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100165 assert f.closed
166
tiernob20a9022018-05-22 12:07:05 +0200167
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100168def test_disconnect_with_read(msg_local_config):
garciadeblas2644b762021-03-24 09:21:01 +0100169 msg_local_config.read("topic1", blocks=False)
170 msg_local_config.read("topic2", blocks=False)
tierno1e9a3292018-11-05 18:18:45 +0100171 files_read = msg_local_config.files_read.copy()
172 files_write = msg_local_config.files_write.copy()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100173 msg_local_config.disconnect()
tierno1e9a3292018-11-05 18:18:45 +0100174 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100175 assert f.closed
tierno1e9a3292018-11-05 18:18:45 +0100176 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100177 assert f.closed
178
tiernob20a9022018-05-22 12:07:05 +0200179
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100180def test_disconnect_with_write(msg_local_with_data):
tierno1e9a3292018-11-05 18:18:45 +0100181 files_read = msg_local_with_data.files_read.copy()
182 files_write = msg_local_with_data.files_write.copy()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100183 msg_local_with_data.disconnect()
184
tierno1e9a3292018-11-05 18:18:45 +0100185 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100186 assert f.closed
garciadeblas2644b762021-03-24 09:21:01 +0100187
tierno1e9a3292018-11-05 18:18:45 +0100188 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100189 assert f.closed
190
tiernob20a9022018-05-22 12:07:05 +0200191
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100192def test_disconnect_with_read_and_write(msg_local_with_data):
garciadeblas2644b762021-03-24 09:21:01 +0100193 msg_local_with_data.read("topic1", blocks=False)
194 msg_local_with_data.read("topic2", blocks=False)
tierno1e9a3292018-11-05 18:18:45 +0100195 files_read = msg_local_with_data.files_read.copy()
196 files_write = msg_local_with_data.files_write.copy()
197
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100198 msg_local_with_data.disconnect()
tierno1e9a3292018-11-05 18:18:45 +0100199 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100200 assert f.closed
tierno1e9a3292018-11-05 18:18:45 +0100201 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100202 assert f.closed
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100203
tiernob20a9022018-05-22 12:07:05 +0200204
garciadeblas2644b762021-03-24 09:21:01 +0100205@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 Sousaacbbdf22018-05-03 15:47:41 +0100221def test_write(msg_local_config, topic, key, msg):
222 file_path = msg_local_config.path + topic
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100223 msg_local_config.write(topic, key, msg)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100224 assert os.path.exists(file_path)
225
garciadeblas2644b762021-03-24 09:21:01 +0100226 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 Sousaacbbdf22018-05-03 15:47:41 +0100230
tiernob20a9022018-05-22 12:07:05 +0200231
garciadeblas2644b762021-03-24 09:21:01 +0100232@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 Sousaacbbdf22018-05-03 15:47:41 +0100248def test_write_with_multiple_calls(msg_local_config, topic, key, msg, times):
249 file_path = msg_local_config.path + topic
garciadeblas2644b762021-03-24 09:21:01 +0100250
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100251 for _ in range(times):
252 msg_local_config.write(topic, key, msg)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100253 assert os.path.exists(file_path)
254
garciadeblas2644b762021-03-24 09:21:01 +0100255 with open(file_path, "r") as stream:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100256 for _ in range(times):
257 data = stream.readline()
garciadeblas2644b762021-03-24 09:21:01 +0100258 assert yaml.safe_load(data) == {
259 key: msg if not isinstance(msg, tuple) else list(msg)
260 }
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100261
tiernob20a9022018-05-22 12:07:05 +0200262
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100263def test_write_exception(msg_local_config):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100264 msg_local_config.files_write = MagicMock()
265 msg_local_config.files_write.__contains__.side_effect = Exception()
garciadeblas2644b762021-03-24 09:21:01 +0100266
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100267 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
tiernob20a9022018-05-22 12:07:05 +0200272
garciadeblas2644b762021-03-24 09:21:01 +0100273@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 Sousaacbbdf22018-05-03 15:47:41 +0100292def test_read(msg_local_with_data, topics, datas):
293 def write_to_topic(topics, datas):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100294 # Allow msglocal to block while waiting
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100295 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 Sousaa7f8a6d2018-05-09 13:57:22 +0100304 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100305 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 Sousaacbbdf22018-05-03 15:47:41 +0100313 key = list(data.keys())[0]
314 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100315 assert recv_topic == topic
316 assert recv_key == key
317 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100318 t.join()
319
tiernob20a9022018-05-22 12:07:05 +0200320
garciadeblas2644b762021-03-24 09:21:01 +0100321@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 Sousaacbbdf22018-05-03 15:47:41 +0100340def 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 Sousaa7f8a6d2018-05-09 13:57:22 +0100350 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100351 msg_local_with_data.read(topic, blocks=False)
352
353 t = threading.Thread(target=write_to_topic, args=(topics, datas))
354 t.start()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100355 t.join()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100356
357 for topic in topics:
358 for data in datas:
garciadeblas2644b762021-03-24 09:21:01 +0100359 recv_topic, recv_key, recv_msg = msg_local_with_data.read(
360 topic, blocks=False
361 )
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100362 key = list(data.keys())[0]
363 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100364 assert recv_topic == topic
365 assert recv_key == key
366 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100367
tiernob20a9022018-05-22 12:07:05 +0200368
garciadeblas2644b762021-03-24 09:21:01 +0100369@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 Sousaacbbdf22018-05-03 15:47:41 +0100388def 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()
garciadeblas2644b762021-03-24 09:21:01 +0100396
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100397 # If file is not opened first, the messages written won't be seen
398 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100399 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100400 msg_local_with_data.read(topic, blocks=False)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100401 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)
tiernob20a9022018-05-22 12:07:05 +0200406 assert recv_data is None
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100407 t.join()
408
tiernob20a9022018-05-22 12:07:05 +0200409
garciadeblas2644b762021-03-24 09:21:01 +0100410@pytest.mark.parametrize("blocks", [(True), (False)])
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100411def test_read_exception(msg_local_with_data, blocks):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100412 msg_local_with_data.files_read = MagicMock()
413 msg_local_with_data.files_read.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100414
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
tiernob20a9022018-05-22 12:07:05 +0200420
garciadeblas2644b762021-03-24 09:21:01 +0100421@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)
Gulsum Aticia06b8542023-05-09 13:42:13 +0300440def test_aioread(msg_local_with_data, topics, datas):
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100441 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()
garciadeblas2644b762021-03-24 09:21:01 +0100448
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100449 # If file is not opened first, the messages written won't be seen
450 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100451 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100452 msg_local_with_data.read(topic, blocks=False)
453
454 t = threading.Thread(target=write_to_topic, args=(topics, datas))
455 t.start()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100456 for topic in topics:
457 for data in datas:
Gulsum Aticia06b8542023-05-09 13:42:13 +0300458 recv = asyncio.run(msg_local_with_data.aioread(topic))
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100459 recv_topic, recv_key, recv_msg = recv
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100460 key = list(data.keys())[0]
461 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100462 assert recv_topic == topic
463 assert recv_key == key
464 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100465 t.join()
466
tiernob20a9022018-05-22 12:07:05 +0200467
Gulsum Aticia06b8542023-05-09 13:42:13 +0300468def test_aioread_exception(msg_local_with_data):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100469 msg_local_with_data.files_read = MagicMock()
470 msg_local_with_data.files_read.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100471
472 with pytest.raises(MsgException) as excinfo:
Gulsum Aticia06b8542023-05-09 13:42:13 +0300473 asyncio.run(msg_local_with_data.aioread("topic1"))
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100474 assert str(excinfo.value).startswith(empty_exception_message())
475 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
476
tiernob20a9022018-05-22 12:07:05 +0200477
Gulsum Aticia06b8542023-05-09 13:42:13 +0300478def test_aioread_general_exception(msg_local_with_data):
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100479 msg_local_with_data.read = MagicMock()
480 msg_local_with_data.read.side_effect = Exception()
481
482 with pytest.raises(MsgException) as excinfo:
Gulsum Aticia06b8542023-05-09 13:42:13 +0300483 asyncio.run(msg_local_with_data.aioread("topic1"))
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100484 assert str(excinfo.value).startswith(empty_exception_message())
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100485 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
486
tiernob20a9022018-05-22 12:07:05 +0200487
garciadeblas2644b762021-03-24 09:21:01 +0100488@pytest.mark.parametrize(
489 "topic, key, msg",
490 [
491 ("test_topic", "test_key", "test_msg"),
492 ("test", "test_key", "test_msg"),
493 ("test_topic", "test", "test_msg"),
494 ("test_topic", "test_key", "test"),
495 ("test_topic", "test_list", ["a", "b", "c"]),
496 ("test_topic", "test_tuple", ("c", "b", "a")),
497 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}),
498 ("test_topic", "test_number", 123),
499 ("test_topic", "test_float", 1.23),
500 ("test_topic", "test_boolean", True),
501 ("test_topic", "test_none", None),
502 ],
503)
Gulsum Aticia06b8542023-05-09 13:42:13 +0300504def test_aiowrite(msg_local_config, topic, key, msg):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100505 file_path = msg_local_config.path + topic
Gulsum Aticia06b8542023-05-09 13:42:13 +0300506 asyncio.run(msg_local_config.aiowrite(topic, key, msg))
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100507 assert os.path.exists(file_path)
508
garciadeblas2644b762021-03-24 09:21:01 +0100509 with open(file_path, "r") as stream:
510 assert yaml.safe_load(stream) == {
511 key: msg if not isinstance(msg, tuple) else list(msg)
512 }
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100513
tiernob20a9022018-05-22 12:07:05 +0200514
garciadeblas2644b762021-03-24 09:21:01 +0100515@pytest.mark.parametrize(
516 "topic, key, msg, times",
517 [
518 ("test_topic", "test_key", "test_msg", 2),
519 ("test", "test_key", "test_msg", 3),
520 ("test_topic", "test", "test_msg", 4),
521 ("test_topic", "test_key", "test", 2),
522 ("test_topic", "test_list", ["a", "b", "c"], 3),
523 ("test_topic", "test_tuple", ("c", "b", "a"), 4),
524 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2),
525 ("test_topic", "test_number", 123, 3),
526 ("test_topic", "test_float", 1.23, 4),
527 ("test_topic", "test_boolean", True, 2),
528 ("test_topic", "test_none", None, 3),
529 ],
530)
Gulsum Aticia06b8542023-05-09 13:42:13 +0300531def test_aiowrite_with_multiple_calls(msg_local_config, topic, key, msg, times):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100532 file_path = msg_local_config.path + topic
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100533 for _ in range(times):
Gulsum Aticia06b8542023-05-09 13:42:13 +0300534 asyncio.run(msg_local_config.aiowrite(topic, key, msg))
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100535 assert os.path.exists(file_path)
536
garciadeblas2644b762021-03-24 09:21:01 +0100537 with open(file_path, "r") as stream:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100538 for _ in range(times):
539 data = stream.readline()
garciadeblas2644b762021-03-24 09:21:01 +0100540 assert yaml.safe_load(data) == {
541 key: msg if not isinstance(msg, tuple) else list(msg)
542 }
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100543
tiernob20a9022018-05-22 12:07:05 +0200544
Gulsum Aticia06b8542023-05-09 13:42:13 +0300545def test_aiowrite_exception(msg_local_config):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100546 msg_local_config.files_write = MagicMock()
547 msg_local_config.files_write.__contains__.side_effect = Exception()
garciadeblas2644b762021-03-24 09:21:01 +0100548
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100549 with pytest.raises(MsgException) as excinfo:
Gulsum Aticia06b8542023-05-09 13:42:13 +0300550 asyncio.run(msg_local_config.aiowrite("test", "test", "test"))
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100551 assert str(excinfo.value).startswith(empty_exception_message())
552 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR