source: ralphm-patches/connectionLost-reason.patch @ 47:f6d222b68f1c

Last change on this file since 47:f6d222b68f1c was 47:f6d222b68f1c, checked in by Ralph Meijer <ralphm@…>, 11 years ago

Many new patches, upstreamed affiliations and worked on pubsub item.
Upstreamed pubsub_manage_affiliations

File size: 3.5 KB
RevLine 
[47]1# HG changeset patch
2# Parent 28a4dc8ad569c665b609533feb1823aa5241f7e4
3Track change in Twisted Words, to pass a reason Failure with STREAM_END_EVENT.
4
5diff -r 28a4dc8ad569 wokkel/subprotocols.py
6--- a/wokkel/subprotocols.py    Mon Feb 28 20:38:27 2011 +0100
7+++ b/wokkel/subprotocols.py    Mon Feb 28 20:40:04 2011 +0100
8@@ -10,7 +10,8 @@
9 from zope.interface import implements
10 
11 from twisted.internet import defer
12-from twisted.python import log
13+from twisted.internet.error import ConnectionDone
14+from twisted.python import failure, log
15 from twisted.words.protocols.jabber import error, xmlstream
16 from twisted.words.protocols.jabber.xmlstream import toResponse
17 from twisted.words.xish import xpath
18@@ -246,7 +247,7 @@
19         """
20 
21 
22-    def _disconnected(self, _):
23+    def _disconnected(self, reason):
24         """
25         Called when the stream has been closed.
26 
27@@ -257,10 +258,14 @@
28         self.xmlstream = None
29         self._initialized = False
30 
31+        # Twisted versions before 11.0 passed an XmlStream here.
32+        if not hasattr(reason, 'trap'):
33+            reason = failure.Failure(ConnectionDone())
34+
35         # Notify all child services which implement
36         # the IService interface
37         for e in self:
38-            e.connectionLost(None)
39+            e.connectionLost(reason)
40 
41 
42     def send(self, obj):
43diff -r 28a4dc8ad569 wokkel/test/test_subprotocols.py
44--- a/wokkel/test/test_subprotocols.py  Mon Feb 28 20:38:27 2011 +0100
45+++ b/wokkel/test/test_subprotocols.py  Mon Feb 28 20:40:04 2011 +0100
46@@ -10,6 +10,7 @@
47 from twisted.trial import unittest
48 from twisted.test import proto_helpers
49 from twisted.internet import defer
50+from twisted.python import failure
51 from twisted.words.xish import domish
52 from twisted.words.protocols.jabber import error, xmlstream
53 
54@@ -55,6 +56,20 @@
55 
56 
57 
58+class FailureReasonXMPPHandler(subprotocols.XMPPHandler):
59+    """
60+    Dummy handler specifically for failure Reason tests.
61+    """
62+    def __init__(self):
63+        self.gotFailureReason = False
64+
65+
66+    def connectionLost(self, reason):
67+        if isinstance(reason, failure.Failure):
68+            self.gotFailureReason = True
69+
70+
71+
72 class XMPPHandlerTest(unittest.TestCase):
73     """
74     Tests for L{subprotocols.XMPPHandler}.
75@@ -234,19 +249,30 @@
76 
77     def test_disconnected(self):
78         """
79-        Test that protocol handlers have their connectionLost method
80-        called when the XML stream is disconnected.
81+        Protocol handlers have connectionLost called on stream disconnect.
82         """
83         sm = self.streamManager
84         handler = DummyXMPPHandler()
85         handler.setHandlerParent(sm)
86-        xs = xmlstream.XmlStream(xmlstream.Authenticator())
87-        sm._disconnected(xs)
88+        sm._disconnected(None)
89         self.assertEquals(0, handler.doneMade)
90         self.assertEquals(0, handler.doneInitialized)
91         self.assertEquals(1, handler.doneLost)
92 
93 
94+    def test_disconnectedReason(self):
95+        """
96+        A L{STREAM_END_EVENT} results in L{StreamManager} firing the handlers
97+        L{connectionLost} methods, passing a L{failure.Failure} reason.
98+        """
99+        sm = self.streamManager
100+        handler = FailureReasonXMPPHandler()
101+        handler.setHandlerParent(sm)
102+        xs = xmlstream.XmlStream(xmlstream.Authenticator())
103+        sm._disconnected(failure.Failure(Exception("no reason")))
104+        self.assertEquals(True, handler.gotFailureReason)
105+
106+
107     def test_addHandler(self):
108         """
109         Test the addition of a protocol handler while not connected.
Note: See TracBrowser for help on using the repository browser.