test-batching.py
184 lines
| 5.5 KiB
| text/x-python
|
PythonLexer
/ tests / test-batching.py
|
r14621 | # test-batching.py - tests for transparent command batching | ||
# | ||||
# Copyright 2011 Peter Arrenbrecht <peter@arrenbrecht.ch> | ||||
# | ||||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
|
r28732 | from __future__ import absolute_import, print_function | ||
|
r28800 | |||
|
r37651 | import contextlib | ||
|
r28800 | from mercurial import ( | ||
|
r37633 | localrepo, | ||
|
r41366 | pycompat, | ||
|
r37632 | wireprotov1peer, | ||
|
r41366 | ) | ||
|
r37633 | |||
|
r41366 | def bprint(*bs): | ||
print(*[pycompat.sysstr(b) for b in bs]) | ||||
|
r14621 | |||
# equivalent of repo.repository | ||||
class thing(object): | ||||
def hello(self): | ||||
|
r41366 | return b"Ready." | ||
|
r14621 | |||
# equivalent of localrepo.localrepository | ||||
class localthing(thing): | ||||
def foo(self, one, two=None): | ||||
if one: | ||||
|
r41366 | return b"%s and %s" % (one, two,) | ||
return b"Nope" | ||||
|
r14621 | def bar(self, b, a): | ||
|
r41366 | return b"%s und %s" % (b, a,) | ||
|
r14621 | def greet(self, name=None): | ||
|
r41366 | return b"Hello, %s" % name | ||
|
r37651 | |||
@contextlib.contextmanager | ||||
def commandexecutor(self): | ||||
e = localrepo.localcommandexecutor(self) | ||||
try: | ||||
yield e | ||||
finally: | ||||
e.close() | ||||
|
r14621 | |||
# usage of "thing" interface | ||||
def use(it): | ||||
# Direct call to base method shared between client and server. | ||||
|
r41366 | bprint(it.hello()) | ||
|
r14621 | |||
# Direct calls to proxied methods. They cause individual roundtrips. | ||||
|
r41366 | bprint(it.foo(b"Un", two=b"Deux")) | ||
bprint(it.bar(b"Eins", b"Zwei")) | ||||
|
r14621 | |||
|
r33761 | # Batched call to a couple of proxied methods. | ||
|
r37651 | with it.commandexecutor() as e: | ||
|
r41366 | ffoo = e.callcommand(b'foo', {b'one': b'One', b'two': b'Two'}) | ||
fbar = e.callcommand(b'bar', {b'b': b'Eins', b'a': b'Zwei'}) | ||||
fbar2 = e.callcommand(b'bar', {b'b': b'Uno', b'a': b'Due'}) | ||||
|
r14621 | |||
|
r41366 | bprint(ffoo.result()) | ||
bprint(fbar.result()) | ||||
bprint(fbar2.result()) | ||||
|
r33761 | |||
|
r14621 | # local usage | ||
mylocal = localthing() | ||||
|
r28732 | print() | ||
|
r41366 | bprint(b"== Local") | ||
|
r14621 | use(mylocal) | ||
# demo remoting; mimicks what wireproto and HTTP/SSH do | ||||
# shared | ||||
def escapearg(plain): | ||||
return (plain | ||||
|
r41366 | .replace(b':', b'::') | ||
.replace(b',', b':,') | ||||
.replace(b';', b':;') | ||||
.replace(b'=', b':=')) | ||||
|
r14621 | def unescapearg(escaped): | ||
return (escaped | ||||
|
r41366 | .replace(b':=', b'=') | ||
.replace(b':;', b';') | ||||
.replace(b':,', b',') | ||||
.replace(b'::', b':')) | ||||
|
r14621 | |||
# server side | ||||
# equivalent of wireproto's global functions | ||||
|
r14764 | class server(object): | ||
|
r14621 | def __init__(self, local): | ||
self.local = local | ||||
def _call(self, name, args): | ||||
|
r41366 | args = dict(arg.split(b'=', 1) for arg in args) | ||
|
r14621 | return getattr(self, name)(**args) | ||
def perform(self, req): | ||||
|
r41366 | bprint(b"REQ:", req) | ||
name, args = req.split(b'?', 1) | ||||
args = args.split(b'&') | ||||
vals = dict(arg.split(b'=', 1) for arg in args) | ||||
res = getattr(self, pycompat.sysstr(name))(**pycompat.strkwargs(vals)) | ||||
bprint(b" ->", res) | ||||
|
r14621 | return res | ||
def batch(self, cmds): | ||||
res = [] | ||||
|
r41366 | for pair in cmds.split(b';'): | ||
name, args = pair.split(b':', 1) | ||||
|
r14621 | vals = {} | ||
|
r41366 | for a in args.split(b','): | ||
|
r14621 | if a: | ||
|
r41366 | n, v = a.split(b'=') | ||
|
r14621 | vals[n] = unescapearg(v) | ||
|
r41366 | res.append(escapearg(getattr(self, pycompat.sysstr(name))( | ||
**pycompat.strkwargs(vals)))) | ||||
return b';'.join(res) | ||||
|
r14621 | def foo(self, one, two): | ||
return mangle(self.local.foo(unmangle(one), unmangle(two))) | ||||
def bar(self, b, a): | ||||
return mangle(self.local.bar(unmangle(b), unmangle(a))) | ||||
def greet(self, name): | ||||
return mangle(self.local.greet(unmangle(name))) | ||||
myserver = server(mylocal) | ||||
# local side | ||||
# equivalent of wireproto.encode/decodelist, that is, type-specific marshalling | ||||
# here we just transform the strings a bit to check we're properly en-/decoding | ||||
def mangle(s): | ||||
|
r41366 | return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s)) | ||
|
r14621 | def unmangle(s): | ||
|
r41366 | return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s)) | ||
|
r14621 | |||
# equivalent of wireproto.wirerepository and something like http's wire format | ||||
class remotething(thing): | ||||
def __init__(self, server): | ||||
self.server = server | ||||
def _submitone(self, name, args): | ||||
|
r41366 | req = name + b'?' + b'&'.join([b'%s=%s' % (n, v) for n, v in args]) | ||
|
r14621 | return self.server.perform(req) | ||
def _submitbatch(self, cmds): | ||||
req = [] | ||||
for name, args in cmds: | ||||
|
r41366 | args = b','.join(n + b'=' + escapearg(v) for n, v in args) | ||
req.append(name + b':' + args) | ||||
req = b';'.join(req) | ||||
res = self._submitone(b'batch', [(b'cmds', req,)]) | ||||
for r in res.split(b';'): | ||||
|
r33761 | yield r | ||
|
r14621 | |||
|
r37651 | @contextlib.contextmanager | ||
def commandexecutor(self): | ||||
e = wireprotov1peer.peerexecutor(self) | ||||
try: | ||||
yield e | ||||
finally: | ||||
e.close() | ||||
|
r14621 | |||
|
r37633 | @wireprotov1peer.batchable | ||
|
r14621 | def foo(self, one, two=None): | ||
|
r41366 | encargs = [(b'one', mangle(one),), (b'two', mangle(two),)] | ||
|
r37633 | encresref = wireprotov1peer.future() | ||
|
r14621 | yield encargs, encresref | ||
yield unmangle(encresref.value) | ||||
|
r37633 | @wireprotov1peer.batchable | ||
|
r14621 | def bar(self, b, a): | ||
|
r37633 | encresref = wireprotov1peer.future() | ||
|
r41366 | yield [(b'b', mangle(b),), (b'a', mangle(a),)], encresref | ||
|
r14621 | yield unmangle(encresref.value) | ||
# greet is coded directly. It therefore does not support batching. If it | ||||
# does appear in a batch, the batch is split around greet, and the call to | ||||
# greet is done in its own roundtrip. | ||||
def greet(self, name=None): | ||||
|
r41366 | return unmangle(self._submitone(b'greet', [(b'name', mangle(name),)])) | ||
|
r14621 | |||
# demo remote usage | ||||
myproxy = remotething(myserver) | ||||
|
r28732 | print() | ||
|
r41366 | bprint(b"== Remote") | ||
|
r14621 | use(myproxy) | ||