Skip to content

Commit

Permalink
Tidy error checking+reporting; remove @skip_data; remove unused impor…
Browse files Browse the repository at this point in the history
…ts.Â
  • Loading branch information
pp-mo committed Mar 27, 2019
1 parent 3b72e9b commit 951c6fd
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/iris/tests/test_image_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,42 +29,44 @@
import json
import os
import requests
import unittest
import time


@tests.skip_inet
@tests.skip_data
class TestImageFile(tests.IrisTest):
def test_resolve(self):
# https://developer.github.com/v3/#user-agent-required
headers = {'User-Agent': 'scitools-bot'}
rate_limit_uri = 'https://api.github.com/rate_limit'
rl = requests.get(rate_limit_uri, headers=headers)
some_left = False
if rl.status_code == 200:
rates = rl.json()
remaining = rates.get('rate', {})
ghapi_remaining = remaining.get('remaining')
else:
ghapi_remaining = 0
if rl.status_code != 200:
msg = 'Get request on {} failed : rc={}.'
raise ValueError(msg.format(rate_limit_uri, rl.status_code))

# Only run this test if there are IP based rate limited calls left.
# Abort test if there are < 3 IP based rate limited calls left.
# 3 is an engineering tolerance, in case of race conditions.
amin = 3
if ghapi_remaining < amin:
rl_json = rl.json()
rate_info = rl_json.get('rate', {})
reqs_left, reset_level, next_time_epoch_secs = (
rate_info.get(key)
for key in ('remaining', 'limit', 'reset'))
if any(not isinstance(value, int)
for value in (reqs_left, reset_level, next_time_epoch_secs)):
msg = 'Failed to parse result of GET {} : result={}'.format(
rate_limit_uri, rl_json)
raise ValueError(msg)

min_requests = 3
if reqs_left < min_requests:
msg = ('This test failed because it hit a GitHub web API request '
'rate-limit, \n'
'for anonymous requests by "User-Agent: scitools-bot".\n'
'N-calls-left is {}, less than margin allowance of {}.\n'
'Next reset due : {} requests at {}\n.'
'Please retry after that time.')
reset_level = rates['rate']['limit']
next_time_epoch_secs = rates['rate']['reset']
from cf_units import Unit
reset_date = Unit('seconds since epoch').num2date(
next_time_epoch_secs)
raise ValueError(msg.format(ghapi_remaining, amin,
raise ValueError(msg.format(reqs_left, min_requests,
reset_level, reset_date))

iuri = ('https://api.github.com/repos/scitools/'
Expand Down

0 comments on commit 951c6fd

Please sign in to comment.