本文整理汇总了Python中googleapiclient.http.HttpRequest._sleep方法的典型用法代码示例。如果您正苦于以下问题:Python HttpRequest._sleep方法的具体用法?Python HttpRequest._sleep怎么用?Python HttpRequest._sleep使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类googleapiclient.http.HttpRequest
的用法示例。
在下文中一共展示了HttpRequest._sleep方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_media_io_base_next_chunk_no_retry_403_not_configured
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_media_io_base_next_chunk_no_retry_403_not_configured(self):
fd = BytesIO(b"i am png")
upload = MediaIoBaseUpload(
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
http = HttpMockSequence([
({'status': '403'}, NOT_CONFIGURED_RESPONSE),
({'status': '200'}, '{}')
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
headers={},
resumable=upload)
request._rand = lambda: 1.0
request._sleep = mock.MagicMock()
with self.assertRaises(HttpError):
request.execute(num_retries=3)
request._sleep.assert_not_called()
示例2: test_retry
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_retry(self):
num_retries = 5
resp_seq = [({'status': '500'}, '')] * num_retries
resp_seq.append(({'status': '200'}, '{}'))
http = HttpMockSequence(resp_seq)
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})
sleeptimes = []
request._sleep = lambda x: sleeptimes.append(x)
request._rand = lambda: 10
request.execute(num_retries=num_retries)
self.assertEqual(num_retries, len(sleeptimes))
for retry_num in xrange(num_retries):
self.assertEqual(10 * 2**(retry_num + 1), sleeptimes[retry_num])
示例3: test_retry_connection_errors_non_resumable
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_retry_connection_errors_non_resumable(self):
model = JsonModel()
request = HttpRequest(
HttpMockWithErrors(3, {'status': '200'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/json_api_endpoint')
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
response = request.execute(num_retries=3)
self.assertEqual({u'foo': u'bar'}, response)
示例4: test_no_retry_connection_errors
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_no_retry_connection_errors(self):
model = JsonModel()
request = HttpRequest(
HttpMockWithNonRetriableErrors(1, {'status': '200'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/json_api_endpoint')
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
with self.assertRaises(socket.error):
response = request.execute(num_retries=3)
示例5: test_retry_connection_errors_resumable
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_retry_connection_errors_resumable(self):
with open(datafile('small.png'), 'rb') as small_png_file:
small_png_fd = BytesIO(small_png_file.read())
upload = MediaIoBaseUpload(fd=small_png_fd, mimetype='image/png',
chunksize=500, resumable=True)
model = JsonModel()
request = HttpRequest(
HttpMockWithErrors(
3, {'status': '200', 'location': 'location'}, '{"foo": "bar"}'),
model.response,
u'https://www.example.com/file_upload',
method='POST',
resumable=upload)
request._sleep = lambda _x: 0 # do nothing
request._rand = lambda: 10
response = request.execute(num_retries=3)
self.assertEqual({u'foo': u'bar'}, response)
示例6: test_media_io_base_next_chunk_retries
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_media_io_base_next_chunk_retries(self):
try:
import io
except ImportError:
return
f = open(datafile('small.png'), 'r')
fd = io.BytesIO(f.read())
upload = MediaIoBaseUpload(
fd=fd, mimetype='image/png', chunksize=500, resumable=True)
# Simulate 5XXs for both the request that creates the resumable upload and
# the upload itself.
http = HttpMockSequence([
({'status': '500'}, ''),
({'status': '500'}, ''),
({'status': '503'}, ''),
({'status': '200', 'location': 'location'}, ''),
({'status': '500'}, ''),
({'status': '500'}, ''),
({'status': '503'}, ''),
({'status': '200'}, '{}'),
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/upload/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
headers={},
resumable=upload)
sleeptimes = []
request._sleep = lambda x: sleeptimes.append(x)
request._rand = lambda: 10
request.execute(num_retries=3)
self.assertEqual([20, 40, 80, 20, 40, 80], sleeptimes)
示例7: test_no_retry_401_fails_fast
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_no_retry_401_fails_fast(self):
http = HttpMockSequence([
({'status': '401'}, ''),
({'status': '200'}, '{}')
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})
request._rand = lambda: 1.0
request._sleep = mock.MagicMock()
with self.assertRaises(HttpError):
request.execute()
request._sleep.assert_not_called()
示例8: test_no_retry_fails_fast
# 需要导入模块: from googleapiclient.http import HttpRequest [as 别名]
# 或者: from googleapiclient.http.HttpRequest import _sleep [as 别名]
def test_no_retry_fails_fast(self):
http = HttpMockSequence([
({'status': '500'}, ''),
({'status': '200'}, '{}')
])
model = JsonModel()
uri = u'https://www.googleapis.com/someapi/v1/collection/?foo=bar'
method = u'POST'
request = HttpRequest(
http,
model.response,
uri,
method=method,
body=u'{}',
headers={'content-type': 'application/json'})
request._rand = lambda: 1.0
request._sleep = lambda _: self.fail('sleep should not have been called.')
try:
request.execute()
self.fail('Should have raised an exception.')
except HttpError:
pass