tests/test-batching.py
author Martin von Zweigbergk <martinvonz@google.com>
Tue, 09 Feb 2021 09:37:39 -0800
changeset 46480 05dd091dfa6a
parent 45942 89a2afe31e82
child 47873 c424ff4807e6
permissions -rw-r--r--
wireprotopeer: clarify some variable names now that we allow snake_case "encargsorres" is hard to parse ("encarg sorres" sounds like it might be Spanish to me, and indeed Google Translate tells me that it's Catalan for "order sands"). Let's clarify with some added underscores and longer names. Differential Revision: https://phab.mercurial-scm.org/D9973
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:
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    33
            return b"%s and %s" % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    34
                one,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    35
                two,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    36
            )
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    37
        return b"Nope"
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 bar(self, b, a):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    40
        return b"%s und %s" % (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    41
            b,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    42
            a,
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
    43
        )
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    44
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    45
    def greet(self, name=None):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    46
        return b"Hello, %s" % name
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    47
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    48
    @contextlib.contextmanager
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    49
    def commandexecutor(self):
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    50
        e = localrepo.localcommandexecutor(self)
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    51
        try:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    52
            yield e
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    53
        finally:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    54
            e.close()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    55
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    56
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    57
# usage of "thing" interface
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    58
def use(it):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    59
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    60
    # 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
    61
    bprint(it.hello())
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    62
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    63
    # 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
    64
    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
    65
    bprint(it.bar(b"Eins", b"Zwei"))
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    66
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
    67
    # 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
    68
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37615
diff changeset
    69
    with it.commandexecutor() as e:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    70
        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
    71
        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
    72
        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
    73
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    74
    bprint(ffoo.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    75
    bprint(fbar.result())
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    76
    bprint(fbar2.result())
33766
4c706037adef wireproto: overhaul iterating batcher code (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33765
diff changeset
    77
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    78
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    79
# local usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    80
mylocal = localthing()
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    81
print()
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
    82
bprint(b"== Local")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    83
use(mylocal)
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
# 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
    86
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    87
# shared
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    88
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    89
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    90
def escapearg(plain):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    91
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    92
        plain.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
        .replace(b';', b':;')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    95
        .replace(b'=', b':=')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    96
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    97
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
    98
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    99
def unescapearg(escaped):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   100
    return (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   101
        escaped.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
        .replace(b':,', b',')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   104
        .replace(b'::', b':')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   105
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   106
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   107
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   108
# server side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   109
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   110
# 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
   111
class server(object):
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   112
    def __init__(self, local):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   113
        self.local = local
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   114
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   115
    def _call(self, name, args):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   116
        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
   117
        return getattr(self, name)(**args)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   118
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   119
    def perform(self, req):
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   120
        bprint(b"REQ:", req)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   121
        name, args = req.split(b'?', 1)
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   122
        args = args.split(b'&')
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   123
        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
   124
        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
   125
        bprint(b"  ->", res)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   126
        return res
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   127
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   128
    def batch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   129
        res = []
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   130
        for pair in cmds.split(b';'):
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   131
            name, args = pair.split(b':', 1)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   132
            vals = {}
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   133
            for a in args.split(b','):
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   134
                if a:
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   135
                    n, v = a.split(b'=')
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   136
                    vals[n] = unescapearg(v)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   137
            res.append(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   138
                escapearg(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   139
                    getattr(self, pycompat.sysstr(name))(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   140
                        **pycompat.strkwargs(vals)
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
                )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   143
            )
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   144
        return b';'.join(res)
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 foo(self, one, two):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   147
        return mangle(self.local.foo(unmangle(one), unmangle(two)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   148
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   149
    def bar(self, b, a):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   150
        return mangle(self.local.bar(unmangle(b), unmangle(a)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   151
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   152
    def greet(self, name):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   153
        return mangle(self.local.greet(unmangle(name)))
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   154
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   155
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   156
myserver = server(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   157
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   158
# local side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   159
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
        ]
46480
05dd091dfa6a wireprotopeer: clarify some variable names now that we allow snake_case
Martin von Zweigbergk <martinvonz@google.com>
parents: 45942
diff changeset
   217
        encoded_res_future = wireprotov1peer.future()
05dd091dfa6a wireprotopeer: clarify some variable names now that we allow snake_case
Martin von Zweigbergk <martinvonz@google.com>
parents: 45942
diff changeset
   218
        yield encoded_args, encoded_res_future
05dd091dfa6a wireprotopeer: clarify some variable names now that we allow snake_case
Martin von Zweigbergk <martinvonz@google.com>
parents: 45942
diff changeset
   219
        yield unmangle(encoded_res_future.value)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   220
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   221
    @wireprotov1peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   222
    def bar(self, b, a):
37615
f3dc8239e3a9 peer: scatter module to the wind (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   223
        encresref = wireprotov1peer.future()
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   224
        yield [
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'b',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   227
                mangle(b),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   228
            ),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   229
            (
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   230
                b'a',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   231
                mangle(a),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   232
            ),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   233
        ], encresref
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   234
        yield unmangle(encresref.value)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   235
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   236
    # 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
   237
    # 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
   238
    # greet is done in its own roundtrip.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   239
    def greet(self, name=None):
45942
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   240
        return unmangle(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   241
            self._submitone(
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   242
                b'greet',
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
                        b'name',
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   246
                        mangle(name),
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   247
                    )
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   248
                ],
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   249
            )
89a2afe31e82 formating: upgrade to black 20.8b1
Augie Fackler <raf@durin42.com>
parents: 43076
diff changeset
   250
        )
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   251
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41335
diff changeset
   252
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   253
# demo remote usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   254
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   255
myproxy = remotething(myserver)
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   256
print()
41335
b81ca9a3f4e4 py3: port test-batching.py to python3
Augie Fackler <augie@google.com>
parents: 37633
diff changeset
   257
bprint(b"== Remote")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   258
use(myproxy)