tests/test-batching.py
author Peter Arrenbrecht <peter@arrenbrecht.ch>
Mon, 11 May 2020 08:13:40 +0200
branchstable
changeset 44820 f71c8eea7161
parent 43076 2372284d9457
child 45942 89a2afe31e82
permissions -rw-r--r--
bash_completion: do not use aliased hg if it sources a script (issue6308) I have an alias that sources a script around hg. Mercurial's bash_completion script tries to use this as its main hg binary. But sourcing a wrapper breaks Bash's completion. So this patch disables using the alias as the hg binary if it starts with "source ". Alias resolution was introduced in rev 191ab08e7099 for users with "alias hg='hg --some_opts'". See https://www.mercurial-scm.org/repo/hg/rev/191ab08e7099
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     1
# test-batching.py - tests for transparent command batching
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     2
#
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     3
# Copyright 2011 Peter Arrenbrecht <peter@arrenbrecht.ch>
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     4
#
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     5
# This software may be used and distributed according to the terms of the
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     6
# GNU General Public License version 2 or any later version.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
     7
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
     8
from __future__ import absolute_import, print_function
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
     9
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    10
import contextlib
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    11
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
    12
from mercurial import (
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
    13
    localrepo,
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    14
    pycompat,
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33766
diff changeset
    15
    wireprotov1peer,
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    16
)
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
    17
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    18
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    19
def bprint(*bs):
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    20
    print(*[pycompat.sysstr(b) for b in bs])
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    21
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    22
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    23
# equivalent of repo.repository
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    24
class thing(object):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    25
    def hello(self):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    26
        return b"Ready."
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    27
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    28
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    29
# equivalent of localrepo.localrepository
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    30
class localthing(thing):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    31
    def foo(self, one, two=None):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    32
        if one:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    33
            return b"%s and %s" % (one, two,)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    34
        return b"Nope"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    35
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    36
    def bar(self, b, a):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    37
        return b"%s und %s" % (b, a,)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    38
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    39
    def greet(self, name=None):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    40
        return b"Hello, %s" % name
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    41
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    42
    @contextlib.contextmanager
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    43
    def commandexecutor(self):
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    44
        e = localrepo.localcommandexecutor(self)
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    45
        try:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    46
            yield e
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    47
        finally:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    48
            e.close()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    49
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    50
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    51
# usage of "thing" interface
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    52
def use(it):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    53
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    54
    # Direct call to base method shared between client and server.
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    55
    bprint(it.hello())
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    56
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    57
    # Direct calls to proxied methods. They cause individual roundtrips.
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    58
    bprint(it.foo(b"Un", two=b"Deux"))
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    59
    bprint(it.bar(b"Eins", b"Zwei"))
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    60
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
    61
    # Batched call to a couple of proxied methods.
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
    62
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    63
    with it.commandexecutor() as e:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    64
        ffoo = e.callcommand(b'foo', {b'one': b'One', b'two': b'Two'})
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    65
        fbar = e.callcommand(b'bar', {b'b': b'Eins', b'a': b'Zwei'})
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    66
        fbar2 = e.callcommand(b'bar', {b'b': b'Uno', b'a': b'Due'})
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    67
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    68
    bprint(ffoo.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    69
    bprint(fbar.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    70
    bprint(fbar2.result())
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
    71
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    72
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    73
# local usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    74
mylocal = localthing()
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    75
print()
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    76
bprint(b"== Local")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    77
use(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    78
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    79
# demo remoting; mimicks what wireproto and HTTP/SSH do
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    80
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    81
# shared
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    82
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    83
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    84
def escapearg(plain):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    85
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    86
        plain.replace(b':', b'::')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    87
        .replace(b',', b':,')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    88
        .replace(b';', b':;')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    89
        .replace(b'=', b':=')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    90
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    91
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    92
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    93
def unescapearg(escaped):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    94
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    95
        escaped.replace(b':=', b'=')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    96
        .replace(b':;', b';')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    97
        .replace(b':,', b',')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    98
        .replace(b'::', b':')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    99
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   100
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   101
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   102
# server side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   103
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   104
# equivalent of wireproto's global functions
14764
a7d5816087a9 classes: fix class style problems found by b071cd58af50
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14621
diff changeset
   105
class server(object):
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   106
    def __init__(self, local):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   107
        self.local = local
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   108
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   109
    def _call(self, name, args):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   110
        args = dict(arg.split(b'=', 1) for arg in args)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   111
        return getattr(self, name)(**args)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   112
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   113
    def perform(self, req):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   114
        bprint(b"REQ:", req)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   115
        name, args = req.split(b'?', 1)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   116
        args = args.split(b'&')
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   117
        vals = dict(arg.split(b'=', 1) for arg in args)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   118
        res = getattr(self, pycompat.sysstr(name))(**pycompat.strkwargs(vals))
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   119
        bprint(b"  ->", res)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   120
        return res
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   121
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   122
    def batch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   123
        res = []
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   124
        for pair in cmds.split(b';'):
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   125
            name, args = pair.split(b':', 1)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   126
            vals = {}
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   127
            for a in args.split(b','):
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   128
                if a:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   129
                    n, v = a.split(b'=')
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   130
                    vals[n] = unescapearg(v)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   131
            res.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   132
                escapearg(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   133
                    getattr(self, pycompat.sysstr(name))(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   134
                        **pycompat.strkwargs(vals)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   135
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   136
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   137
            )
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   138
        return b';'.join(res)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   139
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   140
    def foo(self, one, two):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   141
        return mangle(self.local.foo(unmangle(one), unmangle(two)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   142
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   143
    def bar(self, b, a):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   144
        return mangle(self.local.bar(unmangle(b), unmangle(a)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   145
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   146
    def greet(self, name):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   147
        return mangle(self.local.greet(unmangle(name)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   148
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   149
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   150
myserver = server(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   151
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   152
# local side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   153
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   154
# equivalent of wireproto.encode/decodelist, that is, type-specific marshalling
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   155
# here we just transform the strings a bit to check we're properly en-/decoding
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   156
def mangle(s):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   157
    return b''.join(pycompat.bytechr(ord(c) + 1) for c in pycompat.bytestr(s))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   158
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   159
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   160
def unmangle(s):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   161
    return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   162
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   163
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   164
# equivalent of wireproto.wirerepository and something like http's wire format
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   165
class remotething(thing):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   166
    def __init__(self, server):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   167
        self.server = server
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   168
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   169
    def _submitone(self, name, args):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   170
        req = name + b'?' + b'&'.join([b'%s=%s' % (n, v) for n, v in args])
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   171
        return self.server.perform(req)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   172
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   173
    def _submitbatch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   174
        req = []
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   175
        for name, args in cmds:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   176
            args = b','.join(n + b'=' + escapearg(v) for n, v in args)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   177
            req.append(name + b':' + args)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   178
        req = b';'.join(req)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   179
        res = self._submitone(b'batch', [(b'cmds', req,)])
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   180
        for r in res.split(b';'):
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
   181
            yield r
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   182
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   183
    @contextlib.contextmanager
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   184
    def commandexecutor(self):
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   185
        e = wireprotov1peer.peerexecutor(self)
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   186
        try:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   187
            yield e
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   188
        finally:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   189
            e.close()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   190
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   191
    @wireprotov1peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   192
    def foo(self, one, two=None):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   193
        encargs = [(b'one', mangle(one),), (b'two', mangle(two),)]
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   194
        encresref = wireprotov1peer.future()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   195
        yield encargs, encresref
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   196
        yield unmangle(encresref.value)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   197
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   198
    @wireprotov1peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   199
    def bar(self, b, a):
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   200
        encresref = wireprotov1peer.future()
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   201
        yield [(b'b', mangle(b),), (b'a', mangle(a),)], encresref
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   202
        yield unmangle(encresref.value)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   203
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   204
    # greet is coded directly. It therefore does not support batching. If it
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   205
    # does appear in a batch, the batch is split around greet, and the call to
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   206
    # greet is done in its own roundtrip.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   207
    def greet(self, name=None):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   208
        return unmangle(self._submitone(b'greet', [(b'name', mangle(name),)]))
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   209
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   210
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   211
# demo remote usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   212
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   213
myproxy = remotething(myserver)
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   214
print()
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   215
bprint(b"== Remote")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   216
use(myproxy)