test-wireproto.py
80 lines
| 1.7 KiB
| text/x-python
|
PythonLexer
/ tests / test-wireproto.py
|
r28675 | from __future__ import absolute_import, print_function | ||
|
r27301 | |||
|
r28860 | from mercurial import ( | ||
|
r28861 | util, | ||
|
r28860 | wireproto, | ||
) | ||||
|
r28861 | stringio = util.stringio | ||
|
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] | ||||
|
r17192 | class clientpeer(wireproto.wirepeer): | ||
|
r14765 | def __init__(self, serverrepo): | ||
self.serverrepo = serverrepo | ||||
|
r25912 | |||
|
r33805 | @property | ||
def ui(self): | ||||
return self.serverrepo.ui | ||||
def url(self): | ||||
return 'test' | ||||
def local(self): | ||||
return None | ||||
def peer(self): | ||||
return self | ||||
def canpush(self): | ||||
return True | ||||
def close(self): | ||||
pass | ||||
def capabilities(self): | ||||
|
r25912 | return ['batch'] | ||
|
r14765 | def _call(self, cmd, **args): | ||
return wireproto.dispatch(self.serverrepo, proto(args), cmd) | ||||
|
r28438 | def _callstream(self, cmd, **args): | ||
|
r28861 | return stringio(self._call(cmd, **args)) | ||
|
r28438 | |||
|
r14765 | @wireproto.batchable | ||
def greet(self, name): | ||||
f = wireproto.future() | ||||
|
r20686 | yield {'name': mangle(name)}, f | ||
|
r14765 | yield unmangle(f.value) | ||
class serverrepo(object): | ||||
def greet(self, name): | ||||
return "Hello, " + name | ||||
|
r18278 | def filtered(self, name): | ||
return self | ||||
|
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() | ||||
|
r17192 | clt = clientpeer(srv) | ||
|
r14765 | |||
|
r28675 | print(clt.greet("Foobar")) | ||
|
r33762 | b = clt.iterbatch() | ||
map(b.greet, ('Fo, =;:<o', 'Bar')) | ||||
|
r14765 | b.submit() | ||
|
r33762 | print([r for r in b.results()]) | ||