tests/test-batching.py
changeset 28732 e2b118592c63
parent 28731 f8872b507cd3
child 28800 544908ae36ce
equal deleted inserted replaced
28731:f8872b507cd3 28732:e2b118592c63
     3 # Copyright 2011 Peter Arrenbrecht <peter@arrenbrecht.ch>
     3 # Copyright 2011 Peter Arrenbrecht <peter@arrenbrecht.ch>
     4 #
     4 #
     5 # This software may be used and distributed according to the terms of the
     5 # This software may be used and distributed according to the terms of the
     6 # GNU General Public License version 2 or any later version.
     6 # GNU General Public License version 2 or any later version.
     7 
     7 
     8 from __future__ import absolute_import
     8 from __future__ import absolute_import, print_function
     9 from mercurial.peer import (
     9 from mercurial.peer import (
    10     localbatch,
    10     localbatch,
    11     batchable,
    11     batchable,
    12     future,
    12     future,
    13 )
    13 )
    36 
    36 
    37 # usage of "thing" interface
    37 # usage of "thing" interface
    38 def use(it):
    38 def use(it):
    39 
    39 
    40     # Direct call to base method shared between client and server.
    40     # Direct call to base method shared between client and server.
    41     print it.hello()
    41     print(it.hello())
    42 
    42 
    43     # Direct calls to proxied methods. They cause individual roundtrips.
    43     # Direct calls to proxied methods. They cause individual roundtrips.
    44     print it.foo("Un", two="Deux")
    44     print(it.foo("Un", two="Deux"))
    45     print it.bar("Eins", "Zwei")
    45     print(it.bar("Eins", "Zwei"))
    46 
    46 
    47     # Batched call to a couple of (possibly proxied) methods.
    47     # Batched call to a couple of (possibly proxied) methods.
    48     batch = it.batch()
    48     batch = it.batch()
    49     # The calls return futures to eventually hold results.
    49     # The calls return futures to eventually hold results.
    50     foo = batch.foo(one="One", two="Two")
    50     foo = batch.foo(one="One", two="Two")
    58     bar2 = batch.bar(b="Uno", a="Due")
    58     bar2 = batch.bar(b="Uno", a="Due")
    59     # Only now are all the calls executed in sequence, with as few roundtrips
    59     # Only now are all the calls executed in sequence, with as few roundtrips
    60     # as possible.
    60     # as possible.
    61     batch.submit()
    61     batch.submit()
    62     # After the call to submit, the futures actually contain values.
    62     # After the call to submit, the futures actually contain values.
    63     print foo.value
    63     print(foo.value)
    64     print foo2.value
    64     print(foo2.value)
    65     print bar.value
    65     print(bar.value)
    66     print greet.value
    66     print(greet.value)
    67     print hello.value
    67     print(hello.value)
    68     print bar2.value
    68     print(bar2.value)
    69 
    69 
    70 # local usage
    70 # local usage
    71 mylocal = localthing()
    71 mylocal = localthing()
    72 print
    72 print()
    73 print "== Local"
    73 print("== Local")
    74 use(mylocal)
    74 use(mylocal)
    75 
    75 
    76 # demo remoting; mimicks what wireproto and HTTP/SSH do
    76 # demo remoting; mimicks what wireproto and HTTP/SSH do
    77 
    77 
    78 # shared
    78 # shared
    98         self.local = local
    98         self.local = local
    99     def _call(self, name, args):
    99     def _call(self, name, args):
   100         args = dict(arg.split('=', 1) for arg in args)
   100         args = dict(arg.split('=', 1) for arg in args)
   101         return getattr(self, name)(**args)
   101         return getattr(self, name)(**args)
   102     def perform(self, req):
   102     def perform(self, req):
   103         print "REQ:", req
   103         print("REQ:", req)
   104         name, args = req.split('?', 1)
   104         name, args = req.split('?', 1)
   105         args = args.split('&')
   105         args = args.split('&')
   106         vals = dict(arg.split('=', 1) for arg in args)
   106         vals = dict(arg.split('=', 1) for arg in args)
   107         res = getattr(self, name)(**vals)
   107         res = getattr(self, name)(**vals)
   108         print "  ->", res
   108         print("  ->", res)
   109         return res
   109         return res
   110     def batch(self, cmds):
   110     def batch(self, cmds):
   111         res = []
   111         res = []
   112         for pair in cmds.split(';'):
   112         for pair in cmds.split(';'):
   113             name, args = pair.split(':', 1)
   113             name, args = pair.split(':', 1)
   176         return unmangle(self._submitone('greet', [('name', mangle(name),)]))
   176         return unmangle(self._submitone('greet', [('name', mangle(name),)]))
   177 
   177 
   178 # demo remote usage
   178 # demo remote usage
   179 
   179 
   180 myproxy = remotething(myserver)
   180 myproxy = remotething(myserver)
   181 print
   181 print()
   182 print "== Remote"
   182 print("== Remote")
   183 use(myproxy)
   183 use(myproxy)