[telepathy-gabble/master] Test calling Stop() in various circumstances

Will Thompson will.thompson at collabora.co.uk
Wed Aug 26 09:21:57 PDT 2009


---
 tests/twisted/search/ceci-nest-pas-un-serveur.py |    8 +
 tests/twisted/search/unextended.py               |  149 ++++++++++++++++------
 2 files changed, 119 insertions(+), 38 deletions(-)

diff --git a/tests/twisted/search/ceci-nest-pas-un-serveur.py b/tests/twisted/search/ceci-nest-pas-un-serveur.py
index 3e926ba..e8c11ca 100644
--- a/tests/twisted/search/ceci-nest-pas-un-serveur.py
+++ b/tests/twisted/search/ceci-nest-pas-un-serveur.py
@@ -90,6 +90,10 @@ def returns_error_from_search(q, stream, conn, requests):
     assert new_state == cs.SEARCH_FAILED, new_state
     assert reason == cs.PERMISSION_DENIED, reason
 
+    # We call stop after the search has failed; it should succeed and do nothing.
+    call_async(q, c_search, 'Stop')
+    event = q.expect('dbus-return', method='Stop')
+
     c.Close()
 
 def returns_bees_from_search(q, stream, conn, requests):
@@ -124,6 +128,10 @@ def returns_bees_from_search(q, stream, conn, requests):
     assert new_state == cs.SEARCH_FAILED, new_state
     assert reason == cs.NOT_AVAILABLE, reason
 
+    # We call stop after the search has failed; it should succeed and do nothing.
+    call_async(q, c_search, 'Stop')
+    event = q.expect('dbus-return', method='Stop')
+
     c.Close()
 
 def disconnected_before_reply(q, stream, conn, requests):
diff --git a/tests/twisted/search/unextended.py b/tests/twisted/search/unextended.py
index b30fcae..40a9643 100644
--- a/tests/twisted/search/unextended.py
+++ b/tests/twisted/search/unextended.py
@@ -7,7 +7,7 @@ import dbus
 
 from twisted.words.protocols.jabber.client import IQ
 
-from gabbletest import exec_test
+from gabbletest import exec_test, sync_stream
 from servicetest import call_async, unwrap, make_channel_proxy, EventPattern
 
 from pprint import pformat
@@ -15,13 +15,26 @@ from pprint import pformat
 import constants as cs
 import ns
 
-server = 'jud.localhost'
+g_jid = 'guybrush.threepwood at lucasarts.example.com'
+f_jid = 'freddiet at pgwodehouse.example.com'
+g_results = (g_jid, 'Guybrush', 'Threepwood', 'Fancy Pants')
+f_results = (f_jid, 'Frederick', 'Threepwood', 'Freddie')
+results = [g_results, f_results]
+
 
 def test(q, bus, conn, stream):
     conn.Connect()
     q.expect('dbus-signal', signal='StatusChanged', args=[0, 1])
 
     requests = dbus.Interface(conn, cs.CONN_IFACE_REQUESTS)
+
+    for f in [complete_search, cancelled_while_in_progress]:
+        f(q, bus, conn, requests, stream, 'jud.localhost')
+
+    conn.Disconnect()
+    q.expect('dbus-signal', signal='StatusChanged', args=[2, 1])
+
+def call_create(q, requests, server):
     request = dbus.Dictionary(
         {
             cs.CHANNEL_TYPE: cs.CHANNEL_TYPE_CONTACT_SEARCH,
@@ -29,6 +42,7 @@ def test(q, bus, conn, stream):
         }, signature='sv')
     call_async(q, requests, 'CreateChannel', request)
 
+def answer_field_query(q, stream, server):
     # Gabble asks the server what search fields it supports
     iq_event = q.expect('stream-iq', to=server, query_ns=ns.SEARCH)
     iq = iq_event.stanza
@@ -43,26 +57,11 @@ def test(q, bus, conn, stream):
     stream.send(result)
 
     ret = q.expect('dbus-return', method='CreateChannel')
-    sig = q.expect('dbus-signal', signal='NewChannels')
+    nc_sig = q.expect('dbus-signal', signal='NewChannels')
 
-    path, props = ret.value
-    props = unwrap(props)
-
-    expected_search_keys = ['email', 'nickname', 'x-n-family', 'x-n-given']
+    return (ret, nc_sig)
 
-    assert props[cs.CONTACT_SEARCH_SERVER] == server, pformat(props)
-    assert sorted(props[cs.CONTACT_SEARCH_ASK]) == expected_search_keys, \
-        pformat(props)
-    assert cs.CONTACT_SEARCH_STATE not in props, pformat(props)
-
-    c = make_channel_proxy(conn, path, 'Channel')
-    c_props = dbus.Interface(c, cs.PROPERTIES_IFACE)
-    c_search = dbus.Interface(c, cs.CHANNEL_TYPE_CONTACT_SEARCH)
-
-    state = c_props.Get(cs.CHANNEL_TYPE_CONTACT_SEARCH, 'SearchState')
-    assert state == cs.SEARCH_NOT_STARTED, state
-
-    # We make a search.
+def make_search(q, c_search, c_props, server):
     terms = { 'x-n-family': 'Threepwood' }
     call_async(q, c_search, 'Search', terms)
 
@@ -77,23 +76,9 @@ def test(q, bus, conn, stream):
     state = c_props.Get(cs.CHANNEL_TYPE_CONTACT_SEARCH, 'SearchState')
     assert state == cs.SEARCH_IN_PROGRESS, state
 
