[Commented On] D12044: test-http-bad-server: factor code dealing with "read" in the new object
baymax (Baymax, Your Personal Patch-care Companion)
phabricator at mercurial-scm.org
Mon Jan 24 18:17:00 UTC 2022
baymax added a comment.
baymax updated this revision to Diff 31868.
✅ refresh by Heptapod after a successful CI run (🐙 💚)
REPOSITORY
rHG Mercurial
CHANGES SINCE LAST UPDATE
https://phab.mercurial-scm.org/D12044?vs=31747&id=31868
BRANCH
default
CHANGES SINCE LAST ACTION
https://phab.mercurial-scm.org/D12044/new/
REVISION DETAIL
https://phab.mercurial-scm.org/D12044
AFFECTED FILES
tests/testlib/badserverext.py
CHANGE DETAILS
diff --git a/tests/testlib/badserverext.py b/tests/testlib/badserverext.py
--- a/tests/testlib/badserverext.py
+++ b/tests/testlib/badserverext.py
@@ -146,6 +146,51 @@
return result
+ def forward_read(self, obj, method, size=-1):
+ """call an underlying read function until condition are met
+
+ When the condition are met the socket is closed
+ """
+ remaining = self.remaining_recv_bytes
+
+ orig = object.__getattribute__(obj, '_orig')
+ bmethod = method.encode('ascii')
+ func = getattr(orig, method)
+
+ # No read limit. Call original function.
+ if not remaining:
+ result = func(size)
+ obj._writelog(
+ b'%s(%d) -> (%d) %s' % (bmethod, size, len(result), result)
+ )
+ return result
+
+ origsize = size
+
+ if size < 0:
+ size = remaining
+ else:
+ size = min(remaining, size)
+
+ result = func(size)
+ remaining -= len(result)
+
+ obj._writelog(
+ b'%s(%d from %d) -> (%d) %s'
+ % (bmethod, size, origsize, len(result), result)
+ )
+
+ self.remaining_recv_bytes = remaining
+
+ if remaining <= 0:
+ obj._writelog(b'read limit reached; closing socket')
+ obj._cond_close()
+
+ # This is the easiest way to abort the current request.
+ raise Exception('connection closed after receiving N bytes')
+
+ return result
+
# We can't adjust __class__ on a socket instance. So we define a proxy type.
class socketproxy(object):
@@ -240,78 +285,12 @@
self._sock.shutdown(socket.SHUT_RDWR)
def read(self, size=-1):
- remaining = object.__getattribute__(self, '_cond').remaining_recv_bytes
-
- # No read limit. Call original function.
- if not remaining:
- result = object.__getattribute__(self, '_orig').read(size)
- self._writelog(
- b'read(%d) -> (%d) (%s) %s' % (size, len(result), result)
- )
- return result
-
- origsize = size
-
- if size < 0:
- size = remaining
- else:
- size = min(remaining, size)
-
- result = object.__getattribute__(self, '_orig').read(size)
- remaining -= len(result)
-
- self._writelog(
- b'read(%d from %d) -> (%d) %s'
- % (size, origsize, len(result), result)
- )
-
- object.__getattribute__(self, '_cond').remaining_recv_bytes = remaining
-
- if remaining <= 0:
- self._writelog(b'read limit reached; closing socket')
- self._close()
-
- # This is the easiest way to abort the current request.
- raise Exception('connection closed after receiving N bytes')
-
- return result
+ cond = object.__getattribute__(self, '_cond')
+ return cond.forward_read(self, 'read', size)
def readline(self, size=-1):
- remaining = object.__getattribute__(self, '_cond').remaining_recv_bytes
-
- # No read limit. Call original function.
- if not remaining:
- result = object.__getattribute__(self, '_orig').readline(size)
- self._writelog(
- b'readline(%d) -> (%d) %s' % (size, len(result), result)
- )
- return result
-
- origsize = size
-
- if size < 0:
- size = remaining
- else:
- size = min(remaining, size)
-
- result = object.__getattribute__(self, '_orig').readline(size)
- remaining -= len(result)
-
- self._writelog(
- b'readline(%d from %d) -> (%d) %s'
- % (size, origsize, len(result), result)
- )
-
- object.__getattribute__(self, '_cond').remaining_recv_bytes = remaining
-
- if remaining <= 0:
- self._writelog(b'read limit reached; closing socket')
- self._close()
-
- # This is the easiest way to abort the current request.
- raise Exception('connection closed after receiving N bytes')
-
- return result
+ cond = object.__getattribute__(self, '_cond')
+ return cond.forward_read(self, 'readline', size)
def write(self, data):
cond = object.__getattribute__(self, '_cond')
To: marmoute, #hg-reviewers, Alphare
Cc: mercurial-patches
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurial-scm.org/pipermail/mercurial-patches/attachments/20220124/9ebbb021/attachment-0002.html>
More information about the Mercurial-patches
mailing list