test-wireproto.py
111 lines
| 2.6 KiB
| text/x-python
|
PythonLexer
/ tests / test-wireproto.py
Pulkit Goyal
|
r28675 | from __future__ import absolute_import, print_function | ||
Gregory Szorc
|
r27301 | |||
Augie Fackler
|
r37942 | import sys | ||
timeless
|
r28860 | from mercurial import ( | ||
Gregory Szorc
|
r36091 | error, | ||
Pulkit Goyal
|
r36573 | pycompat, | ||
Augie Fackler
|
r36962 | ui as uimod, | ||
timeless
|
r28861 | util, | ||
Gregory Szorc
|
r36091 | wireprototypes, | ||
Gregory Szorc
|
r37632 | wireprotov1peer, | ||
Gregory Szorc
|
r37803 | wireprotov1server, | ||
timeless
|
r28860 | ) | ||
Augie Fackler
|
r37942 | from mercurial.utils import ( | ||
stringutil, | ||||
) | ||||
timeless
|
r28861 | stringio = util.stringio | ||
Thomas Arendsen Hein
|
r14765 | |||
class proto(object): | ||||
def __init__(self, args): | ||||
self.args = args | ||||
Gregory Szorc
|
r37311 | self.name = 'dummyproto' | ||
Thomas Arendsen Hein
|
r14765 | def getargs(self, spec): | ||
args = self.args | ||||
Pulkit Goyal
|
r36573 | args.setdefault(b'*', {}) | ||
Thomas Arendsen Hein
|
r14765 | names = spec.split() | ||
return [args[n] for n in names] | ||||
Gregory Szorc
|
r36819 | def checkperm(self, perm): | ||
pass | ||||
Gregory Szorc
|
r37311 | wireprototypes.TRANSPORTS['dummyproto'] = { | ||
'transport': 'dummy', | ||||
'version': 1, | ||||
} | ||||
Gregory Szorc
|
r37632 | class clientpeer(wireprotov1peer.wirepeer): | ||
Augie Fackler
|
r36962 | def __init__(self, serverrepo, ui): | ||
Thomas Arendsen Hein
|
r14765 | self.serverrepo = serverrepo | ||
Gregory Szorc
|
r37337 | self.ui = ui | ||
Gregory Szorc
|
r33805 | |||
def url(self): | ||||
Pulkit Goyal
|
r36573 | return b'test' | ||
Gregory Szorc
|
r33805 | |||
def local(self): | ||||
return None | ||||
def peer(self): | ||||
return self | ||||
def canpush(self): | ||||
return True | ||||
def close(self): | ||||
pass | ||||
def capabilities(self): | ||||
Pulkit Goyal
|
r36573 | return [b'batch'] | ||
Augie Fackler
|
r25912 | |||
Thomas Arendsen Hein
|
r14765 | def _call(self, cmd, **args): | ||
Pulkit Goyal
|
r36573 | args = pycompat.byteskwargs(args) | ||
Gregory Szorc
|
r37803 | res = wireprotov1server.dispatch(self.serverrepo, proto(args), cmd) | ||
Gregory Szorc
|
r36091 | if isinstance(res, wireprototypes.bytesresponse): | ||
return res.data | ||||
elif isinstance(res, bytes): | ||||
return res | ||||
else: | ||||
raise error.Abort('dummy client does not support response type') | ||||
Thomas Arendsen Hein
|
r14765 | |||
Augie Fackler
|
r28438 | def _callstream(self, cmd, **args): | ||
timeless
|
r28861 | return stringio(self._call(cmd, **args)) | ||
Augie Fackler
|
r28438 | |||
Gregory Szorc
|
r37632 | @wireprotov1peer.batchable | ||
Thomas Arendsen Hein
|
r14765 | def greet(self, name): | ||
Gregory Szorc
|
r37632 | f = wireprotov1peer.future() | ||
Pulkit Goyal
|
r36573 | yield {b'name': mangle(name)}, f | ||
Thomas Arendsen Hein
|
r14765 | yield unmangle(f.value) | ||
class serverrepo(object): | ||||
def greet(self, name): | ||||
Pulkit Goyal
|
r36573 | return b"Hello, " + name | ||
Thomas Arendsen Hein
|
r14765 | |||
Pierre-Yves David
|
r18278 | def filtered(self, name): | ||
return self | ||||
Thomas Arendsen Hein
|
r14765 | def mangle(s): | ||
Pulkit Goyal
|
r36573 | return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s)) | ||
Thomas Arendsen Hein
|
r14765 | def unmangle(s): | ||
Pulkit Goyal
|
r36573 | return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s)) | ||
Thomas Arendsen Hein
|
r14765 | |||
def greet(repo, proto, name): | ||||
return mangle(repo.greet(unmangle(name))) | ||||
Gregory Szorc
|
r37803 | wireprotov1server.commands[b'greet'] = (greet, b'name') | ||
Thomas Arendsen Hein
|
r14765 | |||
srv = serverrepo() | ||||
Augie Fackler
|
r36962 | clt = clientpeer(srv, uimod.ui()) | ||
Thomas Arendsen Hein
|
r14765 | |||
Augie Fackler
|
r37942 | def printb(data, end=b'\n'): | ||
out = getattr(sys.stdout, 'buffer', sys.stdout) | ||||
out.write(data + end) | ||||
out.flush() | ||||
printb(clt.greet(b"Foobar")) | ||||
Gregory Szorc
|
r37651 | |||
with clt.commandexecutor() as e: | ||||
fgreet1 = e.callcommand(b'greet', {b'name': b'Fo, =;:<o'}) | ||||
fgreet2 = e.callcommand(b'greet', {b'name': b'Bar'}) | ||||
Augie Fackler
|
r37942 | printb(stringutil.pprint([f.result() for f in (fgreet1, fgreet2)])) | ||