D319: wireproto: overhaul iterating batcher code (API)

2017-08-11 Thread indygreg (Gregory Szorc)
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG4c706037adef: wireproto: overhaul iterating batcher code 
(API) (authored by indygreg).

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D319?vs=734&id=793

REVISION DETAIL
  https://phab.mercurial-scm.org/D319

AFFECTED FILES
  mercurial/peer.py
  mercurial/wireproto.py
  tests/test-batching.py
  tests/test-batching.py.out

CHANGE DETAILS

diff --git a/tests/test-batching.py.out b/tests/test-batching.py.out
--- a/tests/test-batching.py.out
+++ b/tests/test-batching.py.out
@@ -5,9 +5,8 @@
 Eins und Zwei
 One and Two
 Eins und Zwei
-Hello, John Smith
-Ready.
 Uno und Due
+proper end of results generator
 
 == Remote
 Ready.
@@ -17,14 +16,11 @@
 REQ: bar?b=Fjot&a=[xfj
   -> Fjot!voe![xfj
 Eins und Zwei
-REQ: batch?cmds=foo:one=Pof,two=Uxp;bar:b=Fjot,a=[xfj
-  -> Pof!boe!Uxp;Fjot!voe![xfj
-REQ: greet?name=Kpio!Tnjui
-  -> Ifmmp-!Kpio!Tnjui
-REQ: batch?cmds=bar:b=Vop,a=Evf
-  -> Vop!voe!Evf
+REQ: batch?cmds=foo:one=Pof,two=Uxp;bar:b=Fjot,a=[xfj;bar:b=Vop,a=Evf
+  -> Pof!boe!Uxp;Fjot!voe![xfj;Vop!voe!Evf
 One and Two
 Eins und Zwei
-Hello, John Smith
-Ready.
 Uno und Due
+proper end of results generator
+Attempted to batch a non-batchable call to 'greet'
+Attempted to batch a non-batchable call to 'hello'
diff --git a/tests/test-batching.py b/tests/test-batching.py
--- a/tests/test-batching.py
+++ b/tests/test-batching.py
@@ -8,7 +8,9 @@
 from __future__ import absolute_import, print_function
 
 from mercurial import (
+error,
 peer,
+util,
 wireproto,
 )
 
@@ -27,9 +29,9 @@
 return "%s und %s" % (b, a,)
 def greet(self, name=None):
 return "Hello, %s" % name
-def batch(self):
+def batchiter(self):
 '''Support for local batching.'''
-return peer.localbatch(self)
+return peer.localiterbatcher(self)
 
 # usage of "thing" interface
 def use(it):
@@ -41,27 +43,54 @@
 print(it.foo("Un", two="Deux"))
 print(it.bar("Eins", "Zwei"))
 
-# Batched call to a couple of (possibly proxied) methods.
-batch = it.batch()
+# Batched call to a couple of proxied methods.
+batch = it.batchiter()
 # The calls return futures to eventually hold results.
 foo = batch.foo(one="One", two="Two")
 bar = batch.bar("Eins", "Zwei")
-# We can call non-batchable proxy methods, but the break the current batch
-# request and cause additional roundtrips.
-greet = batch.greet(name="John Smith")
-# We can also add local methods into the mix, but they break the batch too.
-hello = batch.hello()
 bar2 = batch.bar(b="Uno", a="Due")
-# Only now are all the calls executed in sequence, with as few roundtrips
-# as possible.
+
+# Future shouldn't be set until we submit().
+assert isinstance(foo, peer.future)
+assert not util.safehasattr(foo, 'value')
+assert not util.safehasattr(bar, 'value')
 batch.submit()
-# After the call to submit, the futures actually contain values.
+# Call results() to obtain results as a generator.
+results = batch.results()
+
+# Future results shouldn't be set until we consume a value.
+assert not util.safehasattr(foo, 'value')
+foovalue = next(results)
+assert util.safehasattr(foo, 'value')
+assert foovalue == foo.value
 print(foo.value)
+next(results)
 print(bar.value)
-print(greet.value)
-print(hello.value)
+next(results)
 print(bar2.value)
 
+# We should be at the end of the results generator.
+try:
+next(results)
+except StopIteration:
+print('proper end of results generator')
+else:
+print('extra emitted element!')
+
+# Attempting to call a non-batchable method inside a batch fails.
+batch = it.batchiter()
+try:
+batch.greet(name='John Smith')
+except error.ProgrammingError as e:
+print(e)
+
+# Attempting to call a local method inside a batch fails.
+batch = it.batchiter()
+try:
+batch.hello()
+except error.ProgrammingError as e:
+print(e)
+
 # local usage
 mylocal = localthing()
 print()
@@ -144,10 +173,11 @@
 req.append(name + ':' + args)
 req = ';'.join(req)
 res = self._submitone('batch', [('cmds', req,)])
-return res.split(';')
+for r in res.split(';'):
+yield r
 
-def batch(self):
-return wireproto.remotebatch(self)
+def batchiter(self):
+return wireproto.remoteiterbatcher(self)
 
 @peer.batchable
 def foo(self, one, two=None):
diff --git a/mercurial/wireproto.py b/mercurial/wireproto.py
--- a/mercurial/wireproto.py
+++ b/mercurial/wireproto.py
@@ -133,23 +133,47 @@
 This is mostly valuable over http where request sizes can be
 limited, but can be used in other places as well.
 """
-req, rsp = [], []
-for name, args, opts, resref in self.calls:
-  

D319: wireproto: overhaul iterating batcher code (API)

2017-08-09 Thread indygreg (Gregory Szorc)
indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  The remote batching code is difficult to read. Let's improve it.
  
  As part of the refactor, the future returned by method calls on
  batchiter() instances is now populated. However, you still need to
  consume the results() generator for the future to be set.  But at
  least now we can stuff the future somewhere and not have to worry
  about aligning method call order with result order since you can
  use a future to hold the result.
  
  Also as part of the change, we now verify that @batchable generators
  yield exactly 2 values. In other words, we enforce their API.
  
  The non-iter batcher has been unused since 
https://phab.mercurial-scm.org/rHGb6e71f8af5b8710139b64706a375b3f16eb1241e. And 
to my
  surprise we had no explicit unit test coverage of it! test-batching.py
  has been overhauled to use the iterating batcher.
  
  Since the iterating batcher doesn't allow non-batchable method
  calls nor local calls, tests have been updated to reflect reality.
  The iterating batcher has been used for multiple releases apparently
  without major issue. So this shouldn't cause alarm.
  
  .. api::
  
@peer.batchable functions must now yield exactly 2 values

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D319

AFFECTED FILES
  mercurial/peer.py
  mercurial/wireproto.py
  tests/test-batching.py
  tests/test-batching.py.out

CHANGE DETAILS

diff --git a/tests/test-batching.py.out b/tests/test-batching.py.out
--- a/tests/test-batching.py.out
+++ b/tests/test-batching.py.out
@@ -5,9 +5,8 @@
 Eins und Zwei
 One and Two
 Eins und Zwei
-Hello, John Smith
-Ready.
 Uno und Due
+proper end of results generator
 
 == Remote
 Ready.
@@ -17,14 +16,11 @@
 REQ: bar?b=Fjot&a=[xfj
   -> Fjot!voe![xfj
 Eins und Zwei
-REQ: batch?cmds=foo:one=Pof,two=Uxp;bar:b=Fjot,a=[xfj
-  -> Pof!boe!Uxp;Fjot!voe![xfj
-REQ: greet?name=Kpio!Tnjui
-  -> Ifmmp-!Kpio!Tnjui
-REQ: batch?cmds=bar:b=Vop,a=Evf
-  -> Vop!voe!Evf
+REQ: batch?cmds=foo:one=Pof,two=Uxp;bar:b=Fjot,a=[xfj;bar:b=Vop,a=Evf
+  -> Pof!boe!Uxp;Fjot!voe![xfj;Vop!voe!Evf
 One and Two
 Eins und Zwei
-Hello, John Smith
-Ready.
 Uno und Due
+proper end of results generator
+Attempted to batch a non-batchable call to 'greet'
+Attempted to batch a non-batchable call to 'hello'
diff --git a/tests/test-batching.py b/tests/test-batching.py
--- a/tests/test-batching.py
+++ b/tests/test-batching.py
@@ -8,7 +8,9 @@
 from __future__ import absolute_import, print_function
 
 from mercurial import (
+error,
 peer,
+util,
 wireproto,
 )
 
@@ -27,9 +29,9 @@
 return "%s und %s" % (b, a,)
 def greet(self, name=None):
 return "Hello, %s" % name
-def batch(self):
+def batchiter(self):
 '''Support for local batching.'''
-return peer.localbatch(self)
+return peer.localiterbatcher(self)
 
 # usage of "thing" interface
 def use(it):
@@ -41,27 +43,54 @@
 print(it.foo("Un", two="Deux"))
 print(it.bar("Eins", "Zwei"))
 
-# Batched call to a couple of (possibly proxied) methods.
-batch = it.batch()
+# Batched call to a couple of proxied methods.
+batch = it.batchiter()
 # The calls return futures to eventually hold results.
 foo = batch.foo(one="One", two="Two")
 bar = batch.bar("Eins", "Zwei")
-# We can call non-batchable proxy methods, but the break the current batch
-# request and cause additional roundtrips.
-greet = batch.greet(name="John Smith")
-# We can also add local methods into the mix, but they break the batch too.
-hello = batch.hello()
 bar2 = batch.bar(b="Uno", a="Due")
-# Only now are all the calls executed in sequence, with as few roundtrips
-# as possible.
+
+# Future shouldn't be set until we submit().
+assert isinstance(foo, peer.future)
+assert not util.safehasattr(foo, 'value')
+assert not util.safehasattr(bar, 'value')
 batch.submit()
-# After the call to submit, the futures actually contain values.
+# Call results() to obtain results as a generator.
+results = batch.results()
+
+# Future results shouldn't be set until we consume a value.
+assert not util.safehasattr(foo, 'value')
+foovalue = next(results)
+assert util.safehasattr(foo, 'value')
+assert foovalue == foo.value
 print(foo.value)
+next(results)
 print(bar.value)
-print(greet.value)
-print(hello.value)
+next(results)
 print(bar2.value)
 
+# We should be at the end of the results generator.
+try:
+next(results)
+except StopIteration:
+print('proper end of results generator')
+else:
+print('extra emitted element!')
+
+# Attempting to call a non-batchable method inside a batch fails.
+batch = it.batchiter()
+try:
+batch.greet(name='John Smith')
+except error.ProgrammingError as e:
+