コード例 #1
0
ファイル: tester.py プロジェクト: DrCyanide/Riot-Rate-Limiter
def testScenario():
    print('Starting scenario tests...')
    # Mimic a normal flow

    platform = Platform()
    summoner_url = 'https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/SomeSummonerName'
    platform.addURL(summoner_url)
    assert (platform.count == 1)
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == summoner_url)
    assert (platform_limit_needed == True)
    assert (endpoint_limit_needed == True)

    headers = {
        'X-Method-Rate-Limit': '1:0.1,10:1',
        'X-Method-Rate-Limit-Count': '1:0.1,1:1',
        'X-App-Rate-Limit': '5:0.1,40:1',
        'X-App-Rate-Limit-Count': '1:0.1,1:1'
    }
    platform.setLimitAndCount(headers)
    platform.setEndpointLimitAndCount(url, headers)
    assert (platform.available() == False)  # Method limit hit
    matchlist_url = 'https://na1.api.riotgames.com/lol/match/v3/matchlists/by-account/123456789'
    platform.addURL(matchlist_url)
    assert (platform.available())  # New endpoint without a method limit hit
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == matchlist_url)
    assert (platform_limit_needed == False)
    assert (endpoint_limit_needed == True)

    headers = {
        'X-Method-Rate-Limit': '1:0.1,10:1',
        'X-Method-Rate-Limit-Count': '1:0.1,1:1',
        'X-App-Rate-Limit': '5:0.1,40:1',
        'X-App-Rate-Limit-Count': '2:0.1,2:1'
    }
    platform.setEndpointLimitAndCount(url, headers)
    assert (platform.available() == False)  # Method limit hit
    match_example = 'https://na1.api.riotgames.com/lol/match/v3/matches/'
    #print('Generating Matches')
    for matchid in range(1, 50):
        url = '%s%s' % (match_example, matchid)
        #print('%s -> %s'%(url, Endpoint.identifyEndpoint(url)))
        platform.addURL(url)
    assert (platform.count == 49)
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == '%s%s' % (match_example, 1))
    assert (platform_limit_needed == False)
    assert (endpoint_limit_needed == True)

    headers = {
        'X-Method-Rate-Limit': '1:0.1,5:1',  # Note: 5 per 1 second
        'X-Method-Rate-Limit-Count': '1:0.1,1:1',
        'X-App-Rate-Limit': '5:0.1,40:1',
        'X-App-Rate-Limit-Count': '3:0.1,3:1'
    }
    platform.setEndpointLimitAndCount('%s%s' % (match_example, 1), headers)
    #print('Checking available')
    #print(platform.getUsage())
    assert (platform.available() == False)  # Method limit 1:0.1, 1:1
    time.sleep(0.1)  # Time = 0.1
    assert (platform.available())
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == '%s%s' % (match_example, 2))
    assert (platform_limit_needed == False)
    assert (endpoint_limit_needed == False)

    assert (platform.available() == False)  # Method limit 1:0.1, 2:1
    time.sleep(0.1)  # Time = 0.2
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == '%s%s' % (match_example, 3))
    assert (platform_limit_needed == False)
    assert (endpoint_limit_needed == False)

    assert (platform.available() == False)  # Method limit 1:0.1, 3:1
    time.sleep(0.1)  # Time = 0.3
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == '%s%s' % (match_example, 4))
    assert (platform_limit_needed == False)
    assert (endpoint_limit_needed == False)

    assert (platform.available() == False)  # Method limit 1:0.1, 4:1
    time.sleep(0.1)  # Time = 0.4
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == '%s%s' % (match_example, 5))
    assert (platform_limit_needed == False)
    assert (endpoint_limit_needed == False)

    assert (platform.available() == False)  # Method Limit 1:0.1, 5:1
    time.sleep(0.1)  # Time = 0.5
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == None)
    assert (platform_limit_needed == False)
    assert (endpoint_limit_needed == False)

    assert (platform.available() == False)  # Method Limit 0:0.1, 5:1
    time.sleep(0.1)  # Time = 0.6
    assert (platform.available() == False)  # Method Limit 0:0.1, 5:1
    time.sleep(0.1)  # Time = 0.7
    assert (platform.available() == False)  # Method Limit 0:0.1, 5:1
    time.sleep(0.1)  # Time = 0.8
    assert (platform.available() == False)  # Method Limit 0:0.1, 5:1
    time.sleep(0.1)  # Time = 0.9
    assert (platform.available() == False)  # Method Limit 0:0.1, 5:1
    time.sleep(0.1)  # Time = 1.0
    assert (platform.available())  # Method Limit 0:0.1, 0:1

    # Check that requests from other platforms still go through just fine without causing a delay
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == '%s%s' % (match_example, 6))
    platform.addURL(summoner_url)
    assert (platform.available())
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == summoner_url)
    assert (platform.available() == False)
    time.sleep(0.1)
    url, platform_limit_needed, endpoint_limit_needed = platform.get()
    assert (url == '%s%s' % (match_example, 7))

    print('First 8 manual tests passed, the rest should be staggered')
    # Automatically continue
    next = 8
    while platform.count > 0:
        url, platform_limit_needed, endpoint_limit_needed = grabWhenReady(
            platform)
        assert (url == '%s%s' % (match_example, next))
        print(next)
        next += 1

    print('Scenario tests pass')