-    iq = iq_event.stanza
-    query = iq.firstChildElement()
-    i = 0
-    for field in query.elements():
-        assert field.name == 'last', field.toXml()
-        assert field.children[0] == u'Threepwood', field.children[0]
-        i += 1
-    assert i == 1, query
-
-    # Server sends the results of the search.
-
-    g_jid = 'guybrush.threepwood at lucasarts.example.com'
-    f_jid = 'freddiet at pgwodehouse.example.com'
-    g_results = (g_jid, 'Guybrush', 'Threepwood', 'Fancy Pants')
-    f_results = (f_jid, 'Frederick', 'Threepwood', 'Freddie')
-    results = [g_results, f_results]
+    return iq_event.stanza
 
+def send_results(stream, iq):
     result = IQ(stream, 'result')
     result['id'] = iq['id']
     query = result.addElement((ns.SEARCH, 'query'))
@@ -106,6 +91,41 @@ def test(q, bus, conn, stream):
         item.addElement('email', content=jid)
     stream.send(result)
 
+def complete_search(q, bus, conn, requests, stream, server):
+    call_create(q, requests, server)
+
+    ret, nc_sig = answer_field_query(q, stream, server)
+
+    path, props = ret.value
+    props = unwrap(props)
+
+    expected_search_keys = ['email', 'nickname', 'x-n-family', 'x-n-given']
+
+    assert props[cs.CONTACT_SEARCH_SERVER] == server, pformat(props)
+    assert sorted(props[cs.CONTACT_SEARCH_ASK]) == expected_search_keys, \
+        pformat(props)
+    assert cs.CONTACT_SEARCH_STATE not in props, pformat(props)
+
+    c = make_channel_proxy(conn, path, 'Channel')
+    c_props = dbus.Interface(c, cs.PROPERTIES_IFACE)
+    c_search = dbus.Interface(c, cs.CHANNEL_TYPE_CONTACT_SEARCH)
+
+    state = c_props.Get(cs.CHANNEL_TYPE_CONTACT_SEARCH, 'SearchState')
+    assert state == cs.SEARCH_NOT_STARTED, state
+
+    # We make a search.
+    iq = make_search(q, c_search, c_props, server)
+    query = iq.firstChildElement()
+    i = 0
+    for field in query.elements():
+        assert field.name == 'last', field.toXml()
+        assert field.children[0] == u'Threepwood', field.children[0]
+        i += 1
+    assert i == 1, query
+
+    # Server sends the results of the search.
+    send_results(stream, iq)
+
     r1 = q.expect('dbus-signal', signal='SearchResultReceived')
     r2 = q.expect('dbus-signal', signal='SearchResultReceived')
 
@@ -127,6 +147,14 @@ def test(q, bus, conn, stream):
     ssc = q.expect('dbus-signal', signal='SearchStateChanged')
     assert ssc.args[0] == cs.SEARCH_COMPLETED, ssc.args
 
+    # We call Stop after the search has completed; it should succeed, but leave
+    # the channel in state Completed rather than changing it to Failed for
+    # reason Cancelled.
+    call_async(q, c_search, 'Stop')
+    event = q.expect('dbus-return', method='Stop')
+    state = c_props.Get(cs.CHANNEL_TYPE_CONTACT_SEARCH, 'SearchState')
+    assert state == cs.SEARCH_COMPLETED, (state, cs.SEARCH_COMPLETED)
+
     c.Close()
 
     q.expect_many(
@@ -139,8 +167,53 @@ def test(q, bus, conn, stream):
         call_async(q, conn, 'InspectHandles', cs.HT_CONTACT, [h])
         q.expect('dbus-error', method='InspectHandles')
 
-    conn.Disconnect()
-    q.expect('dbus-signal', signal='StatusChanged', args=[2, 1])
+def cancelled_while_in_progress(q, bus, conn, requests, stream, server):
+    call_create(q, requests, server)
+
+    ret, _ = answer_field_query(q, stream, server)
+    path, props = ret.value
+
+    c = make_channel_proxy(conn, path, 'Channel')
+    c_props = dbus.Interface(c, cs.PROPERTIES_IFACE)
+    c_search = dbus.Interface(c, cs.CHANNEL_TYPE_CONTACT_SEARCH)
+
+    iq = make_search(q, c_search, c_props, server)
+
+    # Before the server sends back the results, the client cancels the search.
+    call_async(q, c_search, 'Stop')
+    ret, ssc = q.expect_many(
+        EventPattern('dbus-return', method='Stop'),
+        EventPattern('dbus-signal', signal='SearchStateChanged'),
+        )
+
+    assert ssc.args[0] == cs.SEARCH_FAILED, ssc.args
+    assert ssc.args[1] == cs.CANCELLED, ssc.args
+
+    state = c_props.Get(cs.CHANNEL_TYPE_CONTACT_SEARCH, 'SearchState')
+    assert state == cs.SEARCH_FAILED, (state, cs.SEARCH_FAILED)
+
+    # Now the server sends us the results; SearchResultReceived shouldn't fire
+    def srr_cb(chan, contact, vcard):
+        assert False, "SearchResultReceived shouldn't fire after Stop()"
+
+    c_search.connect_to_signal('SearchResultReceived', srr_cb)
+
+    send_results(stream, iq)
+
+    # Make sure Gabble's received the results.
+    sync_stream(q, stream)
+
+    # Hooray! We survived. Now let's call Stop again; it should succeed but do
+    # nothing.
+    def search_state_changed_cb(chan, state, reason, details):
+        assert False, "SearchStateChanged shouldn't fire"
+
+    c_search.connect_to_signal('SearchStateChanged', search_state_changed_cb)
+
+    call_async(q, c_search, 'Stop')
+    ssc = q.expect('dbus-return', method='Stop')
+
+    c.Close()
 
 if __name__ == '__main__':
     exec_test(test)
-- 
1.5.6.5




More information about the telepathy-commits mailing list