def testSendFileCancelledUpload(self): responses.add(responses.PUT, "https://foo.bar/quux", status=499) session = gcs.UploadSession("https://foo.bar/quux") with self.assertRaises(gcs.ResponseError): session.SendFile(io.BytesIO(b"foobar"))
def testSendFileEmpty(self): handler = gcs_test_lib.FakeUploadHandler() responses.add_callback(responses.PUT, "https://foo.bar/qux", handler) session = gcs.UploadSession("https://foo.bar/qux") session.SendFile(io.BytesIO(b"")) self.assertEqual(handler.content, b"")
def testSendFileIncorrectResponseLastChunk(self): responses.add(responses.PUT, "https://foo.bar/quux", status=301) session = gcs.UploadSession("https://foo.bar/quux") opts = gcs.UploadSession.Opts() opts.chunk_size = 1024 with self.assertRaisesRegex(gcs.ResponseError, "final chunk"): session.SendFile(io.BytesIO(b"foobar"), opts=opts)
def testSendFileInterrupted(self): responses.add(responses.PUT, "https://foo.bar/quux", status=503) opts = gcs.UploadSession.Opts() opts.retry_chunk_attempts = 1 opts.retry_chunk_init_delay = 0.0 session = gcs.UploadSession("https://foo.bar/quux") with self.assertRaises(gcs.InterruptedResponseError): session.SendFile(io.BytesIO(b"foobar"), opts=opts)
def testSendFileMultipleChunks(self): handler = gcs_test_lib.FakeUploadHandler() responses.add_callback(responses.PUT, "https://foo.bar/qux", handler) opts = gcs.UploadSession.Opts() opts.chunk_size = 1 session = gcs.UploadSession("https://foo.bar/qux") session.SendFile(io.BytesIO(b"foobar"), opts=opts) self.assertEqual(handler.content, b"foobar")
def testSendFileTransmissionFailure(self): unused_port = portpicker.pick_unused_port() session = gcs.UploadSession(f"https://localhost:{unused_port}") opts = gcs.UploadSession.Opts() opts.retry_chunk_attempts = 1 opts.retry_chunk_init_delay = 0.0 with self.assertRaises(gcs.RequestError) as context: session.SendFile(io.BytesIO(b"foobar"), opts=opts) cause = context.exception.__cause__ self.assertIsInstance(cause, exceptions.ConnectionError)
def testSendFileRetrySuccess(self): handler = gcs_test_lib.FakeUploadHandler() responses.add(responses.PUT, "https://foo.bar/qux", status=502) responses.add(responses.PUT, "https://foo.bar/qux", status=503) responses.add(responses.PUT, "https://foo.bar/qux", status=504) responses.add_callback(responses.PUT, "https://foo.bar/qux", handler) opts = gcs.UploadSession.Opts() opts.chunk_size = 1 opts.retry_chunk_attempts = 4 opts.retry_chunk_init_delay = 0.0 session = gcs.UploadSession("https://foo.bar/qux") session.SendFile(io.BytesIO(b"foobar"), opts=opts) self.assertEqual(handler.content, b"foobar")
def testSendFileRetryFailure(self): handler = gcs_test_lib.FakeUploadHandler() responses.add(responses.PUT, "https://foo.bar/qux", status=502) responses.add(responses.PUT, "https://foo.bar/qux", status=503) responses.add(responses.PUT, "https://foo.bar/qux", status=504) responses.add_callback(responses.PUT, "https://foo.bar/qux", handler) opts = gcs.UploadSession.Opts() opts.chunk_size = 1 opts.retry_chunk_attempts = 3 opts.retry_chunk_init_delay = 0.0 session = gcs.UploadSession("https://foo.bar/qux") with self.assertRaises(gcs.InterruptedResponseError) as context: session.SendFile(io.BytesIO(b"foobar"), opts=opts) self.assertEqual(context.exception.response.status_code, 504)
def testSendFileChunkProgress(self): data = b"foobar" handler = gcs_test_lib.FakeUploadHandler() responses.add_callback(responses.PUT, "https://foo.bar/qux", handler) counter = 0 def Progress() -> None: nonlocal counter counter += 1 opts = gcs.UploadSession.Opts() opts.chunk_size = 1 opts.progress_callback = Progress session = gcs.UploadSession("https://foo.bar/qux") session.SendFile(io.BytesIO(data), opts=opts) self.assertGreaterEqual(counter, len(data))
def testSendFileRetryProgress(self): responses.add(responses.PUT, "https://foo.bar/qux", status=503) counter = 0 def Progress() -> None: nonlocal counter counter += 1 opts = gcs.UploadSession.Opts() opts.retry_chunk_attempts = 2 opts.retry_chunk_init_delay = 10.0 opts.progress_interval = 1.0 opts.progress_callback = Progress session = gcs.UploadSession("https://foo.bar/qux") with self.assertRaises(gcs.InterruptedResponseError): session.SendFile(io.BytesIO(b"foobar"), opts=opts) # We should sleep for 10 seconds and do progress calls every second, so it # should be called at least 10 times. self.assertGreaterEqual(counter, 10)