changeset 849:c5a8f602662b

plugin room_game, radiocol: RoomGame.send returns a Deferred.
author souliane <souliane@mailoo.org>
date Tue, 18 Feb 2014 17:49:33 +0100
parents d40306d1da70
children 30fd34309949
files src/plugins/plugin_misc_radiocol.py src/plugins/plugin_misc_room_game.py src/test/helpers.py src/test/test_plugin_misc_radiocol.py
diffstat 4 files changed, 16 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_radiocol.py	Tue Feb 18 16:24:19 2014 +0100
+++ b/src/plugins/plugin_misc_radiocol.py	Tue Feb 18 17:49:33 2014 +0100
@@ -22,7 +22,7 @@
 from twisted.words.xish import domish
 from twisted.internet import reactor
 from twisted.words.protocols.jabber import jid
-from twisted.internet import threads, defer
+from twisted.internet import defer
 from sat.core import exceptions
 import os.path
 import copy
@@ -89,7 +89,9 @@
         """This method is called by libervia when a song has been uploaded
         @param referee: JID of the referee in the room (room userhost + '/' + nick)
         @song_path: absolute path of the song added
-        @param profile_key: %(doc_profile_key)s"""
+        @param profile_key: %(doc_profile_key)s
+        @return: a Deferred instance
+        """
         #XXX: this is a Q&D way for the proof of concept. In the future, the song should
         #     be streamed to the backend using XMPP file copy
         #     Here we cheat because we know we are on the same host, and we don't
@@ -111,14 +113,7 @@
                  }
         radio_data = self.games[jid.JID(referee).userhost()]  # FIXME: referee comes from Libervia's client side, it's unsecure
         radio_data['to_delete'][attrs['filename']] = song_path  # FIXME: works only because of the same host trick, see the note under the docstring
-
-        # XXX: avoid deferToThread which is causing testing troubles. When using deferToThread,
-        # the callbacks that are added (by the test) to the Deferred instance returned by this
-        # method are not run. And if you run d.callback again, you get a AlreadyCalledError.
-        d = defer.Deferred()
-        d.addCallback(self.send, ('', 'song_added'), attrs, profile=profile)
-        d.callback(jid.JID(referee))
-        return d
+        return self.send(jid.JID(referee), ('', 'song_added'), attrs, profile=profile)
 
     def playNext(self, room_jid, profile):
         """"Play next song in queue if exists, and put a timer
--- a/src/plugins/plugin_misc_room_game.py	Tue Feb 18 16:24:19 2014 +0100
+++ b/src/plugins/plugin_misc_room_game.py	Tue Feb 18 17:49:33 2014 +0100
@@ -21,6 +21,7 @@
 from logging import debug, warning, error
 from twisted.words.protocols.jabber.jid import JID
 from twisted.words.xish import domish
+from twisted.internet import defer
 from time import time
 from wokkel import disco, iwokkel
 from zope.interface import implements
@@ -617,10 +618,11 @@
         - attrs: dictionary of attributes for the new child
         - content: unicode that is appended to the child content
         @param profile: the profile from which the message is sent
+        @return: a Deferred instance
         """
         if profile is None:
             error(_("Message can not be sent without a sender profile"))
-            return
+            return defer.fail(None)
         msg = self._createGameElt(to_jid)
         for elem, attrs, content in data:
             if elem is not None:
@@ -633,6 +635,7 @@
                 if content is not None:
                     elem.addContent(content)
         self.host.profiles[profile].xmlstream.send(msg)
+        return defer.succeed(None)
 
     def send(self, to_jid, elem=None, attrs=None, content=None, profile=None):
         """
@@ -644,8 +647,9 @@
         @param attrs: dictionary of attributes for the new child
         @param content: unicode that is appended to the child content
         @param profile: the profile from which the message is sent
+        @return: a Deferred instance
         """
-        self._sendElements(to_jid, [(elem, attrs, content)], profile)
+        return self._sendElements(to_jid, [(elem, attrs, content)], profile)
 
     def getHandler(self, profile):
         return RoomGameHandler(self)
--- a/src/test/helpers.py	Tue Feb 18 16:24:19 2014 +0100
+++ b/src/test/helpers.py	Tue Feb 18 17:49:33 2014 +0100
@@ -313,6 +313,7 @@
     def send(self, obj):
         """Save the sent messages to compare them later"""
         self.sent.append(obj)
+        return defer.succeed(None)
 
 
 class FakeClient(object):
@@ -327,7 +328,7 @@
         self.xmlstream = FakeXmlStream()
 
     def send(self, obj):
-        self.xmlstream.send(obj)
+        return self.xmlstream.send(obj)
 
 
 class SatTestCase(unittest.TestCase):
--- a/src/test/test_plugin_misc_radiocol.py	Tue Feb 18 16:24:19 2014 +0100
+++ b/src/test/test_plugin_misc_radiocol.py	Tue Feb 18 17:49:33 2014 +0100
@@ -30,6 +30,7 @@
 from twisted.words.protocols.jabber.jid import JID
 from twisted.words.xish import domish
 from twisted.internet import reactor
+from twisted.internet import defer
 from twisted.python.failure import Failure
 
 from mutagen.oggvorbis import OggVorbis
@@ -381,6 +382,8 @@
         for filename in self.playlist:
             self.plugin.deleteFile('/tmp/' + filename)
 
+        return defer.succeed(None)
+
     def tearDown(self, *args, **kwargs):
         """Clean the reactor"""
         helpers.SatTestCase.tearDown(self, *args, **kwargs)