Debian Codegraph @

tests/test-batching.py

raw
1 # test-batching.py - tests for transparent command batching
2 #
3 # Copyright 2011 Peter Arrenbrecht <[email protected]>
4 #
5 # This software may be used and distributed according to the terms of the
6 # GNU General Public License version 2 or any later version.
7
8
9 import contextlib
10
11 from mercurial import (
12 localrepo,
13 pycompat,
14 wireprotov1peer,
15 )
16
17
18 def bprint(*bs):
19 print(*[pycompat.sysstr(b) for b in bs])
20
21
22 # equivalent of repo.repository
23 class thing:
24 def hello(self):
25 return b"Ready."
26
27
28 # equivalent of localrepo.localrepository
29 class localthing(thing):
30 def foo(self, one, two=None):
31 if one:
32 return b"%s and %s" % (
33 one,
34 two,
35 )
36 return b"Nope"
37
38 def bar(self, b, a):
39 return b"%s und %s" % (
40 b,
41 a,
42 )
43
44 def greet(self, name=None):
45 return b"Hello, %s" % name
46
47 @contextlib.contextmanager
48 def commandexecutor(self):
49 e = localrepo.localcommandexecutor(self)
50 try:
51 yield e
52 finally:
53 e.close()
54
55
56 # usage of "thing" interface
57 def use(it):
58 # Direct call to base method shared between client and server.
59 bprint(it.hello())
60
61 # Direct calls to proxied methods. They cause individual roundtrips.
62 bprint(it.foo(b"Un", two=b"Deux"))
63 bprint(it.bar(b"Eins", b"Zwei"))
64
65 # Batched call to a couple of proxied methods.
66
67 with it.commandexecutor() as e:
68 ffoo = e.callcommand(b'foo', {b'one': b'One', b'two': b'Two'})
69 fbar = e.callcommand(b'bar', {b'b': b'Eins', b'a': b'Zwei'})
70 fbar2 = e.callcommand(b'bar', {b'b': b'Uno', b'a': b'Due'})
71
72 bprint(ffoo.result())
73 bprint(fbar.result())
74 bprint(fbar2.result())
75
76
77 # local usage
78 mylocal = localthing()
79 print()
80 bprint(b"== Local")
81 use(mylocal)
82
83 # demo remoting; mimicks what wireproto and HTTP/SSH do
84
85 # shared
86
87
88 def escapearg(plain):
89 return (
90 plain.replace(b':', b'::')
91 .replace(b',', b':,')
92 .replace(b';', b':;')
93 .replace(b'=', b':=')
94 )
95
96
97 def unescapearg(escaped):
98 return (
99 escaped.replace(b':=', b'=')
100 .replace(b':;', b';')
101 .replace(b':,', b',')
102 .replace(b'::', b':')
103 )
104
105
106 # server side
107
108
109 # equivalent of wireproto's global functions
110 class server:
111 def __init__(self, local):
112 self.local = local
113
114 def _call(self, name, args):
115 args = dict(arg.split(b'=', 1) for arg in args)
116 return getattr(self, name)(**args)
117
118 def perform(self, req):
119 bprint(b"REQ:", req)
120 name, args = req.split(b'?', 1)
121 args = args.split(b'&')
122 vals = dict(arg.split(b'=', 1) for arg in args)
123 res = getattr(self, pycompat.sysstr(name))(**pycompat.strkwargs(vals))
124 bprint(b" ->", res)
125 return res
126
127 def batch(self, cmds):
128 res = []
129 for pair in cmds.split(b';'):
130 name, args = pair.split(b':', 1)
131 vals = {}
132 for a in args.split(b','):
133 if a:
134 n, v = a.split(b'=')
135 vals[n] = unescapearg(v)
136 res.append(
137 escapearg(
138 getattr(self, pycompat.sysstr(name))(
139 **pycompat.strkwargs(vals)
140 )
141 )
142 )
143 return b';'.join(res)
144
145 def foo(self, one, two):
146 return mangle(self.local.foo(unmangle(one), unmangle(two)))
147
148 def bar(self, b, a):
149 return mangle(self.local.bar(unmangle(b), unmangle(a)))
150
151 def greet(self, name):
152 return mangle(self.local.greet(unmangle(name)))
153
154
155 myserver = server(mylocal)
156
157 # local side
158
159
160 # equivalent of wireproto.encode/decodelist, that is, type-specific marshalling
161 # here we just transform the strings a bit to check we're properly en-/decoding
162 def mangle(s):
163 return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
164
165
166 def unmangle(s):
167 return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
168
169
170 # equivalent of wireproto.wirerepository and something like http's wire format
171 class remotething(thing):
172 def __init__(self, server):
173 self.server = server
174
175 def _submitone(self, name, args):
176 req = name + b'?' + b'&'.join([b'%s=%s' % (n, v) for n, v in args])
177 return self.server.perform(req)
178
179 def _submitbatch(self, cmds):
180 req = []
181 for name, args in cmds:
182 args = b','.join(n + b'=' + escapearg(v) for n, v in args)
183 req.append(name + b':' + args)
184 req = b';'.join(req)
185 res = self._submitone(
186 b'batch',
187 [
188 (
189 b'cmds',
190 req,
191 )
192 ],
193 )
194 yield from res.split(b';')
195
196 @contextlib.contextmanager
197 def commandexecutor(self):
198 e = wireprotov1peer.peerexecutor(self)
199 try:
200 yield e
201 finally:
202 e.close()
203
204 @wireprotov1peer.batchable
205 def foo(self, one, two=None):
206 encoded_args = [
207 (
208 b'one',
209 mangle(one),
210 ),
211 (
212 b'two',
213 mangle(two),
214 ),
215 ]
216 return encoded_args, unmangle
217
218 @wireprotov1peer.batchable
219 def bar(self, b, a):
220 return [
221 (
222 b'b',
223 mangle(b),
224 ),
225 (
226 b'a',
227 mangle(a),
228 ),
229 ], unmangle
230
231 # greet is coded directly. It therefore does not support batching. If it
232 # does appear in a batch, the batch is split around greet, and the call to
233 # greet is done in its own roundtrip.
234 def greet(self, name=None):
235 return unmangle(
236 self._submitone(
237 b'greet',
238 [
239 (
240 b'name',
241 mangle(name),
242 )
243 ],
244 )
245 )
246
247
248 # demo remote usage
249
250 myproxy = remotething(myserver)
251 print()
252 bprint(b"== Remote")
253 use(myproxy)