test-wireproto.py
61 lines
| 1.4 KiB
| text/x-python
|
PythonLexer
/ tests / test-wireproto.py
Pulkit Goyal
|
r28675 | from __future__ import absolute_import, print_function | ||
Gregory Szorc
|
r27301 | |||
timeless
|
r28860 | from mercurial import ( | ||
timeless
|
r28861 | util, | ||
timeless
|
r28860 | wireproto, | ||
) | ||||
timeless
|
r28861 | stringio = util.stringio | ||
Thomas Arendsen Hein
|
r14765 | |||
class proto(object): | ||||
def __init__(self, args): | ||||
self.args = args | ||||
def getargs(self, spec): | ||||
args = self.args | ||||
args.setdefault('*', {}) | ||||
names = spec.split() | ||||
return [args[n] for n in names] | ||||
Peter Arrenbrecht
|
r17192 | class clientpeer(wireproto.wirepeer): | ||
Thomas Arendsen Hein
|
r14765 | def __init__(self, serverrepo): | ||
self.serverrepo = serverrepo | ||||
Augie Fackler
|
r25912 | |||
def _capabilities(self): | ||||
return ['batch'] | ||||
Thomas Arendsen Hein
|
r14765 | def _call(self, cmd, **args): | ||
return wireproto.dispatch(self.serverrepo, proto(args), cmd) | ||||
Augie Fackler
|
r28438 | def _callstream(self, cmd, **args): | ||
timeless
|
r28861 | return stringio(self._call(cmd, **args)) | ||
Augie Fackler
|
r28438 | |||
Thomas Arendsen Hein
|
r14765 | @wireproto.batchable | ||
def greet(self, name): | ||||
f = wireproto.future() | ||||
Augie Fackler
|
r20686 | yield {'name': mangle(name)}, f | ||
Thomas Arendsen Hein
|
r14765 | yield unmangle(f.value) | ||
class serverrepo(object): | ||||
def greet(self, name): | ||||
return "Hello, " + name | ||||
Pierre-Yves David
|
r18278 | def filtered(self, name): | ||
return self | ||||
Thomas Arendsen Hein
|
r14765 | def mangle(s): | ||
return ''.join(chr(ord(c) + 1) for c in s) | ||||
def unmangle(s): | ||||
return ''.join(chr(ord(c) - 1) for c in s) | ||||
def greet(repo, proto, name): | ||||
return mangle(repo.greet(unmangle(name))) | ||||
wireproto.commands['greet'] = (greet, 'name',) | ||||
srv = serverrepo() | ||||
Peter Arrenbrecht
|
r17192 | clt = clientpeer(srv) | ||
Thomas Arendsen Hein
|
r14765 | |||
Pulkit Goyal
|
r28675 | print(clt.greet("Foobar")) | ||
Thomas Arendsen Hein
|
r14765 | b = clt.batch() | ||
Augie Fackler
|
r25708 | fs = [b.greet(s) for s in ["Fo, =;:<o", "Bar"]] | ||
Thomas Arendsen Hein
|
r14765 | b.submit() | ||
Pulkit Goyal
|
r28675 | print([f.value for f in fs]) | ||