summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
authorBruce Merry <bmerry@ska.ac.za>2020-06-25 08:30:21 +0200
committerGitHub <noreply@github.com>2020-06-24 23:30:21 -0700
commit152f0b8beea12e6282d284100b600771b968927a (patch)
treebe477e3f5f3886186aff7d183d21772beb8b607b /Lib/test
parentcf18c9e9d4d44f6671a3fe6011bb53d8ee9bd92b (diff)
downloadcpython-git-152f0b8beea12e6282d284100b600771b968927a.tar.gz
bpo-41002: Optimize HTTPResponse.read with a given amount (GH-20943)
I've done the implementation for both non-chunked and chunked reads. I haven't benchmarked chunked reads because I don't currently have a convenient way to generate a high-bandwidth chunked stream, but I don't see any reason that it shouldn't enjoy the same benefits that the non-chunked case does. I've used the benchmark attached to the bpo bug to verify that performance now matches the unsized read case. Automerge-Triggered-By: @methane
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_httplib.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/test/test_httplib.py b/Lib/test/test_httplib.py
index e95487bcd4..e909980d23 100644
--- a/Lib/test/test_httplib.py
+++ b/Lib/test/test_httplib.py
@@ -569,6 +569,33 @@ class BasicTest(TestCase):
resp.close()
self.assertTrue(resp.closed)
+ def test_partial_reads_past_end(self):
+ # if we have Content-Length, clip reads to the end
+ body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
+ sock = FakeSocket(body)
+ resp = client.HTTPResponse(sock)
+ resp.begin()
+ self.assertEqual(resp.read(10), b'Text')
+ self.assertTrue(resp.isclosed())
+ self.assertFalse(resp.closed)
+ resp.close()
+ self.assertTrue(resp.closed)
+
+ def test_partial_readintos_past_end(self):
+ # if we have Content-Length, clip readintos to the end
+ body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText"
+ sock = FakeSocket(body)
+ resp = client.HTTPResponse(sock)
+ resp.begin()
+ b = bytearray(10)
+ n = resp.readinto(b)
+ self.assertEqual(n, 4)
+ self.assertEqual(bytes(b)[:4], b'Text')
+ self.assertTrue(resp.isclosed())
+ self.assertFalse(resp.closed)
+ resp.close()
+ self.assertTrue(resp.closed)
+
def test_partial_reads_no_content_length(self):
# when no length is present, the socket should be gracefully closed when
# all data was read