tests/test-batching.py
author Matt Harbison <matt_harbison@yahoo.com>
Mon, 29 Jul 2024 12:10:08 -0400
changeset 51740 7226f2626fb1
parent 51703 ca7bde5dbafb
permissions -rw-r--r--
httppeer: avoid another bad reference before assignment warning This wasn't a problem, because `b''` from the `AttributeError` handler is in `bundle2.bundletypes`, so the following loop and conditional always run at least once. But PyCharm can't figure that out on its own, and it took a little exploring to figure out it wasn't a problem. The usage in `bundle2.writebundle` is to look it up in the map of bundle types, so it will break in a more obvious way in the unlikely event that the empty string is removed from the map in the future.
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,
41335
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: 33766
diff changeset
    14
    wireprotov1peer,
41335
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: 41335
diff changeset
    17
41335
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: 41335
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
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
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):
41335
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: 41335
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:
45942
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
            )
41335
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: 41335
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):
45942
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: 41335
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):
41335
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: 41335
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
    # 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
    59
    bprint(it.hello())
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    60
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    61
    # 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
    62
    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
    63
    bprint(it.bar(b"Eins", b"Zwei"))
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    64
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
    65
    # 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
    66
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    67
    with it.commandexecutor() as e:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    68
        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
    69
        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
    70
        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
    71
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    72
    bprint(ffoo.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    73
    bprint(fbar.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    74
    bprint(fbar2.result())
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
    75
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    76
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    77
# local usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    78
mylocal = localthing()
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    79
print()
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    80
bprint(b"== Local")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    81
use(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    82
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    83
# 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
    84
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    85
# shared
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    86
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    87
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    88
def escapearg(plain):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    89
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    90
        plain.replace(b':', b'::')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    91
        .replace(b',', b':,')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    92
        .replace(b';', b':;')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    93
        .replace(b'=', b':=')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    94
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    95
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    96
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    97
def unescapearg(escaped):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    98
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    99
        escaped.replace(b':=', b'=')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   100
        .replace(b':;', b';')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   101
        .replace(b':,', b',')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   102
        .replace(b'::', b':')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   103
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   104
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   105
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   106
# server side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   107
51703
ca7bde5dbafb black: format the codebase with 23.3.0
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
   108
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   109
# equivalent of wireproto's global functions
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
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: 41335
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):
41335
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: 41335
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):
41335
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: 41335
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 = []
41335
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 = {}
41335
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:
41335
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: 41335
diff changeset
   136
            res.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   137
                escapearg(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   138
                    getattr(self, pycompat.sysstr(name))(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   139
                        **pycompat.strkwargs(vals)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   140
                    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   141
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   142
            )
41335
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: 41335
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: 41335
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: 41335
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: 41335
diff changeset
   153
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
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
51703
ca7bde5dbafb black: format the codebase with 23.3.0
Raphaël Gomès <rgomes@octobus.net>
parents: 51700
diff changeset
   159
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   160
# 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
   161
# 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
   162
def mangle(s):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   163
    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
   164
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   165
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   166
def unmangle(s):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   167
    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
   168
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   169
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   170
# 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
   171
class remotething(thing):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   172
    def __init__(self, server):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   173
        self.server = server
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   174
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   175
    def _submitone(self, name, args):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   176
        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
   177
        return self.server.perform(req)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   178
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   179
    def _submitbatch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   180
        req = []
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   181
        for name, args in cmds:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   182
            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
   183
            req.append(name + b':' + args)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   184
        req = b';'.join(req)
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   185
        res = self._submitone(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   186
            b'batch',
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
                (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   189
                    b'cmds',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   190
                    req,
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
            ],
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   193
        )
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   194
        for r in res.split(b';'):
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
   195
            yield r
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   196
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   197
    @contextlib.contextmanager
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   198
    def commandexecutor(self):
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   199
        e = wireprotov1peer.peerexecutor(self)
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   200
        try:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   201
            yield e
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   202
        finally:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
   203
            e.close()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   204
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   205
    @wireprotov1peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   206
    def foo(self, one, two=None):
46480
05dd091dfa6a wireprotopeer: clarify some variable names now that we allow snake_case
Martin von Zweigbergk <martinvonz@google.com>
parents: 45942
diff changeset
   207
        encoded_args = [
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   208
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   209
                b'one',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   210
                mangle(one),
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
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   213
                b'two',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   214
                mangle(two),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   215
            ),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   216
        ]
47873
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46480
diff changeset
   217
        return encoded_args, unmangle
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   218
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   219
    @wireprotov1peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   220
    def bar(self, b, a):
47873
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46480
diff changeset
   221
        return [
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   222
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   223
                b'b',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   224
                mangle(b),
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
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   227
                b'a',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   228
                mangle(a),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   229
            ),
47873
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 46480
diff changeset
   230
        ], unmangle
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   231
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   232
    # 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
   233
    # 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
   234
    # greet is done in its own roundtrip.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   235
    def greet(self, name=None):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   236
        return unmangle(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   237
            self._submitone(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   238
                b'greet',
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
                    (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   241
                        b'name',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   242
                        mangle(name),
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
            )
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   246
        )
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   247
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   248
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   249
# demo remote usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   250
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   251
myproxy = remotething(myserver)
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   252
print()
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   253
bprint(b"== Remote")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   254
use(myproxy)