tests/test-batching.py
author Gregory Szorc <gregory.szorc@gmail.com>
Thu, 06 Jul 2017 17:31:33 -0700
changeset 33316 310f7bcab50b
parent 28800 544908ae36ce
child 33765 e2fc2122029c
permissions -rw-r--r--
sparse: remove custom hash matcher With the recent change to always use repr(), this function was functionally identical to the version in fsmonitor it was replacing. So remove it.
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
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
    10
from mercurial import (
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
    11
    peer,
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
    12
    wireproto,
28731
f8872b507cd3 py3: use absolute_import in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 25912
diff changeset
    13
)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    14
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    15
# equivalent of repo.repository
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    16
class thing(object):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    17
    def hello(self):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    18
        return "Ready."
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    19
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    20
# equivalent of localrepo.localrepository
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    21
class localthing(thing):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    22
    def foo(self, one, two=None):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    23
        if one:
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    24
            return "%s and %s" % (one, two,)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    25
        return "Nope"
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    26
    def bar(self, b, a):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    27
        return "%s und %s" % (b, a,)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    28
    def greet(self, name=None):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    29
        return "Hello, %s" % name
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    30
    def batch(self):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    31
        '''Support for local batching.'''
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
    32
        return peer.localbatch(self)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    33
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    34
# usage of "thing" interface
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    35
def use(it):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    36
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    37
    # Direct call to base method shared between client and server.
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    38
    print(it.hello())
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    39
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    40
    # Direct calls to proxied methods. They cause individual roundtrips.
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    41
    print(it.foo("Un", two="Deux"))
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    42
    print(it.bar("Eins", "Zwei"))
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    43
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    44
    # Batched call to a couple of (possibly proxied) methods.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    45
    batch = it.batch()
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    46
    # The calls return futures to eventually hold results.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    47
    foo = batch.foo(one="One", two="Two")
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    48
    foo2 = batch.foo(None)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    49
    bar = batch.bar("Eins", "Zwei")
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    50
    # We can call non-batchable proxy methods, but the break the current batch
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    51
    # request and cause additional roundtrips.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    52
    greet = batch.greet(name="John Smith")
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    53
    # We can also add local methods into the mix, but they break the batch too.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    54
    hello = batch.hello()
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    55
    bar2 = batch.bar(b="Uno", a="Due")
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    56
    # Only now are all the calls executed in sequence, with as few roundtrips
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    57
    # as possible.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    58
    batch.submit()
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    59
    # After the call to submit, the futures actually contain values.
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    60
    print(foo.value)
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    61
    print(foo2.value)
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    62
    print(bar.value)
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    63
    print(greet.value)
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    64
    print(hello.value)
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    65
    print(bar2.value)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    66
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    67
# local usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    68
mylocal = localthing()
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    69
print()
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
    70
print("== Local")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    71
use(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    72
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    73
# 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
    74
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    75
# shared
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    76
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    77
def escapearg(plain):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    78
    return (plain
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    79
            .replace(':', '::')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    80
            .replace(',', ':,')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    81
            .replace(';', ':;')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    82
            .replace('=', ':='))
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    83
def unescapearg(escaped):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    84
    return (escaped
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    85
            .replace(':=', '=')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    86
            .replace(':;', ';')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    87
            .replace(':,', ',')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    88
            .replace('::', ':'))
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    89
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    90
# server side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    91
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    92
# 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
    93
class server(object):
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    94
    def __init__(self, local):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    95
        self.local = local
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    96
    def _call(self, name, args):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    97
        args = dict(arg.split('=', 1) for arg in args)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    98
        return getattr(self, name)(**args)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
    99
    def perform(self, req):
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   100
        print("REQ:", req)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   101
        name, args = req.split('?', 1)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   102
        args = args.split('&')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   103
        vals = dict(arg.split('=', 1) for arg in args)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   104
        res = getattr(self, name)(**vals)
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   105
        print("  ->", res)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   106
        return res
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   107
    def batch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   108
        res = []
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   109
        for pair in cmds.split(';'):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   110
            name, args = pair.split(':', 1)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   111
            vals = {}
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   112
            for a in args.split(','):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   113
                if a:
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   114
                    n, v = a.split('=')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   115
                    vals[n] = unescapearg(v)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   116
            res.append(escapearg(getattr(self, name)(**vals)))
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   117
        return ';'.join(res)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   118
    def foo(self, one, two):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   119
        return mangle(self.local.foo(unmangle(one), unmangle(two)))
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   120
    def bar(self, b, a):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   121
        return mangle(self.local.bar(unmangle(b), unmangle(a)))
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   122
    def greet(self, name):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   123
        return mangle(self.local.greet(unmangle(name)))
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   124
myserver = server(mylocal)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   125
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   126
# local side
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   127
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   128
# 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
   129
# 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
   130
def mangle(s):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   131
    return ''.join(chr(ord(c) + 1) for c in s)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   132
def unmangle(s):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   133
    return ''.join(chr(ord(c) - 1) for c in s)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   134
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   135
# 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
   136
class remotething(thing):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   137
    def __init__(self, server):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   138
        self.server = server
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   139
    def _submitone(self, name, args):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   140
        req = name + '?' + '&'.join(['%s=%s' % (n, v) for n, v in args])
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   141
        return self.server.perform(req)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   142
    def _submitbatch(self, cmds):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   143
        req = []
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   144
        for name, args in cmds:
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   145
            args = ','.join(n + '=' + escapearg(v) for n, v in args)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   146
            req.append(name + ':' + args)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   147
        req = ';'.join(req)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   148
        res = self._submitone('batch', [('cmds', req,)])
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   149
        return res.split(';')
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   150
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   151
    def batch(self):
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
   152
        return wireproto.remotebatch(self)
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   153
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
   154
    @peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   155
    def foo(self, one, two=None):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   156
        if not one:
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   157
            yield "Nope", None
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   158
        encargs = [('one', mangle(one),), ('two', mangle(two),)]
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
   159
        encresref = peer.future()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   160
        yield encargs, encresref
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   161
        yield unmangle(encresref.value)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   162
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
   163
    @peer.batchable
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   164
    def bar(self, b, a):
28800
544908ae36ce test-batching: stop direct symbol import of mercurial modules
Yuya Nishihara <yuya@tcha.org>
parents: 28732
diff changeset
   165
        encresref = peer.future()
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   166
        yield [('b', mangle(b),), ('a', mangle(a),)], encresref
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   167
        yield unmangle(encresref.value)
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   168
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   169
    # 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
   170
    # 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
   171
    # greet is done in its own roundtrip.
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   172
    def greet(self, name=None):
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   173
        return unmangle(self._submitone('greet', [('name', mangle(name),)]))
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   174
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   175
# demo remote usage
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   176
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   177
myproxy = remotething(myserver)
28732
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   178
print()
e2b118592c63 py3: use print_function in test-batching.py
Robert Stanca <robert.stanca7@gmail.com>
parents: 28731
diff changeset
   179
print("== Remote")
14621
84094c0d2724 wireproto: add basic command batching infrastructure
Peter Arrenbrecht <peter.arrenbrecht@gmail.com>
parents:
diff changeset
   180
use(myproxy)