tests/test-wireproto.py
author Raphaël Gomès <rgomes@octobus.net>
Mon, 06 Nov 2023 11:06:08 +0100
changeset 51118 532e74ad3ff6
parent 49037 642e31cb55f0
permissions -rw-r--r--
rust: run a clippy pass with the latest stable version Our current version of clippy is older than the latest stable. The newest version has new lints that are moslty good advice, so let's apply them ahead of time. This has the added benefit of reducing the noise for developpers like myself that use clippy as an IDE helper, as well as being more prepared for a future clippy upgrade.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37967
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
     1
import sys
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
     2
28860
50d11dd8ac02 py3: use multi-line import in test-wireproto.py
timeless <timeless@mozdev.org>
parents: 28675
diff changeset
     3
from mercurial import (
36112
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
     4
    error,
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
     5
    pycompat,
36950
65615c29e74f tests: fix test-wireproto.py to work around serverrepo() not having a ui
Augie Fackler <augie@google.com>
parents: 36809
diff changeset
     6
    ui as uimod,
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28860
diff changeset
     7
    util,
36112
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
     8
    wireprototypes,
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37321
diff changeset
     9
    wireprotov1peer,
37785
b4d85bc122bd wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37633
diff changeset
    10
    wireprotov1server,
28860
50d11dd8ac02 py3: use multi-line import in test-wireproto.py
timeless <timeless@mozdev.org>
parents: 28675
diff changeset
    11
)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    12
from mercurial.utils import stringutil
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    13
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28860
diff changeset
    14
stringio = util.stringio
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    15
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    16
49037
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48966
diff changeset
    17
class proto:
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    18
    def __init__(self, args):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    19
        self.args = args
37295
45b39c69fae0 wireproto: separate commands tables for version 1 and 2 commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36950
diff changeset
    20
        self.name = 'dummyproto'
45b39c69fae0 wireproto: separate commands tables for version 1 and 2 commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36950
diff changeset
    21
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    22
    def getargs(self, spec):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    23
        args = self.args
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
    24
        args.setdefault(b'*', {})
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    25
        names = spec.split()
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    26
        return [args[n] for n in names]
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    27
36809
66de4555cefd wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36584
diff changeset
    28
    def checkperm(self, perm):
66de4555cefd wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36584
diff changeset
    29
        pass
66de4555cefd wireproto: formalize permissions checking as part of protocol interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36584
diff changeset
    30
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    31
37295
45b39c69fae0 wireproto: separate commands tables for version 1 and 2 commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36950
diff changeset
    32
wireprototypes.TRANSPORTS['dummyproto'] = {
45b39c69fae0 wireproto: separate commands tables for version 1 and 2 commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36950
diff changeset
    33
    'transport': 'dummy',
45b39c69fae0 wireproto: separate commands tables for version 1 and 2 commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36950
diff changeset
    34
    'version': 1,
45b39c69fae0 wireproto: separate commands tables for version 1 and 2 commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36950
diff changeset
    35
}
45b39c69fae0 wireproto: separate commands tables for version 1 and 2 commands
Gregory Szorc <gregory.szorc@gmail.com>
parents: 36950
diff changeset
    36
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    37
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37321
diff changeset
    38
class clientpeer(wireprotov1peer.wirepeer):
36950
65615c29e74f tests: fix test-wireproto.py to work around serverrepo() not having a ui
Augie Fackler <augie@google.com>
parents: 36809
diff changeset
    39
    def __init__(self, serverrepo, ui):
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    40
        self.serverrepo = serverrepo
37321
e826fe7a08c7 peer: make ui an attribute
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37295
diff changeset
    41
        self.ui = ui
33827
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    42
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    43
    def url(self):
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
    44
        return b'test'
