comparison tests/test-batching.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents b81ca9a3f4e4
children 89a2afe31e82
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
13 localrepo, 13 localrepo,
14 pycompat, 14 pycompat,
15 wireprotov1peer, 15 wireprotov1peer,
16 ) 16 )
17 17
18
18 def bprint(*bs): 19 def bprint(*bs):
19 print(*[pycompat.sysstr(b) for b in bs]) 20 print(*[pycompat.sysstr(b) for b in bs])
21
20 22
21 # equivalent of repo.repository 23 # equivalent of repo.repository
22 class thing(object): 24 class thing(object):
23 def hello(self): 25 def hello(self):
24 return b"Ready." 26 return b"Ready."
27
25 28
26 # equivalent of localrepo.localrepository 29 # equivalent of localrepo.localrepository
27 class localthing(thing): 30 class localthing(thing):
28 def foo(self, one, two=None): 31 def foo(self, one, two=None):
29 if one: 32 if one:
30 return b"%s and %s" % (one, two,) 33 return b"%s and %s" % (one, two,)
31 return b"Nope" 34 return b"Nope"
35
32 def bar(self, b, a): 36 def bar(self, b, a):
33 return b"%s und %s" % (b, a,) 37 return b"%s und %s" % (b, a,)
38
34 def greet(self, name=None): 39 def greet(self, name=None):
35 return b"Hello, %s" % name 40 return b"Hello, %s" % name
36 41
37 @contextlib.contextmanager 42 @contextlib.contextmanager
38 def commandexecutor(self): 43 def commandexecutor(self):
40 try: 45 try:
41 yield e 46 yield e
42 finally: 47 finally:
43 e.close() 48 e.close()
44 49
50
45 # usage of "thing" interface 51 # usage of "thing" interface
46 def use(it): 52 def use(it):
47 53
48 # Direct call to base method shared between client and server. 54 # Direct call to base method shared between client and server.
49 bprint(it.hello()) 55 bprint(it.hello())
60 fbar2 = e.callcommand(b'bar', {b'b': b'Uno', b'a': b'Due'}) 66 fbar2 = e.callcommand(b'bar', {b'b': b'Uno', b'a': b'Due'})
61 67
62 bprint(ffoo.result()) 68 bprint(ffoo.result())
63 bprint(fbar.result()) 69 bprint(fbar.result())
64 bprint(fbar2.result()) 70 bprint(fbar2.result())
71
65 72
66 # local usage 73 # local usage
67 mylocal = localthing() 74 mylocal = localthing()
68 print() 75 print()
69 bprint(b"== Local") 76 bprint(b"== Local")
71 78
72 # demo remoting; mimicks what wireproto and HTTP/SSH do 79 # demo remoting; mimicks what wireproto and HTTP/SSH do
73 80
74 # shared 81 # shared
75 82
83
76 def escapearg(plain): 84 def escapearg(plain):
77 return (plain 85 return (
78 .replace(b':', b'::') 86 plain.replace(b':', b'::')
79 .replace(b',', b':,') 87 .replace(b',', b':,')
80 .replace(b';', b':;') 88 .replace(b';', b':;')
81 .replace(b'=', b':=')) 89 .replace(b'=', b':=')
90 )
91
92
82 def unescapearg(escaped): 93 def unescapearg(escaped):
83 return (escaped 94 return (
84 .replace(b':=', b'=') 95 escaped.replace(b':=', b'=')
85 .replace(b':;', b';') 96 .replace(b':;', b';')
86 .replace(b':,', b',') 97 .replace(b':,', b',')
87 .replace(b'::', b':')) 98 .replace(b'::', b':')
99 )
100
88 101
89 # server side 102 # server side
90 103
91 # equivalent of wireproto's global functions 104 # equivalent of wireproto's global functions
92 class server(object): 105 class server(object):
93 def __init__(self, local): 106 def __init__(self, local):
94 self.local = local 107 self.local = local
108
95 def _call(self, name, args): 109 def _call(self, name, args):
96 args = dict(arg.split(b'=', 1) for arg in args) 110 args = dict(arg.split(b'=', 1) for arg in args)
97 return getattr(self, name)(**args) 111 return getattr(self, name)(**args)
112
98 def perform(self, req): 113 def perform(self, req):
99 bprint(b"REQ:", req) 114 bprint(b"REQ:", req)
100 name, args = req.split(b'?', 1) 115 name, args = req.split(b'?', 1)
101 args = args.split(b'&') 116 args = args.split(b'&')
102 vals = dict(arg.split(b'=', 1) for arg in args) 117 vals = dict(arg.split(b'=', 1) for arg in args)
103 res = getattr(self, pycompat.sysstr(name))(**pycompat.strkwargs(vals)) 118 res = getattr(self, pycompat.sysstr(name))(**pycompat.strkwargs(vals))
104 bprint(b" ->", res) 119 bprint(b" ->", res)
105 return res 120 return res
121
106 def batch(self, cmds): 122 def batch(self, cmds):
107 res = [] 123 res = []
108 for pair in cmds.split(b';'): 124 for pair in cmds.split(b';'):
109 name, args = pair.split(b':', 1) 125 name, args = pair.split(b':', 1)
110 vals = {} 126 vals = {}
111 for a in args.split(b','): 127 for a in args.split(b','):
112 if a: 128 if a:
113 n, v = a.split(b'=') 129 n, v = a.split(b'=')
114 vals[n] = unescapearg(v) 130 vals[n] = unescapearg(v)
115 res.append(escapearg(getattr(self, pycompat.sysstr(name))( 131 res.append(
116 **pycompat.strkwargs(vals)))) 132 escapearg(
133 getattr(self, pycompat.sysstr(name))(
134 **pycompat.strkwargs(vals)
135 )
136 )
137 )
117 return b';'.join(res) 138 return b';'.join(res)
139
118 def foo(self, one, two): 140 def foo(self, one, two):
119 return mangle(self.local.foo(unmangle(one), unmangle(two))) 141 return mangle(self.local.foo(unmangle(one), unmangle(two)))
142
120 def bar(self, b, a): 143 def bar(self, b, a):
121 return mangle(self.local.bar(unmangle(b), unmangle(a))) 144 return mangle(self.local.bar(unmangle(b), unmangle(a)))
145
122 def greet(self, name): 146 def greet(self, name):
123 return mangle(self.local.greet(unmangle(name))) 147 return mangle(self.local.greet(unmangle(name)))
148
149
124 myserver = server(mylocal) 150 myserver = server(mylocal)
125 151
126 # local side 152 # local side
127 153
128 # equivalent of wireproto.encode/decodelist, that is, type-specific marshalling 154 # equivalent of wireproto.encode/decodelist, that is, type-specific marshalling
129 # here we just transform the strings a bit to check we're properly en-/decoding 155 # here we just transform the strings a bit to check we're properly en-/decoding
130 def mangle(s): 156 def mangle(s):
131 return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s)) 157 return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
158
159
132 def unmangle(s): 160 def unmangle(s):
133 return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s)) 161 return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
162
134 163
135 # equivalent of wireproto.wirerepository and something like http's wire format 164 # equivalent of wireproto.wirerepository and something like http's wire format
136 class remotething(thing): 165 class remotething(thing):
137 def __init__(self, server): 166 def __init__(self, server):
138 self.server = server 167 self.server = server
168
139 def _submitone(self, name, args): 169 def _submitone(self, name, args):
140 req = name + b'?' + b'&'.join([b'%s=%s' % (n, v) for n, v in args]) 170 req = name + b'?' + b'&'.join([b'%s=%s' % (n, v) for n, v in args])
141 return self.server.perform(req) 171 return self.server.perform(req)
172
142 def _submitbatch(self, cmds): 173 def _submitbatch(self, cmds):
143 req = [] 174 req = []
144 for name, args in cmds: 175 for name, args in cmds:
145 args = b','.join(n + b'=' + escapearg(v) for n, v in args) 176 args = b','.join(n + b'=' + escapearg(v) for n, v in args)
146 req.append(name + b':' + args) 177 req.append(name + b':' + args)
174 # does appear in a batch, the batch is split around greet, and the call to 205 # does appear in a batch, the batch is split around greet, and the call to
175 # greet is done in its own roundtrip. 206 # greet is done in its own roundtrip.
176 def greet(self, name=None): 207 def greet(self, name=None):
177 return unmangle(self._submitone(b'greet', [(b'name', mangle(name),)])) 208 return unmangle(self._submitone(b'greet', [(b'name', mangle(name),)]))
178 209
210
179 # demo remote usage 211 # demo remote usage
180 212
181 myproxy = remotething(myserver) 213 myproxy = remotething(myserver)
182 print() 214 print()
183 bprint(b"== Remote") 215 bprint(b"== Remote")