Merge lp:~mattia.barbon/txsymmetricjsonrpc/free-yajl-handle into lp:txsymmetricjsonrpc

Proposed by Mattia Barbon
Status: Needs review
Proposed branch: lp:~mattia.barbon/txsymmetricjsonrpc/free-yajl-handle
Merge into: lp:txsymmetricjsonrpc
Diff against target: 132 lines (+39/-8)
5 files modified
txsymmetricjsonrpc/protocol.py (+11/-4)
txsymmetricjsonrpc/proxy.py (+7/-0)
txsymmetricjsonrpc/test/test_proxy.py (+4/-0)
txsymmetricjsonrpc/test/test_v20.py (+4/-0)
txsymmetricjsonrpc/test/utils.py (+13/-4)
To merge this branch: bzr merge lp:~mattia.barbon/txsymmetricjsonrpc/free-yajl-handle
Reviewer Review Type Date Requested Status
Manuel de la Peña Pending
Review via email: mp+62091@code.launchpad.net

Description of the change

Free the YAJL parser handle when the connection is closed.
Cancel pending JSON-RPC calls when the connection is closed.
Call loseConnection() in test tearDown() method.

To post a comment you must log in.

Unmerged revisions

5. By Mattia Barbon

Close the mocked connection at the end of each test.

4. By Mattia Barbon

Don't try to dispatch objects if there was a parse error.

3. By Mattia Barbon

Cancel pending JSON-RPC calls when closing the connection.

2. By Mattia Barbon

Free the JSON parser when closing the connection.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'txsymmetricjsonrpc/protocol.py'
2--- txsymmetricjsonrpc/protocol.py 2011-05-19 10:03:52 +0000
3+++ txsymmetricjsonrpc/protocol.py 2011-05-24 08:55:36 +0000
4@@ -51,9 +51,20 @@
5
6 self.stream = jsonr.Stream()
7
8+ def connectionLost(self, reason):
9+ if self.stream:
10+ self.stream.close()
11+ self.stream = None
12+
13+ protocol.Protocol.connectionLost(self, reason)
14+
15 def dataReceived(self, data):
16 try:
17 self.stream.addData(data)
18+
19+ # TODO maybe dispatch using a deferred
20+ while self.stream.hasObject():
21+ self._dispatch(self.stream.nextObject())
22 except jsonr.ParseError:
23 self.sendError(None, PARSE_ERROR, 'Parse error')
24 self.transport.loseConnection()
25@@ -61,10 +72,6 @@
26 self.sendError(None, INTERNAL_ERROR, 'Internal error')
27 self.transport.loseConnection()
28
29- # TODO maybe dispatch using a deferred
30- while self.stream.hasObject():
31- self._dispatch(self.stream.nextObject())
32-
33 def _dispatch(self, obj):
34 if isinstance(obj, list):
35 if len(obj) > 0:
36
37=== modified file 'txsymmetricjsonrpc/proxy.py'
38--- txsymmetricjsonrpc/proxy.py 2011-05-19 10:03:52 +0000
39+++ txsymmetricjsonrpc/proxy.py 2011-05-24 08:55:36 +0000
40@@ -58,6 +58,13 @@
41
42 protocol.JsonRpcProtocol.makeConnection(self, transport)
43
44+ def connectionLost(self, reason):
45+ for d in self.pending.values():
46+ d.cancel()
47+ self.pending = {}
48+
49+ protocol.JsonRpcProtocol.connectionLost(self, reason)
50+
51 def jsonCallReceived(self, request_id, method, parameters):
52 deferred = defer.Deferred()
53 deferred.addCallback(lambda _: self._dispatchMethod(method, parameters))
54
55=== modified file 'txsymmetricjsonrpc/test/test_proxy.py'
56--- txsymmetricjsonrpc/test/test_proxy.py 2011-05-18 09:59:30 +0000
57+++ txsymmetricjsonrpc/test/test_proxy.py 2011-05-24 08:55:36 +0000
58@@ -63,6 +63,10 @@
59 self.server = EchoServer()
60 self.connection = utils.LoopbackConnection(self.client, self.server)
61
62+ def tearDown(self):
63+ self.client.transport.loseConnection()
64+ self.server.transport.loseConnection()
65+
66 def assertFaultEquals(self, f1, f2):
67 if f1.faultCode == f2.faultCode and f1.faultString == f2.faultString:
68 return
69
70=== modified file 'txsymmetricjsonrpc/test/test_v20.py'
71--- txsymmetricjsonrpc/test/test_v20.py 2011-05-19 10:00:23 +0000
72+++ txsymmetricjsonrpc/test/test_v20.py 2011-05-24 08:55:36 +0000
73@@ -26,6 +26,10 @@
74 def setUp(self):
75 self.p = utils.mockProtocol()
76
77+ def tearDown(self):
78+ if not self.p.transport.disconnected:
79+ self.p.transport.loseConnection()
80+
81
82 class CallTest(Base):
83 def testRecv_withIdParams(self):
84
85=== modified file 'txsymmetricjsonrpc/test/utils.py'
86--- txsymmetricjsonrpc/test/utils.py 2011-05-18 17:51:46 +0000
87+++ txsymmetricjsonrpc/test/utils.py 2011-05-24 08:55:36 +0000
88@@ -24,9 +24,10 @@
89
90
91 class MockTransport(object):
92- def __init__(self):
93+ def __init__(self, protocol):
94 self.data = []
95 self.disconnected = False
96+ self.protocol = protocol
97
98 def write(self, data):
99 # this assumes the implementation sends a message in a single write
100@@ -34,14 +35,21 @@
101
102 def loseConnection(self, reason=None):
103 self.disconnected = True
104-
105+ self.protocol.connectionLost(reason)
106+ del self.protocol
107
108 class LoopbackTransport(object):
109 def __init__(self, proto):
110 self.proto = proto
111
112 def write(self, data):
113- reactor.callLater(0, self.proto.dataReceived, data)
114+ if self.proto:
115+ reactor.callLater(0, self.proto.dataReceived, data)
116+
117+ def loseConnection(self, reason=None):
118+ if self.proto:
119+ self.proto.connectionLost(reason)
120+ self.proto = None
121
122
123 class LoopbackConnection(object):
124@@ -77,6 +85,7 @@
125
126 def mockProtocol():
127 proto = MockProtocol()
128- proto.makeConnection(MockTransport())
129+ transport = MockTransport(proto)
130+ proto.makeConnection(transport)
131
132 return proto

Subscribers

People subscribed via source and target branches