source: ralphm-patches/component_multiple.patch @ 12:fc40892815eb

Last change on this file since 12:fc40892815eb was 2:ec684efc6a7b, checked in by Ralph Meijer <ralphm@…>, 13 years ago

Allow multiple domains per internal component.

File size: 5.2 KB
RevLine 
[2]1diff -r 5de8d8c9e331 wokkel/component.py
2--- a/wokkel/component.py       Thu Mar 05 16:29:13 2009 +0100
3+++ b/wokkel/component.py       Thu Mar 05 19:41:28 2009 +0100
4@@ -85,12 +85,18 @@
5     Instead of opening a socket to connect to a router, like L{Component},
6     components of this type connect to a router in the same process. This
7     allows for one-process XMPP servers.
8+
9+    @ivar domains: Domains (as C{str}) this component will handle traffic for.
10+    @type domains: L{set}
11     """
12 
13-    def __init__(self, router, domain):
14+    def __init__(self, router, domain=None):
15         XMPPHandlerCollection.__init__(self)
16-        self.router = router
17-        self.domain = domain
18+
19+        self._router = router
20+        self.domains = set()
21+        if domain:
22+            self.domains.add(domain)
23 
24         self.xmlstream = None
25 
26@@ -100,9 +106,11 @@
27         """
28         service.Service.startService(self)
29 
30-        self.pipe = XmlPipe()
31-        self.xmlstream = self.pipe.source
32-        self.router.addRoute(self.domain, self.pipe.sink)
33+        self._pipe = XmlPipe()
34+        self.xmlstream = self._pipe.source
35+
36+        for domain in self.domains:
37+            self._router.addRoute(domain, self._pipe.sink)
38 
39         for e in self:
40             e.makeConnection(self.xmlstream)
41@@ -115,8 +123,10 @@
42         """
43         service.Service.stopService(self)
44 
45-        self.router.removeRoute(self.domain, self.pipe.sink)
46-        self.pipe = None
47+        for domain in self.domains:
48+            self._router.removeRoute(domain, self._pipe.sink)
49+
50+        self._pipe = None
51         self.xmlstream = None
52 
53         for e in self:
54diff -r 5de8d8c9e331 wokkel/test/test_component.py
55--- a/wokkel/test/test_component.py     Thu Mar 05 16:29:13 2009 +0100
56+++ b/wokkel/test/test_component.py     Thu Mar 05 19:41:28 2009 +0100
57@@ -41,11 +41,43 @@
58         verifyObject(IXMPPHandlerCollection, self.component)
59 
60 
61-    def test_startService(self):
62+
63+    def test_startServiceRunning(self):
64         """
65-        Starting the service creates a new route and hooks up handlers.
66+        Starting the service makes it running.
67         """
68+        self.assertFalse(self.component.running)
69+        self.component.startService()
70+        self.assertTrue(self.component.running)
71 
72+
73+    def test_startServiceAddRoute(self):
74+        """
75+        Starting the service creates a new route.
76+        """
77+        self.component.startService()
78+        self.assertIn('component', self.router.routes)
79+
80+
81+    def test_startServiceNoDomain(self):
82+        self.component = component.InternalComponent(self.router)
83+        self.component.startService()
84+
85+
86+    def test_startServiceAddMultipleRoutes(self):
87+        """
88+        Starting the service creates a new route.
89+        """
90+        self.component.domains.add('component2')
91+        self.component.startService()
92+        self.assertIn('component', self.router.routes)
93+        self.assertIn('component2', self.router.routes)
94+
95+
96+    def test_startServiceHandlerDispatch(self):
97+        """
98+        Starting the service hooks up handlers.
99+        """
100         events = []
101 
102         class TestHandler(XMPPHandler):
103@@ -56,23 +88,51 @@
104 
105         TestHandler().setHandlerParent(self.component)
106 
107-        self.assertFalse(self.component.running)
108-
109         self.component.startService()
110-
111-        self.assertTrue(self.component.running)
112-        self.assertIn('component', self.router.routes)
113-
114         self.assertEquals([], events)
115         self.component.xmlstream.dispatch(None, '//event/test')
116         self.assertEquals([None], events)
117 
118 
119-    def test_stopService(self):
120+    def test_stopServiceNotRunning(self):
121         """
122-        Stopping the service removes the route and disconnects handlers.
123+        Stopping the service makes it not running.
124         """
125+        self.component.startService()
126+        self.component.stopService()
127+        self.assertFalse(self.component.running)
128 
129+
130+    def test_stopServiceRemoveRoute(self):
131+        """
132+        Stopping the service removes routes.
133+        """
134+        self.component.startService()
135+        self.component.stopService()
136+        self.assertNotIn('component', self.router.routes)
137+
138+
139+    def test_stopServiceNoDomain(self):
140+        self.component = component.InternalComponent(self.router)
141+        self.component.startService()
142+        self.component.stopService()
143+
144+
145+    def test_startServiceRemoveMultipleRoutes(self):
146+        """
147+        Starting the service creates a new route.
148+        """
149+        self.component.domains.add('component2')
150+        self.component.startService()
151+        self.component.stopService()
152+        self.assertNotIn('component', self.router.routes)
153+        self.assertNotIn('component2', self.router.routes)
154+
155+
156+    def test_stopServiceHandlerDispatch(self):
157+        """
158+        Stopping the service disconnects handlers.
159+        """
160         events = []
161 
162         class TestHandler(XMPPHandler):
163@@ -84,10 +144,7 @@
164 
165         self.component.startService()
166         self.component.stopService()
167-
168-        self.assertFalse(self.component.running)
169         self.assertEquals(1, len(events))
170-        self.assertNotIn('component', self.router.routes)
171 
172 
173     def test_addHandler(self):
Note: See TracBrowser for help on using the repository browser.