-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_crawler.py
More file actions
39 lines (32 loc) · 1.32 KB
/
test_crawler.py
File metadata and controls
39 lines (32 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import time
from concurrent.futures import ThreadPoolExecutor
from unittest import mock
import pytest
from gitbugactions.github_api import CoreRateLimiter, GithubAPI, SearchRateLimiter
def test_rate_limiter():
rate_limiters = [CoreRateLimiter(), SearchRateLimiter()]
for rate_limiter in rate_limiters:
executor = ThreadPoolExecutor(max_workers=2)
rate_limiter.lock.acquire()
assert rate_limiter.lock.locked()
executor.submit(rate_limiter.request, (lambda x: x + 1, 2))
executor.submit(rate_limiter.request, (lambda x: x + 1, 2))
time.sleep(1)
assert rate_limiter.requests == 0
rate_limiter.lock.release()
time.sleep(1)
assert rate_limiter.requests == 2
@pytest.mark.first
def test_github_api():
with mock.patch("github.Github.get_emojis") as get_emojis:
github = GithubAPI()
github.get_emojis()
github.get_emojis()
assert get_emojis.call_count == 2
assert github.token.core_rate_limiter.requests == 2
with mock.patch("github.Github.search_repositories") as search_repositories:
github = GithubAPI()
github.search_repositories("test")
github.search_repositories("test")
assert search_repositories.call_count == 2
assert github.token.search_rate_limiter.requests == 2