Пример #1
0
 def test_get_wait_time_limit_reached(self):
     """Should wait until end of period when limit is reached, regardless priority"""
     rule = SleepingRateLimitRule(short_limit=10, long_limit=100)
     self.assertEqual(42, rule._get_wait_time(10, 10, 42, 1000))
     self.assertEqual(42, rule._get_wait_time(1234, 10, 42, 1000))
     self.assertEqual(21, rule._get_wait_time(10, 100, 42, 21))
     self.assertEqual(21, rule._get_wait_time(10, 1234, 42, 21))
Пример #2
0
    def test_invocation_changed_limits(self):
        """Should update limits in case of changes, depending on limit enforcement"""
        self.test_response['X-RateLimit-Usage'] = '0, 0'
        self.test_response['X-RateLimit-Limit'] = '600, 30000'

        # without limit enforcement (default)
        rule = SleepingRateLimitRule()
        rule(self.test_response)
        self.assertEqual(600, rule.short_limit)
        self.assertEqual(30000, rule.long_limit)

        # with limit enforcement
        rule = SleepingRateLimitRule(force_limits=True)
        rule(self.test_response)
        self.assertEqual(10000, rule.short_limit)
        self.assertEqual(1000000, rule.long_limit)
Пример #3
0
 def test_invocation_unchanged_limits(self):
     """Should not update limits if these don't change"""
     self.test_response['X-RateLimit-Usage'] = '0, 0'
     self.test_response['X-RateLimit-Limit'] = '10000, 1000000'
     rule = SleepingRateLimitRule()
     self.assertEqual(10000, rule.short_limit)
     self.assertEqual(1000000, rule.long_limit)
     rule(self.test_response)
     self.assertEqual(10000, rule.short_limit)
     self.assertEqual(1000000, rule.long_limit)
Пример #4
0
 def test_get_wait_time_limit_reached(self):
     """Should wait until end of period when limit is reached, regardless priority"""
     rule = SleepingRateLimitRule(short_limit=10, long_limit=100)
     self.assertEqual(42, rule._get_wait_time(10, 10, 42, 1000))
     self.assertEqual(42, rule._get_wait_time(1234, 10, 42, 1000))
     self.assertEqual(21, rule._get_wait_time(10, 100, 42, 21))
     self.assertEqual(21, rule._get_wait_time(10, 1234, 42, 21))
Пример #5
0
def main(segment_id):
    client = stravalib.client.Client(
        access_token='37d4400d9157c0d7b0237e5a5568d9af41272462',
        rate_limiter=SleepingRateLimitRule(priority='low'))

    pos = POS
    main_seg = client.get_segment(segment_id=segment_id)
    priority_queue = PriorityQueue()
    ids = []

    j = 0
    for num in range(0, N, 1):
        print(num)
        for k in range(num):
            for l in range(num):
                for i in range(2):
                    pos = [
                        POS[0] + (POS[2] - POS[0]) * l / num + i *
                        (POS[2] - POS[0]) / 2,
                        POS[1] + (POS[3] - POS[1]) * k / num + i *
                        (POS[2] - POS[0]) / 2, POS[0] + (POS[2] - POS[0]) *
                        (l + 1) / num + i * (POS[2] - POS[0]) / 2,
                        POS[1] + (POS[3] - POS[1]) * (k + 1) / num + i *
                        (POS[2] - POS[0]) / 2
                    ]
                    for min_cat in range(6):
                        o = 0
                        while True:
                            o += 1
                            try:
                                segs = client.explore_segments(
                                    pos,
                                    activity_type='running',
                                    min_cat=min_cat,
                                    max_cat=min_cat + 1)
                            except:
                                time.sleep(min(o, 10))
                                continue
                            break
                        for seg in segs:
                            j += 1
                            if seg.id in ids:
                                continue
                            ids.append(seg.id)
                            w = round(
                                abs(main_seg.average_grade - seg.avg_grade)
                            ) + abs(main_seg.distance.num -
                                    seg.distance.num) / 5000.0
                            priority_queue.put((w, seg.id))

    print(j, len(ids))
    print()
    print()

    print(main_seg)
    print(main_seg.id, main_seg.average_grade, main_seg.distance.num)

    print()
    print()

    for k in range(10):
        closest_seg = client.get_segment(segment_id=priority_queue.get()[1])
        print()
        print(closest_seg)
        print(closest_seg.id, closest_seg.average_grade,
              closest_seg.distance.num)
Пример #6
0
 def test_get_wait_time_low_priority(self):
     """Should return number of seconds to next long limit divided by number of remaining requests
     for that period"""
     rule = SleepingRateLimitRule(priority='low', long_limit=11)
     self.assertEqual(1, rule._get_wait_time(1, 1, 1, 10))
     self.assertEqual(0.5, rule._get_wait_time(1, 1, 1, 5))
Пример #7
0
 def test_get_wait_time_medium_priority(self):
     """Should return number of seconds to next short limit divided by number of remaining requests
     for that period"""
     rule = SleepingRateLimitRule(priority='medium', short_limit=11)
     self.assertEqual(1, rule._get_wait_time(1, 1, 10, 1000))
     self.assertEqual(0.5, rule._get_wait_time(1, 1, 5, 1000))
Пример #8
0
 def test_get_wait_time_low_priority(self):
     """Should return number of seconds to next long limit divided by number of remaining requests
     for that period"""
     rule = SleepingRateLimitRule(priority='low', long_limit=11)
     self.assertEqual(1, rule._get_wait_time(1, 1, 1, 10))
     self.assertEqual(0.5, rule._get_wait_time(1, 1, 1, 5))
Пример #9
0
 def test_get_wait_time_medium_priority(self):
     """Should return number of seconds to next short limit divided by number of remaining requests
     for that period"""
     rule = SleepingRateLimitRule(priority='medium', short_limit=11)
     self.assertEqual(1, rule._get_wait_time(1, 1, 10, 1000))
     self.assertEqual(0.5, rule._get_wait_time(1, 1, 5, 1000))
Пример #10
0
 def test_get_wait_time_high_priority(self):
     """Should never sleep/wait after high priority requests"""
     self.assertEqual(
         0,
         SleepingRateLimitRule()._get_wait_time(42, 42, 60, 3600))
Пример #11
0
 def test_invalid_priority(self):
     """Should raise ValueError in case of invalid priority"""
     with self.assertRaises(ValueError):
         SleepingRateLimitRule(priority='foobar')