Mercurial > hg
view tests/test-batching.py @ 46531:d46885119f90
test-copies: don't use empty file for "same content" cases
For main case (using filelog or sidedata), this lead to the following hash
change.
Changesets:
- 01c2f5eabdc4ce2bdee42b5f86311955e6c8f573 → 319179230cc87769ab3a861ebffe7a534ebb3d85
- 01c2f5eabdc4 → 319179230cc8
- c72365ee036fca4fb27fd745459bfb6ea1ac6993 → 6cbc9c2b7b391dd738603173717c601648d3735f
- c72365ee036f → 6cbc9c2b7b39
File revision for `f`:
- 0dd616bc7ab1a111921d95d76f69cda5c2ac539c → cedeacc5bf5d9b9be4d7f8394d33a5349bb29c6e
- 0dd616bc7ab1 → cedeacc5bf5d
- eb806e34ef6be4c264effd5933d31004ad15a793 → ffb76cd765422a18759a335d8a81fa2bd455be6b
- eb806e34ef6b → ffb76cd76542
- 6da5a2eecb9c833f830b67a4972366d49a9a142c → 08d1ff5926fbd0285cdeb044cbe8ab651687e86a
- 6da5a2eecb9c → 08d1ff5926fb
File revision for `d`:
- 7bded9d9da1f7bf9bf7cbfb24fe1e6ccf68ec440 → ba177bbb45ea930ee48469a55d40224537bd57a9
For the "extra in changeset" case we get the following change for file `d`:
- 68d5bca9df0577b6bc2ea30ca724e13ead60da81 → b894de5c94aadcb4894ea7c358389819c27fbcce
- 68d5bca9df05 → b894de5c94aa
- b80de5d138758541c5f05265ad144ab9fa86d1db → 56647659eff080e06e45c18ea9e848836dadea71
- b80de5d13875 → 56647659eff0
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 22 Feb 2021 18:48:45 +0100 |
parents | 05dd091dfa6a |
children | c424ff4807e6 |
line wrap: on
line source
# 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. from __future__ import absolute_import, print_function import contextlib from mercurial import ( localrepo, pycompat, wireprotov1peer, ) def bprint(*bs): print(*[pycompat.sysstr(b) for b in bs]) # equivalent of repo.repository class thing(object): def hello(self): return b"Ready." # equivalent of localrepo.localrepository class localthing(thing): def foo(self, one, two=None): if one: return b"%s and %s" % ( one, two, ) return b"Nope" def bar(self, b, a): return b"%s und %s" % ( b, a, ) def greet(self, name=None): return b"Hello, %s" % name @contextlib.contextmanager def commandexecutor(self): e = localrepo.localcommandexecutor(self) try: yield e finally: e.close() # usage of "thing" interface def use(it): # Direct call to base method shared between client and server. bprint(it.hello()) # Direct calls to proxied methods. They cause individual roundtrips. bprint(it.foo(b"Un", two=b"Deux")) bprint(it.bar(b"Eins", b"Zwei")) # Batched call to a couple of proxied methods. with it.commandexecutor() as e: 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'}) bprint(ffoo.result()) bprint(fbar.result()) bprint(fbar2.result()) # local usage mylocal = localthing() print() bprint(b"== Local") use(mylocal) # demo remoting; mimicks what wireproto and HTTP/SSH do # shared def escapearg(plain): return ( plain.replace(b':', b'::') .replace(b',', b':,') .replace(b';', b':;') .replace(b'=', b':=') ) def unescapearg(escaped): return ( escaped.replace(b':=', b'=') .replace(b':;', b';') .replace(b':,', b',') .replace(b'::', b':') ) # server side # equivalent of wireproto's global functions class server(object): def __init__(self, local): self.local = local def _call(self, name, args): args = dict(arg.split(b'=', 1) for arg in args) return getattr(self, name)(**args) def perform(self, req): 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) return res def batch(self, cmds): res = [] for pair in cmds.split(b';'): name, args = pair.split(b':', 1) vals = {} for a in args.split(b','): if a: n, v = a.split(b'=') vals[n] = unescapearg(v) res.append( escapearg( getattr(self, pycompat.sysstr(name))( **pycompat.strkwargs(vals) ) ) ) return b';'.join(res) 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): return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s)) def unmangle(s): return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s)) # 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): req = name + b'?' + b'&'.join([b'%s=%s' % (n, v) for n, v in args]) return self.server.perform(req) def _submitbatch(self, cmds): req = [] for name, args in cmds: 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';'): yield r @contextlib.contextmanager def commandexecutor(self): e = wireprotov1peer.peerexecutor(self) try: yield e finally: e.close() @wireprotov1peer.batchable def foo(self, one, two=None): encoded_args = [ ( b'one', mangle(one), ), ( b'two', mangle(two), ), ] encoded_res_future = wireprotov1peer.future() yield encoded_args, encoded_res_future yield unmangle(encoded_res_future.value) @wireprotov1peer.batchable def bar(self, b, a): encresref = wireprotov1peer.future() yield [ ( b'b', mangle(b), ), ( b'a', mangle(a), ), ], encresref 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): return unmangle( self._submitone( b'greet', [ ( b'name', mangle(name), ) ], ) ) # demo remote usage myproxy = remotething(myserver) print() bprint(b"== Remote") use(myproxy)