improvements in dbmemory. Change yaml.load to save_load
[osm/common.git] / osm_common / tests / test_msglocal.py
index 93bd54d..41f6eb8 100644 (file)
@@ -1,3 +1,22 @@
+# Copyright 2018 Whitestack, LLC
+# Copyright 2018 Telefonica S.A.
+#
+# Licensed under the Apache License, Version 2.0 (the "License"); you may
+# not use this file except in compliance with the License. You may obtain
+# a copy of the License at
+#
+#         http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+# For those usages not covered by the Apache License, Version 2.0 please
+# contact: esousa@whitestack.com or alfonso.tiernosepulveda@telefonica.com
+##
+
 import http
 import logging
 import pytest
@@ -24,19 +43,19 @@ def invalid_path():
     return '/#tweeter/'
 
 
-@pytest.fixture
-def msg_local():
-    msg = MsgLocal()
+@pytest.fixture(scope="function", params=[True, False])
+def msg_local(request):
+    msg = MsgLocal(lock=request.param)
     yield msg
 
+    msg.disconnect()
     if msg.path and msg.path != invalid_path() and msg.path != valid_path():
-        msg.disconnect()
         shutil.rmtree(msg.path)
 
 
-@pytest.fixture
-def msg_local_config():
-    msg = MsgLocal()
+@pytest.fixture(scope="function", params=[True, False])
+def msg_local_config(request):
+    msg = MsgLocal(lock=request.param)
     msg.connect({"path": valid_path() + str(uuid.uuid4())})
     yield msg
 
@@ -45,9 +64,9 @@ def msg_local_config():
         shutil.rmtree(msg.path)
 
 
-@pytest.fixture
-def msg_local_with_data():
-    msg = MsgLocal()
+@pytest.fixture(scope="function", params=[True, False])
+def msg_local_with_data(request):
+    msg = MsgLocal(lock=request.param)
     msg.connect({"path": valid_path() + str(uuid.uuid4())})
 
     msg.write("topic1", "key1", "msg1")
@@ -117,41 +136,49 @@ def test_connect_with_exception(msg_local, config):
 
 
 def test_disconnect(msg_local_config):
+    files_read = msg_local_config.files_read.copy()
+    files_write = msg_local_config.files_write.copy()
     msg_local_config.disconnect()
-    for f in msg_local_config.files_read.values():
+    for f in files_read.values():
         assert f.closed
-    for f in msg_local_config.files_write.values():
+    for f in files_write.values():
         assert f.closed
 
 
 def test_disconnect_with_read(msg_local_config):
     msg_local_config.read('topic1', blocks=False)
     msg_local_config.read('topic2', blocks=False)
+    files_read = msg_local_config.files_read.copy()
+    files_write = msg_local_config.files_write.copy()
     msg_local_config.disconnect()
-    for f in msg_local_config.files_read.values():
+    for f in files_read.values():
         assert f.closed
-    for f in msg_local_config.files_write.values():
+    for f in files_write.values():
         assert f.closed
 
 
 def test_disconnect_with_write(msg_local_with_data):
+    files_read = msg_local_with_data.files_read.copy()
+    files_write = msg_local_with_data.files_write.copy()
     msg_local_with_data.disconnect()
 
-    for f in msg_local_with_data.files_read.values():
+    for f in files_read.values():
         assert f.closed
     
-    for f in msg_local_with_data.files_write.values():
+    for f in files_write.values():
         assert f.closed
 
 
 def test_disconnect_with_read_and_write(msg_local_with_data):
     msg_local_with_data.read('topic1', blocks=False)
     msg_local_with_data.read('topic2', blocks=False)
-    
+    files_read = msg_local_with_data.files_read.copy()
+    files_write = msg_local_with_data.files_write.copy()
+
     msg_local_with_data.disconnect()
-    for f in msg_local_with_data.files_read.values():
+    for f in files_read.values():
         assert f.closed
-    for f in msg_local_with_data.files_write.values():
+    for f in files_write.values():
         assert f.closed
 
 
@@ -173,7 +200,7 @@ def test_write(msg_local_config, topic, key, msg):
     assert os.path.exists(file_path)
 
     with open(file_path, 'r') as stream:
-        assert yaml.load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
+        assert yaml.safe_load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
 
 
 @pytest.mark.parametrize("topic, key, msg, times", [
@@ -198,7 +225,7 @@ def test_write_with_multiple_calls(msg_local_config, topic, key, msg, times):
     with open(file_path, 'r') as stream:
         for _ in range(times):
             data = stream.readline()
-            assert yaml.load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
+            assert yaml.safe_load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
 
 
 def test_write_exception(msg_local_config):
@@ -426,7 +453,7 @@ def test_aiowrite(msg_local_config, event_loop, topic, key, msg):
     assert os.path.exists(file_path)
 
     with open(file_path, 'r') as stream:
-        assert yaml.load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
+        assert yaml.safe_load(stream) == {key: msg if not isinstance(msg, tuple) else list(msg)}
 
 
 @pytest.mark.parametrize("topic, key, msg, times", [
@@ -450,7 +477,7 @@ def test_aiowrite_with_multiple_calls(msg_local_config, event_loop, topic, key,
     with open(file_path, 'r') as stream:
         for _ in range(times):
             data = stream.readline()
-            assert yaml.load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
+            assert yaml.safe_load(data) == {key: msg if not isinstance(msg, tuple) else list(msg)}
 
 
 def test_aiowrite_exception(msg_local_config, event_loop):