[telepathy-gabble/telepathy-gabble-0.8] test-avatar-async.py: test the timer: gabble should reemit the vCard request after waiting
Alban Crequy
alban.crequy at collabora.co.uk
Thu Sep 24 10:05:37 PDT 2009
---
tests/twisted/vcard/test-avatar-async.py | 107 ++++++++++++++++++++----------
1 files changed, 73 insertions(+), 34 deletions(-)
diff --git a/tests/twisted/vcard/test-avatar-async.py b/tests/twisted/vcard/test-avatar-async.py
index 40619ec..e97a52a 100644
--- a/tests/twisted/vcard/test-avatar-async.py
+++ b/tests/twisted/vcard/test-avatar-async.py
@@ -13,27 +13,24 @@ from gabbletest import (exec_test, acknowledge_iq, make_result_iq,
import constants as cs
import ns
-def test(q, bus, conn, stream):
- conn.Connect()
- _, iq_event = q.expect_many(
- EventPattern('dbus-signal', signal='StatusChanged',
- args=[cs.CONN_STATUS_CONNECTED, cs.CSR_REQUESTED]),
- EventPattern('stream-iq', to=None, query_ns='vcard-temp',
- query_name='vCard'))
+avatar_retrieved_event = EventPattern('dbus-signal',
+ signal='AvatarRetrieved')
+avatar_request_event = EventPattern('stream-iq', query_ns='vcard-temp')
- acknowledge_iq(stream, iq_event.stanza)
-
- handle = conn.RequestHandles(1, ['bob at foo.com'])[0]
+def test_get_avatar(q, bus, conn, stream, contact, handle, in_cache=False):
conn.Avatars.RequestAvatars([handle])
- iq_event = q.expect('stream-iq', to='bob at foo.com', query_ns='vcard-temp',
- query_name='vCard')
- iq = make_result_iq(stream, iq_event.stanza)
- vcard = iq.firstChildElement()
- photo = vcard.addElement('PHOTO')
- photo.addElement('TYPE', content='image/png')
- photo.addElement('BINVAL', content=base64.b64encode('hello'))
- stream.send(iq)
+ if in_cache:
+ q.forbid_events([avatar_request_event])
+ else:
+ iq_event = q.expect('stream-iq', to=contact, query_ns='vcard-temp',
+ query_name='vCard')
+ iq = make_result_iq(stream, iq_event.stanza)
+ vcard = iq.firstChildElement()
+ photo = vcard.addElement('PHOTO')
+ photo.addElement('TYPE', content='image/png')
+ photo.addElement('BINVAL', content=base64.b64encode('hello'))
+ stream.send(iq)
event = q.expect('dbus-signal', signal='AvatarRetrieved')
assert event.args[0] == handle
@@ -41,38 +38,80 @@ def test(q, bus, conn, stream):
assert event.args[2] == 'hello'
assert event.args[3] == 'image/png'
- # Request again; this request should be satisfied from the avatar cache.
- conn.Avatars.RequestAvatars([handle])
+ if in_cache:
+ sync_stream(q, stream)
+ q.unforbid_events([avatar_request_event])
- event = q.demand('dbus-signal', signal='AvatarRetrieved')
- assert event.args[0] == handle
- assert event.args[1] == hashlib.sha1('hello').hexdigest()
- assert event.args[2] == 'hello'
- assert event.args[3] == 'image/png'
+def test(q, bus, conn, stream):
+ conn.Connect()
+ _, iq_event = q.expect_many(
+ EventPattern('dbus-signal', signal='StatusChanged',
+ args=[cs.CONN_STATUS_CONNECTED, cs.CSR_REQUESTED]),
+ EventPattern('stream-iq', to=None, query_ns='vcard-temp',
+ query_name='vCard'))
+
+ acknowledge_iq(stream, iq_event.stanza)
+
+ # Request on the first contact. Test the cache.
+ handle = conn.RequestHandles(1, ['bob at foo.com'])[0]
+ test_get_avatar(q, bus, conn, stream, 'bob at foo.com', handle,
+ in_cache=False)
+ test_get_avatar(q, bus, conn, stream, 'bob at foo.com', handle,
+ in_cache=True)
# Request another vCard and get resource-constraint
- handle = conn.RequestHandles(1, ['jean at busy-server.com'])[0]
- conn.Avatars.RequestAvatars([handle])
+ busy_contact = 'jean at busy-server.com'
+ busy_handle = conn.RequestHandles(1, [busy_contact])[0]
+ conn.Avatars.RequestAvatars([busy_handle])
- iq_event = q.expect('stream-iq', to='jean at busy-server.com', query_ns='vcard-temp',
+ iq_event = q.expect('stream-iq', to=busy_contact, query_ns='vcard-temp',
query_name='vCard')
iq = iq_event.stanza
error = domish.Element((None, 'error'))
error['code'] = '500'
error['type'] = 'wait'
error.addElement((ns.STANZA, 'resource-constraint'))
- send_error_reply(stream, iq, error)
- avatar_retrieved_event = EventPattern('dbus-signal',
- signal='AvatarRetrieved')
- avatar_request_event = EventPattern('stream-iq', query_ns='vcard-temp')
q.forbid_events([avatar_retrieved_event, avatar_request_event])
+ send_error_reply(stream, iq, error)
- # Request the same vCard again during the delay
- conn.Avatars.RequestAvatars([handle])
+ # Request the same vCard again during the suspended delay
+ # We should not get the avatar
+ conn.Avatars.RequestAvatars([busy_handle])
sync_stream(q, stream)
sync_dbus(bus, q, conn)
q.unforbid_events([avatar_retrieved_event, avatar_request_event])
+ # Request on a different contact, on another server
+ # We should get the avatar
+ handle = conn.RequestHandles(1, ['bob2 at foo.com'])[0]
+ test_get_avatar(q, bus, conn, stream, 'bob2 at foo.com', handle)
+
+ # Try again the contact on the busy server.
+ # We should not get the avatar
+ # Note: the timeout is 3 seconds for the test suites. We assume that
+ # a few stanza with be processed fast enough to avoid the race.
+ q.forbid_events([avatar_retrieved_event, avatar_request_event])
+ conn.Avatars.RequestAvatars([busy_handle])
+ sync_stream(q, stream)
+ sync_dbus(bus, q, conn)
+ q.unforbid_events([avatar_retrieved_event, avatar_request_event])
+
+ # After 3 seconds, we receive a new vCard request on the busy server
+ iq_event = q.expect('stream-iq', to=busy_contact, query_ns='vcard-temp',
+ query_name='vCard')
+ iq = make_result_iq(stream, iq_event.stanza)
+ vcard = iq.firstChildElement()
+ photo = vcard.addElement('PHOTO')
+ photo.addElement('TYPE', content='image/png')
+ photo.addElement('BINVAL', content=base64.b64encode('hello'))
+ stream.send(iq)
+
+ event = q.expect('dbus-signal', signal='AvatarRetrieved')
+ assert event.args[0] == busy_handle
+ assert event.args[1] == hashlib.sha1('hello').hexdigest()
+ assert event.args[2] == 'hello'
+ assert event.args[3] == 'image/png'
+
if __name__ == '__main__':
exec_test(test)
--
1.5.6.5
More information about the telepathy-commits
mailing list