From b9d24494586db59d4bf316a2fac875967b21c17a Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Tue, 23 Feb 2016 16:05:38 +0100 Subject: Only do initial sync on session creation - Issue #615 - Fixed some long broken but not failing tests --- service/pixelated/bitmask_libraries/session.py | 12 +++++++-- .../test/unit/bitmask_libraries/test_session.py | 31 ++++++++++++++++++---- 2 files changed, 36 insertions(+), 7 deletions(-) (limited to 'service') diff --git a/service/pixelated/bitmask_libraries/session.py b/service/pixelated/bitmask_libraries/session.py index 9e908ce5..f28d9f59 100644 --- a/service/pixelated/bitmask_libraries/session.py +++ b/service/pixelated/bitmask_libraries/session.py @@ -53,12 +53,20 @@ class LeapSession(object): self.fresh_account = False self.incoming_mail_fetcher = None self.account = None + self._has_been_synced = False + self._sem_intial_sync = defer.DeferredLock() register(events.KEYMANAGER_FINISHED_KEY_GENERATION, self._set_fresh_account, uid=self.account_email()) @defer.inlineCallbacks def initial_sync(self): - yield self.sync() - yield self.after_first_sync() + yield self._sem_intial_sync.acquire() + try: + if not self._has_been_synced: + yield self.sync() + yield self.after_first_sync() + self._has_been_synced = True + finally: + yield self._sem_intial_sync.release() defer.returnValue(self) @defer.inlineCallbacks diff --git a/service/test/unit/bitmask_libraries/test_session.py b/service/test/unit/bitmask_libraries/test_session.py index 22c2b3d9..a41cb805 100644 --- a/service/test/unit/bitmask_libraries/test_session.py +++ b/service/test/unit/bitmask_libraries/test_session.py @@ -16,7 +16,7 @@ from mock import patch from mock import MagicMock - +from twisted.internet import defer from pixelated.bitmask_libraries.session import LeapSession, SessionCache from test_abstract_leap import AbstractLeapTest from leap.common.events.catalog import KEYMANAGER_FINISHED_KEY_GENERATION @@ -28,14 +28,22 @@ class SessionTest(AbstractLeapTest): super(SessionTest, self).setUp() self.smtp_mock = MagicMock() - def test_background_jobs_are_started_during_initial_sync(self): + @patch('pixelated.bitmask_libraries.session.register') + @patch('pixelated.bitmask_libraries.session.IMAPAccount') + @defer.inlineCallbacks + def test_background_jobs_are_started_during_initial_sync(self, *unused): + mailFetcherMock = MagicMock() with patch('pixelated.bitmask_libraries.session.reactor.callFromThread', new=_execute_func) as _: - with patch('pixelated.bitmask_libraries.session.LeapSession._create_incoming_mail_fetcher') as mail_fetcher_mock: + with patch.object(LeapSession, '_create_incoming_mail_fetcher', return_value=mailFetcherMock) as _: session = self._create_session() yield session.initial_sync() - mail_fetcher_mock.startService.assert_called_once() + mailFetcherMock.startService.assert_called_once() - def test_that_close_stops_background_jobs(self): + @patch('pixelated.bitmask_libraries.session.register') + @patch('pixelated.bitmask_libraries.session.unregister') + @patch('pixelated.bitmask_libraries.session.IMAPAccount') + @defer.inlineCallbacks + def test_that_close_stops_background_jobs(self, *unused): with patch('pixelated.bitmask_libraries.session.reactor.callFromThread', new=_execute_func) as _: with patch('pixelated.bitmask_libraries.session.LeapSession._create_incoming_mail_fetcher') as mail_fetcher_mock: session = self._create_session() @@ -133,6 +141,19 @@ class SessionTest(AbstractLeapTest): self.assertFalse(session.fresh_account) + @patch('pixelated.bitmask_libraries.session.register') + @patch('pixelated.bitmask_libraries.session.unregister') + @patch('pixelated.bitmask_libraries.session.IMAPAccount') + @defer.inlineCallbacks + def test_session_initial_sync_only_triggered_once(self, *unused): + mailFetcherMock = MagicMock() + with patch('pixelated.bitmask_libraries.session.reactor.callFromThread', new=_execute_func) as _: + with patch.object(LeapSession, '_create_incoming_mail_fetcher', return_value=mailFetcherMock) as _: + session = self._create_session() + session._has_been_synced = True + yield session.initial_sync() + self.assertFalse(mailFetcherMock.startService.called) + def _create_session(self): return LeapSession(self.provider, self.auth, self.mail_store, self.soledad_session, self.nicknym, self.smtp_mock) -- cgit v1.2.3