From 1b1476a0ebb07a5e22b9ee10b2cbc65226c3a12e Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Tue, 23 Feb 2016 09:34:35 +0100 Subject: Improve error message if send mail fails - Issue #609 --- service/pixelated/resources/mails_resource.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'service') diff --git a/service/pixelated/resources/mails_resource.py b/service/pixelated/resources/mails_resource.py index 1c322d35..c87d4ca2 100644 --- a/service/pixelated/resources/mails_resource.py +++ b/service/pixelated/resources/mails_resource.py @@ -183,7 +183,7 @@ class MailsResource(BaseResource): if isinstance(error.value, SMTPDownException): respond_json_deferred({'message': str(error.value)}, request, status_code=503) else: - err(error, 'something failed') + err(error, 'error occurred while sending') respond_json_deferred({'message': 'an error occurred while sending'}, request, status_code=422) deferred = self._handle_post(request) -- cgit v1.2.3 From 9e79c3b4e948e5d8ae1adab4fad7b49b50fe77c3 Mon Sep 17 00:00:00 2001 From: Folker Bernitt Date: Tue, 23 Feb 2016 10:01:39 +0100 Subject: Improve stopwatch output - measures time and cpu time and displays percentage --- service/pixelated/support/__init__.py | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'service') diff --git a/service/pixelated/support/__init__.py b/service/pixelated/support/__init__.py index 80ecaa2e..9ac52fe0 100644 --- a/service/pixelated/support/__init__.py +++ b/service/pixelated/support/__init__.py @@ -22,13 +22,34 @@ from twisted.internet import defer log = logging.getLogger(__name__) +def _start_stopwatch(): + return (time.time(), time.clock()) + + +def _stop_stopwatch(start): + start_time, start_clock = start + end_clock = time.clock() + end_time = time.time() + clock_duration = end_clock - start_clock + time_duration = end_time - start_time + if time_duration < 0.00000001: # avoid division by zero + time_duration = 0.00000001 + + estimate_percent_io = ((time_duration - clock_duration) / time_duration) * 100.0 + + return time_duration, clock_duration, estimate_percent_io + + def log_time(f): @wraps(f) def wrapper(*args, **kwds): - start = time.clock() + start = _start_stopwatch() + result = f(*args, **kwds) - log.info('Needed %f ms to execute %s' % ((time.clock() - start), f)) + + time_duration, clock_duration, estimate_percent_io = _stop_stopwatch(start) + log.info('Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % (time_duration, clock_duration, estimate_percent_io, f)) return result @@ -38,12 +59,13 @@ def log_time(f): def log_time_deferred(f): def log_time(result, start): - log.info('after callback: Needed %f ms to execute %s' % ((time.clock() - start), f)) + time_duration, clock_duration, estimate_percent_io = _stop_stopwatch(start) + log.info('after callback: Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % (time_duration, clock_duration, estimate_percent_io, f)) return result @wraps(f) def wrapper(*args, **kwds): - start = time.clock() + start = _start_stopwatch() result = f(*args, **kwds) if isinstance(result, defer.Deferred): result.addCallback(log_time, start=start) -- cgit v1.2.3 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