コード例 #2
0
ファイル: tester.py プロジェクト: DrCyanide/Riot-Rate-Limiter
def testPlatform():
    platform = Platform()
    assert (platform.rate_limit_ok())
    assert (platform.has_url() == False)
    assert (platform.available() == False)  # no URLS
    assert (platform.time_next_available() == None)
    assert (platform.count == 0)
    assert (platform.get() == (None, True, False))

    summoner_url = 'https://na1.api.riotgames.com/lol/summoner/v3/summoners/by-name/SomeSummonerName'
    platform.addURL(summoner_url)
    assert (platform.count == 1)
    assert (platform.static_count == 0)
    assert (platform.limited_count == 1)
    assert (platform.time_next_available() < time.time())
    assert (platform.available())

    static_url = 'https://na1.api.riotgames.com/lol/static-data/v3/champions?locale=en_US&dataById=false'
    platform.addURL(static_url)
    assert (platform.count == 2)
    assert (platform.static_count == 1)
    assert (platform.limited_count == 1)
    assert (platform.time_next_available() < time.time())
    assert (platform.available())

    # When two are present, static should be pulled first
    assert (platform.get() == (static_url, True, True))
    assert (platform.count == 1)
    assert (platform.static_count == 0)
    assert (platform.get() == (summoner_url, True, True))
    assert (platform.count == 0)

    platform = Platform()
    champ_mastery_url = 'https://na1.api.riotgames.com/lol/champion-mastery/v3/champion-masteries/by-summoner/28341307'
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    platform.addURL(champ_mastery_url)
    platform.addURL(champ_mastery_url)
    assert (platform.count == 4)
    assert (platform.last_limited_endpoint == '')
    # summoner_url was added first, so that endpoint gets pulled first
    # it then rotates to champ_mastery_url, the next endpoint added
    assert (platform.get() == (summoner_url, True, True))
    assert (platform.get() == (champ_mastery_url, True, True))
    assert (platform.get() == (summoner_url, True, True))
    assert (platform.get() == (champ_mastery_url, True, True))

    headers = {
        'X-Method-Rate-Limit': '1:0.1,10:1',
        'X-Method-Rate-Limit-Count': '0:0.1,9:1',
        'X-App-Rate-Limit': '5:0.1,40:1',
        'X-App-Rate-Limit-Count': '0:0.1,39:1'
    }
    platform.setLimit(headers)
    platform.setCount(headers)
    assert (platform.rate_limit_ok())
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    assert (platform.get() == (summoner_url, False, True))
    assert (platform.rate_limit_ok() == False)
    assert (platform.get() == (None, False, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    platform.setLimitAndCount(headers)
    assert (platform.rate_limit_ok())
    assert (platform.get() == (summoner_url, False, True))
    assert (platform.rate_limit_ok() == False)
    platform.addURL(summoner_url)
    assert (platform.get() == (None, False, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.addURL(summoner_url)
    platform.setEndpointLimit(summoner_url, headers)
    platform.setEndpointCount(summoner_url, headers)
    assert (platform.get() == (summoner_url, True, False))
    assert (platform.get() == (None, True, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.setEndpointLimitAndCount(summoner_url, headers)
    assert (platform.get() == (summoner_url, True, False))

    platform = Platform()
    platform.addURL(summoner_url)
    platform.setLimitAndCount(headers)
    platform.setEndpointLimitAndCount(summoner_url, headers)
    assert (platform.get() == (summoner_url, False, False))

    print('Platform tests pass')