summaryrefslogtreecommitdiff
path: root/testing/tests/sync/test_sync_mutex.py
diff options
context:
space:
mode:
authordrebs <drebs@leap.se>2016-07-07 11:44:01 +0200
committerKali Kaneko <kali@leap.se>2016-07-12 03:09:27 +0200
commit26f87181f8a8fc7fef58ddd1e52cb5f0edd641bb (patch)
tree899c0a7ec979f60073f87af3732edc2eac811044 /testing/tests/sync/test_sync_mutex.py
parentb3fb215860a8e50e4a6c551fef78628acdbf25c7 (diff)
[test] toxify tests
- move tests to root directory - split tests in different subdirectories - setup a small package with common test dependencies in /testing/test_soledad - add tox.ini that will: - install the test_soledad package and other test dependencies - install soledad common, client, server from the repository - run tests contianed in /testing/tests directory using pytest This commit also removes all oauth code from tests, as we have removed the u1db dependency (by importing it into the repo and naming it l2db) and don't neet oauth at all right now.
Diffstat (limited to 'testing/tests/sync/test_sync_mutex.py')
-rw-r--r--testing/tests/sync/test_sync_mutex.py135
1 files changed, 135 insertions, 0 deletions
diff --git a/testing/tests/sync/test_sync_mutex.py b/testing/tests/sync/test_sync_mutex.py
new file mode 100644
index 00000000..787cfee8
--- /dev/null
+++ b/testing/tests/sync/test_sync_mutex.py
@@ -0,0 +1,135 @@
+# -*- coding: utf-8 -*-
+# test_sync_mutex.py
+# Copyright (C) 2013, 2014 LEAP
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+"""
+Test that synchronization is a critical section and, as such, there might not
+be two concurrent synchronization processes at the same time.
+"""
+
+
+import time
+import uuid
+import tempfile
+import shutil
+
+from urlparse import urljoin
+
+from twisted.internet import defer
+
+from leap.soledad.client.sync import SoledadSynchronizer
+
+from leap.soledad.common.couch.state import CouchServerState
+from leap.soledad.common.couch import CouchDatabase
+from test_soledad.u1db_tests import TestCaseWithServer
+
+from test_soledad.util import CouchDBTestCase
+from test_soledad.util import BaseSoledadTest
+from test_soledad.util import make_token_soledad_app
+from test_soledad.util import make_soledad_document_for_test
+from test_soledad.util import soledad_sync_target
+
+
+# monkey-patch the soledad synchronizer so it stores start and finish times
+
+_old_sync = SoledadSynchronizer.sync
+
+
+def _timed_sync(self, defer_decryption=True):
+ t = time.time()
+
+ sync_id = uuid.uuid4()
+
+ if not getattr(self.source, 'sync_times', False):
+ self.source.sync_times = {}
+
+ self.source.sync_times[sync_id] = {'start': t}
+
+ def _store_finish_time(passthrough):
+ t = time.time()
+ self.source.sync_times[sync_id]['end'] = t
+ return passthrough
+
+ d = _old_sync(self, defer_decryption=defer_decryption)
+ d.addBoth(_store_finish_time)
+ return d
+
+SoledadSynchronizer.sync = _timed_sync
+
+# -- end of monkey-patching
+
+
+class TestSyncMutex(
+ BaseSoledadTest, CouchDBTestCase, TestCaseWithServer):
+
+ @staticmethod
+ def make_app_with_state(state):
+ return make_token_soledad_app(state)
+
+ make_document_for_test = make_soledad_document_for_test
+
+ sync_target = soledad_sync_target
+
+ def make_app(self):
+ self.request_state = CouchServerState(self.couch_url)
+ return self.make_app_with_state(self.request_state)
+
+ def setUp(self):
+ TestCaseWithServer.setUp(self)
+ CouchDBTestCase.setUp(self)
+ self.tempdir = tempfile.mkdtemp(prefix="leap_tests-")
+ self.user = ('user-%s' % uuid.uuid4().hex)
+
+ def tearDown(self):
+ CouchDBTestCase.tearDown(self)
+ TestCaseWithServer.tearDown(self)
+ shutil.rmtree(self.tempdir)
+
+ def test_two_concurrent_syncs_do_not_overlap_no_docs(self):
+ self.startServer()
+
+ # ensure remote db exists before syncing
+ db = CouchDatabase.open_database(
+ urljoin(self.couch_url, 'user-' + self.user),
+ create=True,
+ ensure_ddocs=True)
+
+ sol = self._soledad_instance(
+ user=self.user, server_url=self.getURL())
+
+ d1 = sol.sync()
+ d2 = sol.sync()
+
+ def _assert_syncs_do_not_overlap(thearg):
+ # recover sync times
+ sync_times = []
+ for key in sol._dbsyncer.sync_times:
+ sync_times.append(sol._dbsyncer.sync_times[key])
+ sync_times.sort(key=lambda s: s['start'])
+
+ self.assertTrue(
+ (sync_times[0]['start'] < sync_times[0]['end'] and
+ sync_times[0]['end'] < sync_times[1]['start'] and
+ sync_times[1]['start'] < sync_times[1]['end']))
+
+ db.delete_database()
+ db.close()
+ sol.close()
+
+ d = defer.gatherResults([d1, d2])
+ d.addBoth(_assert_syncs_do_not_overlap)
+ return d