Merge lp:~nataliabidart/ubuntu-sso-client/no-quit-if-client into lp:ubuntu-sso-client

Proposed by Natalia Bidart
Status: Merged
Approved by: Natalia Bidart
Approved revision: 672
Merged at revision: 672
Proposed branch: lp:~nataliabidart/ubuntu-sso-client/no-quit-if-client
Merge into: lp:ubuntu-sso-client
Diff against target: 190 lines (+94/-13)
2 files modified
ubuntu_sso/main.py (+11/-3)
ubuntu_sso/tests/test_main.py (+83/-10)
To merge this branch: bzr merge lp:~nataliabidart/ubuntu-sso-client/no-quit-if-client
Reviewer Review Type Date Requested Status
Alejandro J. Cura (community) Approve
Roberto Alsina (community) Approve
Review via email: mp+47806@code.launchpad.net

Commit message

- The service is not shutdown if ref count is not zero, even if the
  shutdown_func was queued (LP: #709200).

- Test failures (caused by a buggy test) fixed (LP: #704941).

To post a comment you must log in.
Revision history for this message
Roberto Alsina (ralsina) wrote :

+1

review: Approve
Revision history for this message
Alejandro J. Cura (alecu) wrote :

The branch does what's supposed to do, but I think the timeouts should not be "ignored when they happen" but instead "cancelled when they are no longer needed"
I'll make a new bug for that.

review: Approve
Revision history for this message
Alejandro J. Cura (alecu) wrote :

The bug for shutdown timeouts is: #709301

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntu_sso/main.py'
2--- ubuntu_sso/main.py 2011-01-11 19:13:19 +0000
3+++ ubuntu_sso/main.py 2011-01-28 13:50:13 +0000
4@@ -50,7 +50,7 @@
5
6 logger = setup_logging("ubuntu_sso.main")
7 U1_PING_URL = "https://one.ubuntu.com/oauth/sso-finished-so-get-tokens/"
8-TIMEOUT_INTERVAL = 500
9+TIMEOUT_INTERVAL = 10000 # 10 seconds
10
11
12 class SSOLoginProcessor(Account):
13@@ -449,7 +449,6 @@
14
15 def _get_ref_count(self):
16 """Get value of ref_count."""
17- logger.debug('ref_count is %r.', self._ref_count)
18 return self._ref_count
19
20 def _set_ref_count(self, new_value):
21@@ -464,10 +463,19 @@
22 self._ref_count = new_value
23
24 if self._ref_count == 0:
25- self.timeout_func(TIMEOUT_INTERVAL, self.shutdown_func)
26+ logger.debug('Setting up timer with %r (%r, %r).',
27+ self.timeout_func, TIMEOUT_INTERVAL, self.shutdown)
28+ self.timeout_func(TIMEOUT_INTERVAL, self.shutdown)
29
30 ref_count = property(fget=_get_ref_count, fset=_set_ref_count)
31
32+ def shutdown(self):
33+ """If no ongoing requests, call self.shutdown_func."""
34+ logger.debug('shutdown!, ref_count is %r.', self._ref_count)
35+ if self._ref_count == 0:
36+ logger.info('Shutting down, calling %r.', self.shutdown_func)
37+ self.shutdown_func()
38+
39 @dbus.service.signal(DBUS_CREDENTIALS_IFACE, signature='s')
40 def AuthorizationDenied(self, app_name):
41 """Signal thrown when the user denies the authorization."""
42
43=== modified file 'ubuntu_sso/tests/test_main.py'
44--- ubuntu_sso/tests/test_main.py 2011-01-11 19:13:19 +0000
45+++ ubuntu_sso/tests/test_main.py 2011-01-28 13:50:13 +0000
46@@ -749,30 +749,68 @@
47 self.assertIsInstance(self.client, ubuntu_sso.main.dbus.service.Object)
48
49
50+class FakeCredentials(object):
51+ """A very dummy Credentials object."""
52+
53+ def __init__(self, *a, **kw):
54+ self.find_credentials = lambda *a: defer.succeed(TOKEN)
55+ self.clear_credentials = lambda *a: defer.succeed(None)
56+ self.store_credentials = lambda *a: defer.succeed(None)
57+ self.login = self.register = lambda *a: None
58+
59+
60 class CredentialsManagementRefCountingTestCase(CredentialsManagementTestCase):
61 """Tests for the CredentialsManagement ref counting."""
62
63+ def setUp(self):
64+ super(CredentialsManagementRefCountingTestCase, self).setUp()
65+ self.patch(ubuntu_sso.main, 'Credentials', FakeCredentials)
66+
67 def test_ref_counting(self):
68 """Ref counting is in place."""
69 self.assertEqual(self.client.ref_count, 0)
70
71 def test_find_credentials(self):
72 """Keep proper track of on going requests."""
73+ d = Deferred()
74+
75+ def verify(*args):
76+ """Make the check."""
77+ self.assertEqual(self.client.ref_count, 1)
78+ d.callback(True)
79+
80+ self.patch(self.client, 'CredentialsFound', verify)
81 self.client.find_credentials(APP_NAME, self.args)
82
83- self.assertEqual(self.client.ref_count, 1)
84+ return d
85
86 def test_clear_credentials(self):
87 """Keep proper track of on going requests."""
88+ d = Deferred()
89+
90+ def verify(*args):
91+ """Make the check."""
92+ self.assertEqual(self.client.ref_count, 1)
93+ d.callback(True)
94+
95+ self.patch(self.client, 'CredentialsCleared', verify)
96 self.client.clear_credentials(APP_NAME, self.args)
97
98- self.assertEqual(self.client.ref_count, 1)
99+ return d
100
101 def test_store_credentials(self):
102 """Keep proper track of on going requests."""
103+ d = Deferred()
104+
105+ def verify(*args):
106+ """Make the check."""
107+ self.assertEqual(self.client.ref_count, 1)
108+ d.callback(True)
109+
110+ self.patch(self.client, 'CredentialsStored', verify)
111 self.client.store_credentials(APP_NAME, self.args)
112
113- self.assertEqual(self.client.ref_count, 1)
114+ return d
115
116 def test_register(self):
117 """Keep proper track of on going requests."""
118@@ -789,10 +827,10 @@
119 def test_several_requests(self):
120 """Requests can be nested."""
121 self.client.login(APP_NAME, self.args)
122- self.client.clear_credentials(APP_NAME, self.args)
123- self.client.find_credentials(APP_NAME, self.args)
124- self.client.register(APP_NAME, self.args)
125- self.client.store_credentials(APP_NAME, self.args)
126+ self.client.register(APP_NAME, self.args)
127+ self.client.login(APP_NAME, self.args)
128+ self.client.register(APP_NAME, self.args)
129+ self.client.register(APP_NAME, self.args)
130
131 self.assertEqual(self.client.ref_count, 5)
132
133@@ -895,19 +933,54 @@
134 def test_on_zero_ref_count_shutdown(self):
135 """When ref count reaches 0, queue shutdown op."""
136 self.client.timeout_func = self._set_called
137- self.client.find_credentials(APP_NAME, self.args)
138+ self.client.login(APP_NAME, self.args)
139 self.client.CredentialsFound(APP_NAME, TOKEN)
140
141 self.assertEqual(self._called,
142- ((TIMEOUT_INTERVAL, self.client.shutdown_func), {}))
143+ ((TIMEOUT_INTERVAL, self.client.shutdown), {}))
144
145 def test_on_non_zero_ref_count_do_not_shutdown(self):
146 """If ref count is not 0, do not queue shutdown op."""
147 self.client.timeout_func = self._set_called
148- self.client.find_credentials(APP_NAME, self.args)
149+ self.client.login(APP_NAME, self.args)
150
151 self.assertEqual(self._called, False)
152
153+ def test_on_non_zero_ref_count_after_zero_do_not_shutdown(self):
154+ """If the shutdown was queued, do not quit if counter is not zero."""
155+
156+ def fake_timeout_func(interval, func):
157+ """Start a new request when the timer is started."""
158+ self.client.register(APP_NAME, self.args)
159+ assert self.client.ref_count > 0
160+ func()
161+
162+ self.client.timeout_func = fake_timeout_func
163+ self.client.shutdown_func = self._set_called
164+
165+ self.client.login(APP_NAME, self.args)
166+ self.client.CredentialsFound(APP_NAME, TOKEN)
167+ # counter reached 0, timeout_func was called
168+
169+ self.assertEqual(self._called, False, 'shutdown_func was not called')
170+
171+ def test_zero_ref_count_after_zero_do_shutdown(self):
172+ """If the shutdown was queued, do quit if counter is zero."""
173+
174+ def fake_timeout_func(interval, func):
175+ """Start a new request when the timer is started."""
176+ assert self.client.ref_count == 0
177+ func()
178+
179+ self.client.timeout_func = fake_timeout_func
180+ self.client.shutdown_func = self._set_called
181+
182+ self.client.login(APP_NAME, self.args)
183+ self.client.CredentialsFound(APP_NAME, TOKEN)
184+ # counter reached 0, timeout_func was called
185+
186+ self.assertEqual(self._called, ((), {}), 'shutdown_func was called')
187+
188
189 class CredentialsManagementFindTestCase(CredentialsManagementTestCase):
190 """Tests for the CredentialsManagement find method."""

Subscribers

People subscribed via source and target branches