tests/test-batching.py
author Raphaël Gomès <rgomes@octobus.net>
Tue, 29 Mar 2022 14:27:45 +0200
changeset 49067 bde2e4ef968a
parent 49037 642e31cb55f0
child 51749 493034cc3265
permissions -rw-r--r--
merge: stable into default
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
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
     8
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
     9
import contextlib
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    10
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
    11
from mercurial import (
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
    12
    localrepo,
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    13
    pycompat,
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33787
diff changeset
    14
    wireprotov1peer,
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    15
)
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
    16
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    17
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    18
def bprint(*bs):
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    19
    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
    20
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    21
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    22
# equivalent of repo.repository
49037
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48966
diff changeset
    23
class thing:
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    24
    def hello(self):
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    25
        return b"Ready."
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    26
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    27
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    28
# equivalent of localrepo.localrepository
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    29
class localthing(thing):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    30
    def foo(self, one, two=None):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    31
        if one:
45957
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    32
            return b"%s and %s" % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    33
                one,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    34
                two,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    35
            )
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    36
        return b"Nope"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    37
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    38
    def bar(self, b, a):
45957
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    39
        return b"%s und %s" % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    40
            b,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    41
            a,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    42
        )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    43
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    44
    def greet(self, name=None):
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    45
        return b"Hello, %s" % name
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    46
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    47
    @contextlib.contextmanager
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    48
    def commandexecutor(self):
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    49
        e = localrepo.localcommandexecutor(self)
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    50
        try:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    51
            yield e
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    52
        finally:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    53
            e.close()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    54
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    55
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    56
# usage of "thing" interface
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    57
def use(it):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    58
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    59
    # Direct call to base method shared between client and server.
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    60
    bprint(it.hello())
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    61
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    62
    # Direct calls to proxied methods. They cause individual roundtrips.
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    63
    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
    64
    bprint(it.bar(b"Eins", b"Zwei"))
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    65
33787
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33786
diff changeset
    66
    # Batched call to a couple of proxied methods.
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33786
diff changeset
    67
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    68
    with it.commandexecutor() as e:
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    69
        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
    70
        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
    71
        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
    72
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    73
    bprint(ffoo.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    74
    bprint(fbar.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    75
    bprint(fbar2.result())
33787
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33786
diff changeset
    76
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    77
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    78
# local usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    79
mylocal = localthing()
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    80
print()
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    81
bprint(b"== Local")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    82
use(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    83
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    84
# 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
    85
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    86
# shared
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    87
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    88
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    89
def escapearg(plain):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    90
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    91
        plain.replace(b':', b'::')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    92
        .replace(b',', b':,')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    93
        .replace(b';', b':;')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    94
        .replace(b'=', b':=')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    95
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    96
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    97
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    98
def unescapearg(escaped):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
    99
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   100
        escaped.replace(b':=', b'=')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   101
        .replace(b':;', b';')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   102
        .replace(b':,', b',')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   103
        .replace(b'::', b':')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   104
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   105
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   106
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   107
# server side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   108
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   109
# equivalent of wireproto's global functions
49037
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48966
diff changeset
   110
class server:
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   111
    def __init__(self, local):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   112
        self.local = local
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   113
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   114
    def _call(self, name, args):
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   115
        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
   116
        return getattr(self, name)(**args)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   117
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   118
    def perform(self, req):
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   119
        bprint(b"REQ:", req)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   120
        name, args = req.split(b'?', 1)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   121
        args = args.split(b'&')
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   122
        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
   123
        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
   124
        bprint(b"  ->", res)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   125
        return res
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   126
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   127
    def batch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   128
        res = []
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   129
        for pair in cmds.split(b';'):
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   130
            name, args = pair.split(b':', 1)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   131
            vals = {}
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   132
            for a in args.split(b','):
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   133
                if a:
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   134
                    n, v = a.split(b'=')
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   135
                    vals[n] = unescapearg(v)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   136
            res.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   137
                escapearg(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   138
                    getattr(self, pycompat.sysstr(name))(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   139
                        **pycompat.strkwargs(vals)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   140
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   141
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   142
            )
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   143
        return b';'.join(res)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   144
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   145
    def foo(self, one, two):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   146
        return mangle(self.local.foo(unmangle(one), unmangle(two)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   147
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   148
    def bar(self, b, a):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   149
        return mangle(self.local.bar(unmangle(b), unmangle(a)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   150
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   151
    def greet(self, name):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   152
        return mangle(self.local.greet(unmangle(name)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   153
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   154
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   155
myserver = server(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   156
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   157
# local side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   158
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   159
# 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
   160
# 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
   161
def mangle(s):
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   162
    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: 41360
diff changeset
   163
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   164
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   165
def unmangle(s):
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   166
    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
   167
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   168
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   169
# 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
   170
class remotething(thing):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   171
    def __init__(self, server):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   172
        self.server = server
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   173
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   174
    def _submitone(self, name, args):
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   175
        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
   176
        return self.server.perform(req)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   177
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   178
    def _submitbatch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   179
        req = []
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   180
        for name, args in cmds:
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   181
            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
   182
            req.append(name + b':' + args)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   183
        req = b';'.join(req)
45957
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   184
        res = self._submitone(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   185
            b'batch',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   186
            [
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   187
                (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   188
                    b'cmds',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   189
                    req,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   190
                )
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   191
            ],
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   192
        )
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   193
        for r in res.split(b';'):
33787
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33786
diff changeset
   194
            yield r
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   195
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   196
    @contextlib.contextmanager
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   197
    def commandexecutor(self):
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   198
        e = wireprotov1peer.peerexecutor(self)
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   199
        try:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   200
            yield e
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   201
        finally:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   202
            e.close()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   203
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   204
    @wireprotov1peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   205
    def foo(self, one, two=None):
46539
05dd091dfa6a wireprotopeer: clarify some variable names now that we allow snake_case
Martin von Zweigbergk <martinvonz@google.com>
parents: 45957
diff changeset
   206
        encoded_args = [
45957
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   207
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   208
                b'one',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   209
                mangle(one),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   210
            ),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   211
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   212
                b'two',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   213
                mangle(two),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   214
            ),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   215
        ]
47914
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46539
diff changeset
   216
        return encoded_args, unmangle
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   217
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   218
    @wireprotov1peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   219
    def bar(self, b, a):
47914
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46539
diff changeset
   220
        return [
45957
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   221
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   222
                b'b',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   223
                mangle(b),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   224
            ),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   225
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   226
                b'a',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   227
                mangle(a),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   228
            ),
47914
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46539
diff changeset
   229
        ], unmangle
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   230
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   231
    # 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
   232
    # 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
   233
    # greet is done in its own roundtrip.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   234
    def greet(self, name=None):
45957
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   235
        return unmangle(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   236
            self._submitone(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   237
                b'greet',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   238
                [
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   239
                    (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   240
                        b'name',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   241
                        mangle(name),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   242
                    )
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   243
                ],
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   244
            )
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   245
        )
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   246
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41360
diff changeset
   247
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   248
# demo remote usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   249
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   250
myproxy = remotething(myserver)
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   251
print()
41360
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   252
bprint(b"== Remote")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   253
use(myproxy)