Merge lp:~awuerl/txjsonrpc/preserve_result into lp:txjsonrpc

Proposed by Andreas Würl
Status: Needs review
Proposed branch: lp:~awuerl/txjsonrpc/preserve_result
Merge into: lp:txjsonrpc
Diff against target: 76 lines (+19/-1)
2 files modified
txjsonrpc/web/jsonrpc.py (+4/-0)
txjsonrpc/web/test/test_jsonrpc.py (+15/-1)
To merge this branch: bzr merge lp:~awuerl/txjsonrpc/preserve_result
Reviewer Review Type Date Requested Status
Duncan McGreggor Pending
Review via email: mp+230865@code.launchpad.net

Description of the change

The current implementation does not allow to cache deferred results in a simple object cache. The result of any deferred is set to None after the rendering callback web.JSONRPC._cbRender() is called. As _cbRender returns nothing, the original value is discarded.

This minimal patch fixes this behaviour. Deferreds can now be stored/cached and returned multiple times. The original result will always be available to the caller of the service method.

To post a comment you must log in.
lp:~awuerl/txjsonrpc/preserve_result updated
97. By Andreas Würl

added test

Unmerged revisions

97. By Andreas Würl

added test

96. By Andreas Würl

preserve result of deferred

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'txjsonrpc/web/jsonrpc.py'
2--- txjsonrpc/web/jsonrpc.py 2014-02-18 13:29:40 +0000
3+++ txjsonrpc/web/jsonrpc.py 2014-08-14 18:55:54 +0000
4@@ -115,6 +115,8 @@
5 return server.NOT_DONE_YET
6
7 def _cbRender(self, result, request, id, version):
8+ original_result = result
9+
10 if isinstance(result, Handler):
11 result = result.result
12 if version == jsonrpclib.VERSION_PRE1:
13@@ -133,6 +135,8 @@
14 request.write(s)
15 request.finish()
16
17+ return original_result
18+
19 def _ebRender(self, failure, id):
20 if isinstance(failure.value, jsonrpclib.Fault):
21 return failure.value
22
23=== modified file 'txjsonrpc/web/test/test_jsonrpc.py'
24--- txjsonrpc/web/test/test_jsonrpc.py 2012-01-28 01:11:15 +0000
25+++ txjsonrpc/web/test/test_jsonrpc.py 2014-08-14 18:55:54 +0000
26@@ -26,6 +26,11 @@
27 NOT_FOUND = jsonrpclib.METHOD_NOT_FOUND
28 SESSION_EXPIRED = 42
29
30+ def __init__(self):
31+ jsonrpc.JSONRPC.__init__(self)
32+ self.cached_deferred = None
33+
34+
35 def jsonrpc_add(self, a, b):
36 """
37 This function add two numbers.
38@@ -52,6 +57,11 @@
39 # The doc string is part of the test.
40 return defer.succeed(x)
41
42+ def jsonrpc_deferCached(self, x):
43+ if self.cached_deferred is None:
44+ self.cached_deferred = defer.succeed(x)
45+ return self.cached_deferred
46+
47 def jsonrpc_deferFail(self):
48 return defer.fail(TestValueError())
49
50@@ -121,6 +131,8 @@
51 return [
52 5,
53 "a",
54+ "a",
55+ "a",
56 1,
57 ["a", 1],
58 "null",
59@@ -130,6 +142,8 @@
60 input = [
61 ("add", (2, 3)),
62 ("defer", ("a",)),
63+ ("deferCached", ("a",)),
64+ ("deferCached", ("b",)),
65 ("dict", ({"a": 1}, "a")),
66 ("pair", ("a", 1)),
67 ("none", ()),
68@@ -173,7 +187,7 @@
69 meths.sort()
70 self.failUnlessEqual(
71 meths,
72- ['add', 'complex', 'defer', 'deferFail',
73+ ['add', 'complex', 'defer', 'deferCached', 'deferFail',
74 'deferFault', 'dict', 'fail', 'fault',
75 'none', 'pair', 'system.listMethods',
76 'system.methodHelp',

Subscribers

People subscribed via source and target branches

to all changes: