Mercurial > hg
comparison tests/testlib/badserverext.py @ 48616:98c502a2c462
test-http-bad-server: refactor the writing logic to avoid early return
Our ultimate goal is to add another way to define the connection needs to be
closed. To do so, we need the "read" code to be more unified.
Differential Revision: https://phab.mercurial-scm.org/D12047
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Fri, 21 Jan 2022 12:44:39 +0100 |
parents | e38776a4c2cb |
children | 9642dbe7bca1 |
comparison
equal
deleted
inserted
replaced
48615:e38776a4c2cb | 48616:98c502a2c462 |
---|---|
106 remaining = self.remaining_send_bytes | 106 remaining = self.remaining_send_bytes |
107 | 107 |
108 orig = object.__getattribute__(obj, '_orig') | 108 orig = object.__getattribute__(obj, '_orig') |
109 bmethod = method.encode('ascii') | 109 bmethod = method.encode('ascii') |
110 func = getattr(orig, method) | 110 func = getattr(orig, method) |
111 # No byte limit on this operation. Call original function. | 111 |
112 if remaining: | |
113 remaining = max(0, remaining) | |
114 | |
112 if not remaining: | 115 if not remaining: |
113 result = func(data, *args, **kwargs) | 116 newdata = data |
114 obj._writelog(b'%s(%d) -> %s' % (bmethod, len(data), data)) | 117 else: |
115 return result | |
116 | |
117 remaining = max(0, remaining) | |
118 | |
119 if remaining > 0: | |
120 if remaining < len(data): | 118 if remaining < len(data): |
121 newdata = data[0:remaining] | 119 newdata = data[0:remaining] |
122 else: | 120 else: |
123 newdata = data | 121 newdata = data |
124 | |
125 remaining -= len(newdata) | 122 remaining -= len(newdata) |
126 | 123 self.remaining_send_bytes = remaining |
124 | |
125 result = func(newdata, *args, **kwargs) | |
126 | |
127 if remaining is None: | |
128 obj._writelog(b'%s(%d) -> %s' % (bmethod, len(data), data)) | |
129 else: | |
127 obj._writelog( | 130 obj._writelog( |
128 b'%s(%d from %d) -> (%d) %s' | 131 b'%s(%d from %d) -> (%d) %s' |
129 % ( | 132 % ( |
130 bmethod, | 133 bmethod, |
131 len(newdata), | 134 len(newdata), |
133 remaining, | 136 remaining, |
134 newdata, | 137 newdata, |
135 ) | 138 ) |
136 ) | 139 ) |
137 | 140 |
138 result = func(newdata, *args, **kwargs) | 141 if remaining is not None and remaining <= 0: |
139 | |
140 self.remaining_send_bytes = remaining | |
141 | |
142 if remaining <= 0: | |
143 obj._writelog(b'write limit reached; closing socket') | 142 obj._writelog(b'write limit reached; closing socket') |
144 object.__getattribute__(obj, '_cond_close')() | 143 object.__getattribute__(obj, '_cond_close')() |
145 raise Exception('connection closed after sending N bytes') | 144 raise Exception('connection closed after sending N bytes') |
146 | 145 |
147 return result | 146 return result |