tests/test-wireproto.py
author Gregory Szorc <gregory.szorc@gmail.com>
Wed, 07 Feb 2018 20:27:36 -0800
changeset 36074 2f7290555c96
parent 33806 dedab036215d
child 36555 c28de190e20a
permissions -rw-r--r--
wireproto: introduce type for raw byte responses (API) Right now we simply return a str/bytes instance for simple responses. I want all wire protocol response types to be strongly typed. So let's invent and use a type for raw bytes responses. .. api:: Wire protocol command handlers now return a wireprototypes.bytesresponse instead of a raw bytes instance. Protocol handlers will continue handling bytes instances. However, any extensions wrapping wire protocol commands will need to handle the new type. Differential Revision: https://phab.mercurial-scm.org/D2089
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28675
fcafd84bc9c5 py3: make test-wireproto use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28438
diff changeset
     1
from __future__ import absolute_import, print_function
27301
5defcb7d6e5f tests: use absolulte_import in test-wireproto.py
Gregory Szorc <gregory.szorc@gmail.com>
parents: 25912
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 (
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
     4
    error,
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28860
diff changeset
     5
    util,
28860
50d11dd8ac02 py3: use multi-line import in test-wireproto.py
timeless <timeless@mozdev.org>
parents: 28675
diff changeset
     6
    wireproto,
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
     7
    wireprototypes,
28860
50d11dd8ac02 py3: use multi-line import in test-wireproto.py
timeless <timeless@mozdev.org>
parents: 28675
diff changeset
     8
)
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28860
diff changeset
     9
stringio = util.stringio
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    10
14764
a7d5816087a9 classes: fix class style problems found by b071cd58af50
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14622
diff changeset
    11
class proto(object):
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    12
    def __init__(self, args):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    13
        self.args = args
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    14
    def getargs(self, spec):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    15
        args = self.args
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    16
        args.setdefault('*', {})
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    17
        names = spec.split()
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    18
        return [args[n] for n in names]
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    19
17192
1ac628cd7113 peer: introduce real peer classes
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14765
diff changeset
    20
class clientpeer(wireproto.wirepeer):
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    21
    def __init__(self, serverrepo):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    22
        self.serverrepo = serverrepo
25912
cbbdd085c991 batching: migrate basic noop batching into peer.peer
Augie Fackler <augie@google.com>
parents: 25708
diff changeset
    23
33806
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    24
    @property
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    25
    def ui(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    26
        return self.serverrepo.ui
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    27
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    28
    def url(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    29
        return 'test'
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    30
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    31
    def local(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    32
        return None
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    33
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    34
    def peer(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    35
        return self
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    36
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    37
    def canpush(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    38
        return True
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    39
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    40
    def close(self):
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    41
        pass
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    42
dedab036215d wireproto: use new peer interface
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33767
diff changeset
    43
    def capabilities(self):
25912
cbbdd085c991 batching: migrate basic noop batching into peer.peer
Augie Fackler <augie@google.com>
parents: 25708
diff changeset
    44
        return ['batch']
cbbdd085c991 batching: migrate basic noop batching into peer.peer
Augie Fackler <augie@google.com>
parents: 25708
diff changeset
    45
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    46
    def _call(self, cmd, **args):
36074
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
    47
        res = wireproto.dispatch(self.serverrepo, proto(args), cmd)
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
    48
        if isinstance(res, wireprototypes.bytesresponse):
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
    49
            return res.data
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
    50
        elif isinstance(res, bytes):
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
    51
            return res
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
    52
        else:
2f7290555c96 wireproto: introduce type for raw byte responses (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 33806
diff changeset
    53
            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
    54
28438
48fd02dac1d4 wireproto: make iterbatcher behave streamily over http(s)
Augie Fackler <augie@google.com>
parents: 27301
diff changeset
    55
    def _callstream(self, cmd, **args):
28861
86db5cb55d46 pycompat: switch to util.stringio for py3 compat
timeless <timeless@mozdev.org>
parents: 28860
diff changeset
    56
        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
    57
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    58
    @wireproto.batchable
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    59
    def greet(self, name):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    60
        f = wireproto.future()
20686
c69f62906358 test-wireproto: move from dict() construction to {} literals
Augie Fackler <raf@durin42.com>
parents: 18278
diff changeset
    61
        yield {'name': mangle(name)}, f
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    62
        yield unmangle(f.value)
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    63
14764
a7d5816087a9 classes: fix class style problems found by b071cd58af50
Thomas Arendsen Hein <thomas@intevation.de>
parents: 14622
diff changeset
    64
class serverrepo(object):
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    65
    def greet(self, name):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    66
        return "Hello, " + name
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    67
18278
753acee7d6dd clfilter: make localpeer use a repo with "unserved" filter
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents: 17192
diff changeset
    68
    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
    69
        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
    70
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    71
def mangle(s):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    72
    return ''.join(chr(ord(c) + 1) for c in s)
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    73
def unmangle(s):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    74
    return ''.join(chr(ord(c) - 1) for c in s)
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    75
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    76
def greet(repo, proto, name):
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    77
    return mangle(repo.greet(unmangle(name)))
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    78
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    79
wireproto.commands['greet'] = (greet, 'name',)
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    80
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    81
srv = serverrepo()
17192
1ac628cd7113 peer: introduce real peer classes
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents: 14765
diff changeset
    82
clt = clientpeer(srv)
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    83
28675
fcafd84bc9c5 py3: make test-wireproto use print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 28438
diff changeset
    84
print(clt.greet("Foobar"))
33767
b47fe9733d76 peer: remove non iterating batcher (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28861
diff changeset
    85
b = clt.iterbatch()
b47fe9733d76 peer: remove non iterating batcher (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28861
diff changeset
    86
map(b.greet, ('Fo, =;:<o', 'Bar'))
14622
bd88561afb4b wireproto: add batching support to wirerepository
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    87
b.submit()
33767
b47fe9733d76 peer: remove non iterating batcher (API)
Gregory Szorc <gregory.szorc@gmail.com>
parents: 28861
diff changeset
    88
print([r for r in b.results()])