blob: f2b63ccccf814f92bc536b978c2879bfc7d3d89f [file] [log] [blame]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +01001import http
2import logging
3import pytest
4import tempfile
5import shutil
6import uuid
7import os
8import yaml
9import time
10import threading
11
12from unittest.mock import MagicMock
13from osm_common.msgbase import MsgException
14from osm_common.msglocal import MsgLocal
15
16__author__ = "Eduardo Sousa <eduardosousa@av.it.pt>"
17
tiernob20a9022018-05-22 12:07:05 +020018
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010019def valid_path():
20 return tempfile.gettempdir() + '/'
21
tiernob20a9022018-05-22 12:07:05 +020022
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010023def invalid_path():
24 return '/#tweeter/'
25
tiernob20a9022018-05-22 12:07:05 +020026
tierno1e9a3292018-11-05 18:18:45 +010027@pytest.fixture(scope="function", params=[True, False])
28def msg_local(request):
29 msg = MsgLocal(lock=request.param)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010030 yield msg
31
tierno1e9a3292018-11-05 18:18:45 +010032 msg.disconnect()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010033 if msg.path and msg.path != invalid_path() and msg.path != valid_path():
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010034 shutil.rmtree(msg.path)
35
tiernob20a9022018-05-22 12:07:05 +020036
tierno1e9a3292018-11-05 18:18:45 +010037@pytest.fixture(scope="function", params=[True, False])
38def msg_local_config(request):
39 msg = MsgLocal(lock=request.param)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010040 msg.connect({"path": valid_path() + str(uuid.uuid4())})
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010041 yield msg
tiernob20a9022018-05-22 12:07:05 +020042
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010043 msg.disconnect()
44 if msg.path != invalid_path():
45 shutil.rmtree(msg.path)
46
tiernob20a9022018-05-22 12:07:05 +020047
tierno1e9a3292018-11-05 18:18:45 +010048@pytest.fixture(scope="function", params=[True, False])
49def msg_local_with_data(request):
50 msg = MsgLocal(lock=request.param)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010051 msg.connect({"path": valid_path() + str(uuid.uuid4())})
tiernob20a9022018-05-22 12:07:05 +020052
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010053 msg.write("topic1", "key1", "msg1")
54 msg.write("topic1", "key2", "msg1")
55 msg.write("topic2", "key1", "msg1")
56 msg.write("topic2", "key2", "msg1")
57 msg.write("topic1", "key1", "msg2")
58 msg.write("topic1", "key2", "msg2")
59 msg.write("topic2", "key1", "msg2")
60 msg.write("topic2", "key2", "msg2")
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010061 yield msg
tiernob20a9022018-05-22 12:07:05 +020062
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010063 msg.disconnect()
64 if msg.path != invalid_path():
65 shutil.rmtree(msg.path)
66
tiernob20a9022018-05-22 12:07:05 +020067
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010068def empty_exception_message():
69 return "messaging exception "
70
tiernob20a9022018-05-22 12:07:05 +020071
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010072def test_constructor():
73 msg = MsgLocal()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010074 assert msg.logger == logging.getLogger('msg')
tiernob20a9022018-05-22 12:07:05 +020075 assert msg.path is None
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +010076 assert len(msg.files_read) == 0
77 assert len(msg.files_write) == 0
78 assert len(msg.buffer) == 0
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010079
tiernob20a9022018-05-22 12:07:05 +020080
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010081def test_constructor_with_logger():
82 logger_name = 'msg_local'
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010083 msg = MsgLocal(logger_name=logger_name)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010084 assert msg.logger == logging.getLogger(logger_name)
tiernob20a9022018-05-22 12:07:05 +020085 assert msg.path is None
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +010086 assert len(msg.files_read) == 0
87 assert len(msg.files_write) == 0
88 assert len(msg.buffer) == 0
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010089
tiernob20a9022018-05-22 12:07:05 +020090
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010091@pytest.mark.parametrize("config, logger_name, path", [
92 ({"logger_name": "msg_local", "path": valid_path()}, "msg_local", valid_path()),
93 ({"logger_name": "msg_local", "path": valid_path()[:-1]}, "msg_local", valid_path()),
94 ({"logger_name": "msg_local", "path": valid_path() + "test_it/"}, "msg_local", valid_path() + "test_it/"),
95 ({"logger_name": "msg_local", "path": valid_path() + "test_it"}, "msg_local", valid_path() + "test_it/"),
96 ({"path": valid_path()}, "msg", valid_path()),
97 ({"path": valid_path()[:-1]}, "msg", valid_path()),
98 ({"path": valid_path() + "test_it/"}, "msg", valid_path() + "test_it/"),
99 ({"path": valid_path() + "test_it"}, "msg", valid_path() + "test_it/")])
100def test_connect(msg_local, config, logger_name, path):
101 msg_local.connect(config)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100102 assert msg_local.logger == logging.getLogger(logger_name)
103 assert msg_local.path == path
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100104 assert len(msg_local.files_read) == 0
105 assert len(msg_local.files_write) == 0
106 assert len(msg_local.buffer) == 0
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100107
tiernob20a9022018-05-22 12:07:05 +0200108
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100109@pytest.mark.parametrize("config", [
110 ({"logger_name": "msg_local", "path": invalid_path()}),
111 ({"path": invalid_path()})])
112def test_connect_with_exception(msg_local, config):
113 with pytest.raises(MsgException) as excinfo:
114 msg_local.connect(config)
115 assert str(excinfo.value).startswith(empty_exception_message())
116 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
117
tiernob20a9022018-05-22 12:07:05 +0200118
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100119def test_disconnect(msg_local_config):
tierno1e9a3292018-11-05 18:18:45 +0100120 files_read = msg_local_config.files_read.copy()
121 files_write = msg_local_config.files_write.copy()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100122 msg_local_config.disconnect()
tierno1e9a3292018-11-05 18:18:45 +0100123 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100124 assert f.closed
tierno1e9a3292018-11-05 18:18:45 +0100125 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100126 assert f.closed
127
tiernob20a9022018-05-22 12:07:05 +0200128
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100129def test_disconnect_with_read(msg_local_config):
130 msg_local_config.read('topic1', blocks=False)
131 msg_local_config.read('topic2', blocks=False)
tierno1e9a3292018-11-05 18:18:45 +0100132 files_read = msg_local_config.files_read.copy()
133 files_write = msg_local_config.files_write.copy()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100134 msg_local_config.disconnect()
tierno1e9a3292018-11-05 18:18:45 +0100135 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100136 assert f.closed
tierno1e9a3292018-11-05 18:18:45 +0100137 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100138 assert f.closed
139
tiernob20a9022018-05-22 12:07:05 +0200140
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100141def test_disconnect_with_write(msg_local_with_data):
tierno1e9a3292018-11-05 18:18:45 +0100142 files_read = msg_local_with_data.files_read.copy()
143 files_write = msg_local_with_data.files_write.copy()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100144 msg_local_with_data.disconnect()
145
tierno1e9a3292018-11-05 18:18:45 +0100146 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100147 assert f.closed
148
tierno1e9a3292018-11-05 18:18:45 +0100149 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100150 assert f.closed
151
tiernob20a9022018-05-22 12:07:05 +0200152
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100153def test_disconnect_with_read_and_write(msg_local_with_data):
154 msg_local_with_data.read('topic1', blocks=False)
155 msg_local_with_data.read('topic2', blocks=False)
tierno1e9a3292018-11-05 18:18:45 +0100156 files_read = msg_local_with_data.files_read.copy()
157 files_write = msg_local_with_data.files_write.copy()
158
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100159 msg_local_with_data.disconnect()
tierno1e9a3292018-11-05 18:18:45 +0100160 for f in files_read.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100161 assert f.closed
tierno1e9a3292018-11-05 18:18:45 +0100162 for f in files_write.values():
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100163 assert f.closed
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100164
tiernob20a9022018-05-22 12:07:05 +0200165
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100166@pytest.mark.parametrize("topic, key, msg", [
167 ("test_topic", "test_key", "test_msg"),
168 ("test", "test_key", "test_msg"),
169 ("test_topic", "test", "test_msg"),
170 ("test_topic", "test_key", "test"),
171 ("test_topic", "test_list", ["a", "b", "c"]),
172 ("test_topic", "test_tuple", ("c", "b", "a")),
173 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}),
174 ("test_topic", "test_number", 123),
175 ("test_topic", "test_float", 1.23),
176 ("test_topic", "test_boolean", True),
177 ("test_topic", "test_none", None)])
178def test_write(msg_local_config, topic, key, msg):
179 file_path = msg_local_config.path + topic
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100180 msg_local_config.write(topic, key, msg)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100181 assert os.path.exists(file_path)
182
183 with open(file_path, 'r') as stream:
184 assert yaml.load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
185
tiernob20a9022018-05-22 12:07:05 +0200186
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100187@pytest.mark.parametrize("topic, key, msg, times", [
188 ("test_topic", "test_key", "test_msg", 2),
189 ("test", "test_key", "test_msg", 3),
190 ("test_topic", "test", "test_msg", 4),
191 ("test_topic", "test_key", "test", 2),
192 ("test_topic", "test_list", ["a", "b", "c"], 3),
193 ("test_topic", "test_tuple", ("c", "b", "a"), 4),
194 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2),
195 ("test_topic", "test_number", 123, 3),
196 ("test_topic", "test_float", 1.23, 4),
197 ("test_topic", "test_boolean", True, 2),
198 ("test_topic", "test_none", None, 3)])
199def test_write_with_multiple_calls(msg_local_config, topic, key, msg, times):
200 file_path = msg_local_config.path + topic
201
202 for _ in range(times):
203 msg_local_config.write(topic, key, msg)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100204 assert os.path.exists(file_path)
205
206 with open(file_path, 'r') as stream:
207 for _ in range(times):
208 data = stream.readline()
209 assert yaml.load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
210
tiernob20a9022018-05-22 12:07:05 +0200211
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100212def test_write_exception(msg_local_config):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100213 msg_local_config.files_write = MagicMock()
214 msg_local_config.files_write.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100215
216 with pytest.raises(MsgException) as excinfo:
217 msg_local_config.write("test", "test", "test")
218 assert str(excinfo.value).startswith(empty_exception_message())
219 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
220
tiernob20a9022018-05-22 12:07:05 +0200221
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100222@pytest.mark.parametrize("topics, datas", [
223 (["topic"], [{"key": "value"}]),
224 (["topic1"], [{"key": "value"}]),
225 (["topic2"], [{"key": "value"}]),
226 (["topic", "topic1"], [{"key": "value"}]),
227 (["topic", "topic2"], [{"key": "value"}]),
228 (["topic1", "topic2"], [{"key": "value"}]),
229 (["topic", "topic1", "topic2"], [{"key": "value"}]),
230 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
231 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
232 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
233 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
234 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
235 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
236 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
237def test_read(msg_local_with_data, topics, datas):
238 def write_to_topic(topics, datas):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100239 # Allow msglocal to block while waiting
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100240 time.sleep(2)
241 for topic in topics:
242 for data in datas:
243 with open(msg_local_with_data.path + topic, "a+") as fp:
244 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
245 fp.flush()
246
247 # If file is not opened first, the messages written won't be seen
248 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100249 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100250 msg_local_with_data.read(topic, blocks=False)
251
252 t = threading.Thread(target=write_to_topic, args=(topics, datas))
253 t.start()
254
255 for topic in topics:
256 for data in datas:
257 recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100258 key = list(data.keys())[0]
259 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100260 assert recv_topic == topic
261 assert recv_key == key
262 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100263 t.join()
264
tiernob20a9022018-05-22 12:07:05 +0200265
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100266@pytest.mark.parametrize("topics, datas", [
267 (["topic"], [{"key": "value"}]),
268 (["topic1"], [{"key": "value"}]),
269 (["topic2"], [{"key": "value"}]),
270 (["topic", "topic1"], [{"key": "value"}]),
271 (["topic", "topic2"], [{"key": "value"}]),
272 (["topic1", "topic2"], [{"key": "value"}]),
273 (["topic", "topic1", "topic2"], [{"key": "value"}]),
274 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
275 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
276 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
277 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
278 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
279 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
280 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
281def test_read_non_block(msg_local_with_data, topics, datas):
282 def write_to_topic(topics, datas):
283 for topic in topics:
284 for data in datas:
285 with open(msg_local_with_data.path + topic, "a+") as fp:
286 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
287 fp.flush()
288
289 # If file is not opened first, the messages written won't be seen
290 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100291 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100292 msg_local_with_data.read(topic, blocks=False)
293
294 t = threading.Thread(target=write_to_topic, args=(topics, datas))
295 t.start()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100296 t.join()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100297
298 for topic in topics:
299 for data in datas:
300 recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic, blocks=False)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100301 key = list(data.keys())[0]
302 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100303 assert recv_topic == topic
304 assert recv_key == key
305 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100306
tiernob20a9022018-05-22 12:07:05 +0200307
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100308@pytest.mark.parametrize("topics, datas", [
309 (["topic"], [{"key": "value"}]),
310 (["topic1"], [{"key": "value"}]),
311 (["topic2"], [{"key": "value"}]),
312 (["topic", "topic1"], [{"key": "value"}]),
313 (["topic", "topic2"], [{"key": "value"}]),
314 (["topic1", "topic2"], [{"key": "value"}]),
315 (["topic", "topic1", "topic2"], [{"key": "value"}]),
316 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
317 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
318 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
319 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
320 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
321 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
322 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
323def test_read_non_block_none(msg_local_with_data, topics, datas):
324 def write_to_topic(topics, datas):
325 time.sleep(2)
326 for topic in topics:
327 for data in datas:
328 with open(msg_local_with_data.path + topic, "a+") as fp:
329 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
330 fp.flush()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100331 # If file is not opened first, the messages written won't be seen
332 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100333 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100334 msg_local_with_data.read(topic, blocks=False)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100335 t = threading.Thread(target=write_to_topic, args=(topics, datas))
336 t.start()
337
338 for topic in topics:
339 recv_data = msg_local_with_data.read(topic, blocks=False)
tiernob20a9022018-05-22 12:07:05 +0200340 assert recv_data is None
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100341 t.join()
342
tiernob20a9022018-05-22 12:07:05 +0200343
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100344@pytest.mark.parametrize("blocks", [
345 (True),
346 (False)])
347def test_read_exception(msg_local_with_data, blocks):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100348 msg_local_with_data.files_read = MagicMock()
349 msg_local_with_data.files_read.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100350
351 with pytest.raises(MsgException) as excinfo:
352 msg_local_with_data.read("topic1", blocks=blocks)
353 assert str(excinfo.value).startswith(empty_exception_message())
354 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
355
tiernob20a9022018-05-22 12:07:05 +0200356
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100357@pytest.mark.parametrize("topics, datas", [
358 (["topic"], [{"key": "value"}]),
359 (["topic1"], [{"key": "value"}]),
360 (["topic2"], [{"key": "value"}]),
361 (["topic", "topic1"], [{"key": "value"}]),
362 (["topic", "topic2"], [{"key": "value"}]),
363 (["topic1", "topic2"], [{"key": "value"}]),
364 (["topic", "topic1", "topic2"], [{"key": "value"}]),
365 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
366 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
367 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
368 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
369 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
370 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
371 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
372def test_aioread(msg_local_with_data, event_loop, topics, datas):
373 def write_to_topic(topics, datas):
374 time.sleep(2)
375 for topic in topics:
376 for data in datas:
377 with open(msg_local_with_data.path + topic, "a+") as fp:
378 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
379 fp.flush()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100380 # If file is not opened first, the messages written won't be seen
381 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100382 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100383 msg_local_with_data.read(topic, blocks=False)
384
385 t = threading.Thread(target=write_to_topic, args=(topics, datas))
386 t.start()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100387 for topic in topics:
388 for data in datas:
389 recv = event_loop.run_until_complete(msg_local_with_data.aioread(topic, event_loop))
390 recv_topic, recv_key, recv_msg = recv
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100391 key = list(data.keys())[0]
392 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100393 assert recv_topic == topic
394 assert recv_key == key
395 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100396 t.join()
397
tiernob20a9022018-05-22 12:07:05 +0200398
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100399def test_aioread_exception(msg_local_with_data, event_loop):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100400 msg_local_with_data.files_read = MagicMock()
401 msg_local_with_data.files_read.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100402
403 with pytest.raises(MsgException) as excinfo:
404 event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop))
405 assert str(excinfo.value).startswith(empty_exception_message())
406 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
407
tiernob20a9022018-05-22 12:07:05 +0200408
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100409def test_aioread_general_exception(msg_local_with_data, event_loop):
410 msg_local_with_data.read = MagicMock()
411 msg_local_with_data.read.side_effect = Exception()
412
413 with pytest.raises(MsgException) as excinfo:
414 event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop))
415 assert str(excinfo.value).startswith(empty_exception_message())
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100416 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
417
tiernob20a9022018-05-22 12:07:05 +0200418
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100419@pytest.mark.parametrize("topic, key, msg", [
420 ("test_topic", "test_key", "test_msg"),
421 ("test", "test_key", "test_msg"),
422 ("test_topic", "test", "test_msg"),
423 ("test_topic", "test_key", "test"),
424 ("test_topic", "test_list", ["a", "b", "c"]),
425 ("test_topic", "test_tuple", ("c", "b", "a")),
426 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}),
427 ("test_topic", "test_number", 123),
428 ("test_topic", "test_float", 1.23),
429 ("test_topic", "test_boolean", True),
430 ("test_topic", "test_none", None)])
431def test_aiowrite(msg_local_config, event_loop, topic, key, msg):
432 file_path = msg_local_config.path + topic
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100433 event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg))
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100434 assert os.path.exists(file_path)
435
436 with open(file_path, 'r') as stream:
437 assert yaml.load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
438
tiernob20a9022018-05-22 12:07:05 +0200439
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100440@pytest.mark.parametrize("topic, key, msg, times", [
441 ("test_topic", "test_key", "test_msg", 2),
442 ("test", "test_key", "test_msg", 3),
443 ("test_topic", "test", "test_msg", 4),
444 ("test_topic", "test_key", "test", 2),
445 ("test_topic", "test_list", ["a", "b", "c"], 3),
446 ("test_topic", "test_tuple", ("c", "b", "a"), 4),
447 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2),
448 ("test_topic", "test_number", 123, 3),
449 ("test_topic", "test_float", 1.23, 4),
450 ("test_topic", "test_boolean", True, 2),
451 ("test_topic", "test_none", None, 3)])
452def test_aiowrite_with_multiple_calls(msg_local_config, event_loop, topic, key, msg, times):
453 file_path = msg_local_config.path + topic
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100454 for _ in range(times):
455 event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg))
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100456 assert os.path.exists(file_path)
457
458 with open(file_path, 'r') as stream:
459 for _ in range(times):
460 data = stream.readline()
461 assert yaml.load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
462
tiernob20a9022018-05-22 12:07:05 +0200463
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100464def test_aiowrite_exception(msg_local_config, event_loop):
465 msg_local_config.files_write = MagicMock()
466 msg_local_config.files_write.__contains__.side_effect = Exception()
467
468 with pytest.raises(MsgException) as excinfo:
469 event_loop.run_until_complete(msg_local_config.aiowrite("test", "test", "test"))
470 assert str(excinfo.value).startswith(empty_exception_message())
471 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR