blob: 93bd54d0f834db70e3e21bf05b75f508d2fe009e [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
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010027@pytest.fixture
28def msg_local():
29 msg = MsgLocal()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010030 yield msg
31
32 if msg.path and msg.path != invalid_path() and msg.path != valid_path():
33 msg.disconnect()
34 shutil.rmtree(msg.path)
35
tiernob20a9022018-05-22 12:07:05 +020036
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010037@pytest.fixture
38def msg_local_config():
39 msg = MsgLocal()
40 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
Eduardo Sousaacbbdf22018-05-03 15:47:41 +010048@pytest.fixture
49def msg_local_with_data():
50 msg = MsgLocal()
51 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):
120 msg_local_config.disconnect()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100121 for f in msg_local_config.files_read.values():
122 assert f.closed
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100123 for f in msg_local_config.files_write.values():
124 assert f.closed
125
tiernob20a9022018-05-22 12:07:05 +0200126
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100127def test_disconnect_with_read(msg_local_config):
128 msg_local_config.read('topic1', blocks=False)
129 msg_local_config.read('topic2', blocks=False)
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100130 msg_local_config.disconnect()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100131 for f in msg_local_config.files_read.values():
132 assert f.closed
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100133 for f in msg_local_config.files_write.values():
134 assert f.closed
135
tiernob20a9022018-05-22 12:07:05 +0200136
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100137def test_disconnect_with_write(msg_local_with_data):
138 msg_local_with_data.disconnect()
139
140 for f in msg_local_with_data.files_read.values():
141 assert f.closed
142
143 for f in msg_local_with_data.files_write.values():
144 assert f.closed
145
tiernob20a9022018-05-22 12:07:05 +0200146
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100147def test_disconnect_with_read_and_write(msg_local_with_data):
148 msg_local_with_data.read('topic1', blocks=False)
149 msg_local_with_data.read('topic2', blocks=False)
150
151 msg_local_with_data.disconnect()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100152 for f in msg_local_with_data.files_read.values():
153 assert f.closed
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100154 for f in msg_local_with_data.files_write.values():
155 assert f.closed
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100156
tiernob20a9022018-05-22 12:07:05 +0200157
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100158@pytest.mark.parametrize("topic, key, msg", [
159 ("test_topic", "test_key", "test_msg"),
160 ("test", "test_key", "test_msg"),
161 ("test_topic", "test", "test_msg"),
162 ("test_topic", "test_key", "test"),
163 ("test_topic", "test_list", ["a", "b", "c"]),
164 ("test_topic", "test_tuple", ("c", "b", "a")),
165 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}),
166 ("test_topic", "test_number", 123),
167 ("test_topic", "test_float", 1.23),
168 ("test_topic", "test_boolean", True),
169 ("test_topic", "test_none", None)])
170def test_write(msg_local_config, topic, key, msg):
171 file_path = msg_local_config.path + topic
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100172 msg_local_config.write(topic, key, msg)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100173 assert os.path.exists(file_path)
174
175 with open(file_path, 'r') as stream:
176 assert yaml.load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
177
tiernob20a9022018-05-22 12:07:05 +0200178
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100179@pytest.mark.parametrize("topic, key, msg, times", [
180 ("test_topic", "test_key", "test_msg", 2),
181 ("test", "test_key", "test_msg", 3),
182 ("test_topic", "test", "test_msg", 4),
183 ("test_topic", "test_key", "test", 2),
184 ("test_topic", "test_list", ["a", "b", "c"], 3),
185 ("test_topic", "test_tuple", ("c", "b", "a"), 4),
186 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2),
187 ("test_topic", "test_number", 123, 3),
188 ("test_topic", "test_float", 1.23, 4),
189 ("test_topic", "test_boolean", True, 2),
190 ("test_topic", "test_none", None, 3)])
191def test_write_with_multiple_calls(msg_local_config, topic, key, msg, times):
192 file_path = msg_local_config.path + topic
193
194 for _ in range(times):
195 msg_local_config.write(topic, key, msg)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100196 assert os.path.exists(file_path)
197
198 with open(file_path, 'r') as stream:
199 for _ in range(times):
200 data = stream.readline()
201 assert yaml.load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
202
tiernob20a9022018-05-22 12:07:05 +0200203
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100204def test_write_exception(msg_local_config):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100205 msg_local_config.files_write = MagicMock()
206 msg_local_config.files_write.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100207
208 with pytest.raises(MsgException) as excinfo:
209 msg_local_config.write("test", "test", "test")
210 assert str(excinfo.value).startswith(empty_exception_message())
211 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
212
tiernob20a9022018-05-22 12:07:05 +0200213
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100214@pytest.mark.parametrize("topics, datas", [
215 (["topic"], [{"key": "value"}]),
216 (["topic1"], [{"key": "value"}]),
217 (["topic2"], [{"key": "value"}]),
218 (["topic", "topic1"], [{"key": "value"}]),
219 (["topic", "topic2"], [{"key": "value"}]),
220 (["topic1", "topic2"], [{"key": "value"}]),
221 (["topic", "topic1", "topic2"], [{"key": "value"}]),
222 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
223 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
224 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
225 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
226 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
227 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
228 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
229def test_read(msg_local_with_data, topics, datas):
230 def write_to_topic(topics, datas):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100231 # Allow msglocal to block while waiting
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100232 time.sleep(2)
233 for topic in topics:
234 for data in datas:
235 with open(msg_local_with_data.path + topic, "a+") as fp:
236 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
237 fp.flush()
238
239 # If file is not opened first, the messages written won't be seen
240 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100241 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100242 msg_local_with_data.read(topic, blocks=False)
243
244 t = threading.Thread(target=write_to_topic, args=(topics, datas))
245 t.start()
246
247 for topic in topics:
248 for data in datas:
249 recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100250 key = list(data.keys())[0]
251 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100252 assert recv_topic == topic
253 assert recv_key == key
254 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100255 t.join()
256
tiernob20a9022018-05-22 12:07:05 +0200257
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100258@pytest.mark.parametrize("topics, datas", [
259 (["topic"], [{"key": "value"}]),
260 (["topic1"], [{"key": "value"}]),
261 (["topic2"], [{"key": "value"}]),
262 (["topic", "topic1"], [{"key": "value"}]),
263 (["topic", "topic2"], [{"key": "value"}]),
264 (["topic1", "topic2"], [{"key": "value"}]),
265 (["topic", "topic1", "topic2"], [{"key": "value"}]),
266 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
267 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
268 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
269 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
270 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
271 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
272 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
273def test_read_non_block(msg_local_with_data, topics, datas):
274 def write_to_topic(topics, datas):
275 for topic in topics:
276 for data in datas:
277 with open(msg_local_with_data.path + topic, "a+") as fp:
278 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
279 fp.flush()
280
281 # If file is not opened first, the messages written won't be seen
282 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100283 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100284 msg_local_with_data.read(topic, blocks=False)
285
286 t = threading.Thread(target=write_to_topic, args=(topics, datas))
287 t.start()
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100288 t.join()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100289
290 for topic in topics:
291 for data in datas:
292 recv_topic, recv_key, recv_msg = msg_local_with_data.read(topic, blocks=False)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100293 key = list(data.keys())[0]
294 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100295 assert recv_topic == topic
296 assert recv_key == key
297 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100298
tiernob20a9022018-05-22 12:07:05 +0200299
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100300@pytest.mark.parametrize("topics, datas", [
301 (["topic"], [{"key": "value"}]),
302 (["topic1"], [{"key": "value"}]),
303 (["topic2"], [{"key": "value"}]),
304 (["topic", "topic1"], [{"key": "value"}]),
305 (["topic", "topic2"], [{"key": "value"}]),
306 (["topic1", "topic2"], [{"key": "value"}]),
307 (["topic", "topic1", "topic2"], [{"key": "value"}]),
308 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
309 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
310 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
311 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
312 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
313 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
314 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
315def test_read_non_block_none(msg_local_with_data, topics, datas):
316 def write_to_topic(topics, datas):
317 time.sleep(2)
318 for topic in topics:
319 for data in datas:
320 with open(msg_local_with_data.path + topic, "a+") as fp:
321 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
322 fp.flush()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100323 # If file is not opened first, the messages written won't be seen
324 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100325 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100326 msg_local_with_data.read(topic, blocks=False)
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100327 t = threading.Thread(target=write_to_topic, args=(topics, datas))
328 t.start()
329
330 for topic in topics:
331 recv_data = msg_local_with_data.read(topic, blocks=False)
tiernob20a9022018-05-22 12:07:05 +0200332 assert recv_data is None
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100333 t.join()
334
tiernob20a9022018-05-22 12:07:05 +0200335
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100336@pytest.mark.parametrize("blocks", [
337 (True),
338 (False)])
339def test_read_exception(msg_local_with_data, blocks):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100340 msg_local_with_data.files_read = MagicMock()
341 msg_local_with_data.files_read.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100342
343 with pytest.raises(MsgException) as excinfo:
344 msg_local_with_data.read("topic1", blocks=blocks)
345 assert str(excinfo.value).startswith(empty_exception_message())
346 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
347
tiernob20a9022018-05-22 12:07:05 +0200348
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100349@pytest.mark.parametrize("topics, datas", [
350 (["topic"], [{"key": "value"}]),
351 (["topic1"], [{"key": "value"}]),
352 (["topic2"], [{"key": "value"}]),
353 (["topic", "topic1"], [{"key": "value"}]),
354 (["topic", "topic2"], [{"key": "value"}]),
355 (["topic1", "topic2"], [{"key": "value"}]),
356 (["topic", "topic1", "topic2"], [{"key": "value"}]),
357 (["topic"], [{"key": "value"}, {"key1": "value1"}]),
358 (["topic1"], [{"key": "value"}, {"key1": "value1"}]),
359 (["topic2"], [{"key": "value"}, {"key1": "value1"}]),
360 (["topic", "topic1"], [{"key": "value"}, {"key1": "value1"}]),
361 (["topic", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
362 (["topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}]),
363 (["topic", "topic1", "topic2"], [{"key": "value"}, {"key1": "value1"}])])
364def test_aioread(msg_local_with_data, event_loop, topics, datas):
365 def write_to_topic(topics, datas):
366 time.sleep(2)
367 for topic in topics:
368 for data in datas:
369 with open(msg_local_with_data.path + topic, "a+") as fp:
370 yaml.safe_dump(data, fp, default_flow_style=True, width=20000)
371 fp.flush()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100372 # If file is not opened first, the messages written won't be seen
373 for topic in topics:
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100374 if topic not in msg_local_with_data.files_read:
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100375 msg_local_with_data.read(topic, blocks=False)
376
377 t = threading.Thread(target=write_to_topic, args=(topics, datas))
378 t.start()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100379 for topic in topics:
380 for data in datas:
381 recv = event_loop.run_until_complete(msg_local_with_data.aioread(topic, event_loop))
382 recv_topic, recv_key, recv_msg = recv
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100383 key = list(data.keys())[0]
384 val = data[key]
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100385 assert recv_topic == topic
386 assert recv_key == key
387 assert recv_msg == val
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100388 t.join()
389
tiernob20a9022018-05-22 12:07:05 +0200390
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100391def test_aioread_exception(msg_local_with_data, event_loop):
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100392 msg_local_with_data.files_read = MagicMock()
393 msg_local_with_data.files_read.__contains__.side_effect = Exception()
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100394
395 with pytest.raises(MsgException) as excinfo:
396 event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop))
397 assert str(excinfo.value).startswith(empty_exception_message())
398 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
399
tiernob20a9022018-05-22 12:07:05 +0200400
Eduardo Sousaacbbdf22018-05-03 15:47:41 +0100401def test_aioread_general_exception(msg_local_with_data, event_loop):
402 msg_local_with_data.read = MagicMock()
403 msg_local_with_data.read.side_effect = Exception()
404
405 with pytest.raises(MsgException) as excinfo:
406 event_loop.run_until_complete(msg_local_with_data.aioread("topic1", event_loop))
407 assert str(excinfo.value).startswith(empty_exception_message())
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100408 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR
409
tiernob20a9022018-05-22 12:07:05 +0200410
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100411@pytest.mark.parametrize("topic, key, msg", [
412 ("test_topic", "test_key", "test_msg"),
413 ("test", "test_key", "test_msg"),
414 ("test_topic", "test", "test_msg"),
415 ("test_topic", "test_key", "test"),
416 ("test_topic", "test_list", ["a", "b", "c"]),
417 ("test_topic", "test_tuple", ("c", "b", "a")),
418 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}),
419 ("test_topic", "test_number", 123),
420 ("test_topic", "test_float", 1.23),
421 ("test_topic", "test_boolean", True),
422 ("test_topic", "test_none", None)])
423def test_aiowrite(msg_local_config, event_loop, topic, key, msg):
424 file_path = msg_local_config.path + topic
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100425 event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg))
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100426 assert os.path.exists(file_path)
427
428 with open(file_path, 'r') as stream:
429 assert yaml.load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
430
tiernob20a9022018-05-22 12:07:05 +0200431
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100432@pytest.mark.parametrize("topic, key, msg, times", [
433 ("test_topic", "test_key", "test_msg", 2),
434 ("test", "test_key", "test_msg", 3),
435 ("test_topic", "test", "test_msg", 4),
436 ("test_topic", "test_key", "test", 2),
437 ("test_topic", "test_list", ["a", "b", "c"], 3),
438 ("test_topic", "test_tuple", ("c", "b", "a"), 4),
439 ("test_topic", "test_dict", {"a": 1, "b": 2, "c": 3}, 2),
440 ("test_topic", "test_number", 123, 3),
441 ("test_topic", "test_float", 1.23, 4),
442 ("test_topic", "test_boolean", True, 2),
443 ("test_topic", "test_none", None, 3)])
444def test_aiowrite_with_multiple_calls(msg_local_config, event_loop, topic, key, msg, times):
445 file_path = msg_local_config.path + topic
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100446 for _ in range(times):
447 event_loop.run_until_complete(msg_local_config.aiowrite(topic, key, msg))
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100448 assert os.path.exists(file_path)
449
450 with open(file_path, 'r') as stream:
451 for _ in range(times):
452 data = stream.readline()
453 assert yaml.load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
454
tiernob20a9022018-05-22 12:07:05 +0200455
Eduardo Sousaa7f8a6d2018-05-09 13:57:22 +0100456def test_aiowrite_exception(msg_local_config, event_loop):
457 msg_local_config.files_write = MagicMock()
458 msg_local_config.files_write.__contains__.side_effect = Exception()
459
460 with pytest.raises(MsgException) as excinfo:
461 event_loop.run_until_complete(msg_local_config.aiowrite("test", "test", "test"))
462 assert str(excinfo.value).startswith(empty_exception_message())
463 assert excinfo.value.http_code == http.HTTPStatus.INTERNAL_SERVER_ERROR