33827
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    45
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    46
    def local(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    47
        return None
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    48
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    49
    def peer(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    50
        return self
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    51
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    52
    def canpush(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    53
        return True
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    54
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    55
    def close(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    56
        pass
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    57
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33788
diff changeset
    58
    def capabilities(self):
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
    59
        return [b'batch']
25912
cbbdd085c991 batching: migrate basic noop batching into peer.peer
Augie Fackler <augie@google.com>
parents: 25708
diff changeset
    60
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    61
    def _call(self, cmd, **args):
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
    62
        args = pycompat.byteskwargs(args)
37785
b4d85bc122bd wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37633
diff changeset
    63
        res = wireprotov1server.dispatch(self.serverrepo, proto(args), cmd)
36112
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
    64
        if isinstance(res, wireprototypes.bytesresponse):
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
    65
            return res.data
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
    66
        elif isinstance(res, bytes):
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
    67
            return res
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
    68
        else:
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33827
diff changeset
    69
            raise error.Abort('dummy client does not support response type')
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    70
28438
48fd02dac1d4 wireproto: make iterbatcher behave streamily over http(s)
Augie Fackler <augie@google.com>
parents: 27301
diff changeset
    71
    def _callstream(self, cmd, **args):
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28860
diff changeset
    72
        return stringio(self._call(cmd, **args))
28438
48fd02dac1d4 wireproto: make iterbatcher behave streamily over http(s)
Augie Fackler <augie@google.com>
parents: 27301
diff changeset
    73
37614
a81d02ea65db wireproto: move version 1 peer functionality to standalone module (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37321
diff changeset
    74
    @wireprotov1peer.batchable
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    75
    def greet(self, name):
47914
c424ff4807e6 wireprotov1peer: update all rpcs to use the new batchable scheme
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 43076
diff changeset
    76
        return {b'name': mangle(name)}, unmangle
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    77
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    78
49037
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48966
diff changeset
    79
class serverrepo:
41845
d6569f1e9b37 server: allow customizing the default repo filter
Joerg Sonnenberger <joerg@bec.de>
parents: 37986
diff changeset
    80
    def __init__(self, ui):
d6569f1e9b37 server: allow customizing the default repo filter
Joerg Sonnenberger <joerg@bec.de>
parents: 37986
diff changeset
    81
        self.ui = ui
d6569f1e9b37 server: allow customizing the default repo filter
Joerg Sonnenberger <joerg@bec.de>
parents: 37986
diff changeset
    82
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    83
    def greet(self, name):
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
    84
        return b"Hello, " + name
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    85
18278
753acee7d6dd clfilter: make localpeer use a repo with "unserved" filter
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17192
diff changeset
    86
    def filtered(self, name):
753acee7d6dd clfilter: make localpeer use a repo with "unserved" filter
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17192
diff changeset
    87
        return self
753acee7d6dd clfilter: make localpeer use a repo with "unserved" filter
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17192
diff changeset
    88
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    89
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    90
def mangle(s):
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
    91
    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: 41845
diff changeset
    92
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    93
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    94
def unmangle(s):
36584
c28de190e20a py3: port tests/test-wireproto.py to Python 3
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36112
diff changeset
    95
    return b''.join(pycompat.bytechr(ord(c) - 1) for c in pycompat.bytestr(s))
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    96
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
    97
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    98
def greet(repo, proto, name):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    99
    return mangle(repo.greet(unmangle(name)))
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   100
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
   101
37785
b4d85bc122bd wireproto: rename wireproto to wireprotov1server (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37633
diff changeset
   102
wireprotov1server.commands[b'greet'] = (greet, b'name')
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   103
41845
d6569f1e9b37 server: allow customizing the default repo filter
Joerg Sonnenberger <joerg@bec.de>
parents: 37986
diff changeset
   104
srv = serverrepo(uimod.ui())
36950
65615c29e74f tests: fix test-wireproto.py to work around serverrepo() not having a ui
Augie Fackler <augie@google.com>
parents: 36809
diff changeset
   105
clt = clientpeer(srv, uimod.ui())
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   106
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
   107
37967
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
   108
def printb(data, end=b'\n'):
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
   109
    out = getattr(sys.stdout, 'buffer', sys.stdout)
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
   110
    out.write(data + end)
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
   111
    out.flush()
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
   112
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
   113
37967
40381a88bab4 tests: port test-wireproto.py to Python 3
Augie Fackler <augie@google.com>
parents: 37785
diff changeset
   114
printb(clt.greet(b"Foobar"))
37633
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   115
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   116
with clt.commandexecutor() as e:
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   117
    fgreet1 = e.callcommand(b'greet', {b'name': b'Fo, =;:<o'})
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   118
    fgreet2 = e.callcommand(b'greet', {b'name': b'Bar'})
33a6eee08db2 wireproto: remove iterbatch() from peer interface (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 37614
diff changeset
   119
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
   120
printb(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
   121
    stringutil.pprint([f.result() for f in (fgreet1, fgreet2)], bprefix=True)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41845
diff changeset
   122
)