async def test_resume_session_abandoned_after_a_long_time( time: FakeTime, drive: DriveSource, config: Config, server: SimulationServer, snapshot_helper, interceptor: RequestInterceptor): from_snapshot, data = await snapshot_helper.createFile() # Configure the upload to fail after the first upload chunk interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, 501, 1) with pytest.raises(ClientResponseError): await drive.save(from_snapshot, data) # Verify it reuses the session a few times assert server.wasUrlRequested(URL_START_UPLOAD) assert drive.drivebackend.last_attempt_count == 1 assert drive.drivebackend.last_attempt_location is not None assert drive.drivebackend.last_attempt_metadata is not None data.position(0) with pytest.raises(ClientResponseError): await drive.save(from_snapshot, data) assert drive.drivebackend.last_attempt_count == 2 assert drive.drivebackend.last_attempt_location is not None assert drive.drivebackend.last_attempt_metadata is not None last_location = drive.drivebackend.last_attempt_location # Fast forward a lot, then verify the session is restarted server.urls.clear() interceptor.clear() time.advance(duration=UPLOAD_SESSION_EXPIRATION_DURATION) data.position(0) await drive.save(from_snapshot, data) assert interceptor.urlWasCalled(URL_START_UPLOAD) assert not interceptor.urlWasCalled(last_location)
async def verify_upload_resumed(time, drive: DriveSource, config: Config, server: SimulationServer, interceptor: RequestInterceptor, status, snapshot_helper, expected=ClientResponseError): from_snapshot, data = await snapshot_helper.createFile() # Configure the upload to fail after the first upload chunk interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, status, 1) with pytest.raises(expected): await drive.save(from_snapshot, data) # Verify a requst was made to start the upload assert server.wasUrlRequested( "/upload/drive/v3/files/?uploadType=resumable&supportsAllDrives=true") assert drive.drivebackend.last_attempt_location is not None assert drive.drivebackend.last_attempt_metadata is not None last_location = drive.drivebackend.last_attempt_location # Retry the upload and let is succeed server.urls.clear() interceptor.clear() data.position(0) snapshot = await drive.save(from_snapshot, data) # We shoudl nto see the upload "initialize" url assert not server.wasUrlRequested( "/upload/drive/v3/files/?uploadType=resumable&supportsAllDrives=true") # We should see the last location url (which has a unique token) reused to resume the upload assert server.wasUrlRequested(last_location) # The saved metadata should be cleared out. assert drive.drivebackend.last_attempt_count == 1 assert drive.drivebackend.last_attempt_location is None assert drive.drivebackend.last_attempt_metadata is None # Verify the uploaded bytes are identical from_snapshot.addSource(snapshot) download = await drive.read(from_snapshot) data.position(0) await compareStreams(data, download)
async def test_resume_session_reused_abonded_after_retries( time, drive: DriveSource, config: Config, server: SimulationServer, snapshot_helper, interceptor: RequestInterceptor): from_snapshot, data = await snapshot_helper.createFile() # Configure the upload to fail after the first upload chunk interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, 501, 1) with pytest.raises(ClientResponseError): await drive.save(from_snapshot, data) # Verify a requst was made to start the upload but not cached assert server.wasUrlRequested(URL_START_UPLOAD) assert drive.drivebackend.last_attempt_count == 1 assert drive.drivebackend.last_attempt_location is not None assert drive.drivebackend.last_attempt_metadata is not None last_location = drive.drivebackend.last_attempt_location for x in range(1, RETRY_SESSION_ATTEMPTS): server.urls.clear() interceptor.clear() interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, 501) data.position(0) with pytest.raises(ClientResponseError): await drive.save(from_snapshot, data) assert not server.wasUrlRequested(URL_START_UPLOAD) assert server.wasUrlRequested(last_location) assert drive.drivebackend.last_attempt_count == x + 1 assert drive.drivebackend.last_attempt_location is last_location assert drive.drivebackend.last_attempt_metadata is not None # Next attempt should give up and restart the upload server.urls.clear() interceptor.clear() interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, 501, 1) data.position(0) with pytest.raises(ClientResponseError): await drive.save(from_snapshot, data) assert server.wasUrlRequested(URL_START_UPLOAD) assert not server.wasUrlRequested(last_location) assert drive.drivebackend.last_attempt_count == 1 # upload again, which should retry server.urls.clear() interceptor.clear() data.position(0) snapshot = await drive.save(from_snapshot, data) assert not server.wasUrlRequested(URL_START_UPLOAD) # Verify the uploaded bytes are identical from_snapshot.addSource(snapshot) download = await drive.read(from_snapshot) data.position(0) await compareStreams(data, download)
async def test_resume_session_reused_on_http410(time, drive: DriveSource, config: Config, server: SimulationServer, backup_helper: BackupHelper, interceptor: RequestInterceptor): from_backup, data = await backup_helper.createFile() # Configure the upload to fail interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, 500) with pytest.raises(GoogleInternalError): await drive.save(from_backup, data) # Verify a requst was made to start the upload assert server.wasUrlRequested(URL_START_UPLOAD) assert drive.drivebackend.last_attempt_location is not None server.urls.clear() interceptor.clear() data.position(0) interceptor.setError(drive.drivebackend.last_attempt_location, 0, 410) await drive.save(from_backup, data)
async def test_resume_session_reused_on_http408( time, drive: DriveSource, config: Config, server: SimulationServer, snapshot_helper: SnapshotHelper, interceptor: RequestInterceptor): from_snapshot, data = await snapshot_helper.createFile() # Configure the upload to fail interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, 408) with pytest.raises(GoogleTimeoutError): await drive.save(from_snapshot, data) # Verify a requst was made to start the upload assert server.wasUrlRequested(URL_START_UPLOAD) location = drive.drivebackend.last_attempt_location assert location is not None server.urls.clear() interceptor.clear() data.position(0) await drive.save(from_snapshot, data) assert interceptor.urlWasCalled(URL(location).path)
async def test_resume_session_reused_on_http410( time, drive: DriveSource, config: Config, server: SimulationServer, snapshot_helper: SnapshotHelper, interceptor: RequestInterceptor): from_snapshot, data = await snapshot_helper.createFile() # Configure the upload to fail interceptor.setError(URL_MATCH_UPLOAD_PROGRESS, 500) with pytest.raises(GoogleInternalError): await drive.save(from_snapshot, data) # Verify a requst was made to start the upload assert server.wasUrlRequested( "/upload/drive/v3/files/?uploadType=resumable&supportsAllDrives=true") assert drive.drivebackend.last_attempt_location is not None server.urls.clear() interceptor.clear() data.position(0) interceptor.setError(drive.drivebackend.last_attempt_location, 0, 410) await drive.save(from_snapshot, data)