Skip to content

Commit 04a9ffc

Browse files
committed
Merge pull request #34 from codebendercc/disqus-stop-when-api-rate-limit-reached
Stopping tests when Disqus API rate limit reached
2 parents 5549c40 + 55445dd commit 04a9ffc

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

bin/seleniumbender.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,20 @@ def send_mail_with_logs(self, identifier):
6969
logfile_timestamp = re.match(r'(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-.+', logfile).group(1)
7070

7171
report_regexp = re.compile('report_{timestamp}-{identifier}_(\d+)'.format(timestamp=logfile_timestamp, identifier=identifier))
72-
reportfile = sorted([filename for filename in os.listdir(reports) if filename not in self.files_to_ignore and report_regexp.match(filename)], reverse=True)[0]
73-
changes = report_regexp.match(reportfile).group(1)
74-
75-
email_date = time.strftime('%Y-%m-%d %H:%M:%S')
76-
77-
command = [
78-
'(echo "Changes since the last time: {changes}";'.format(changes=changes),
79-
'uuencode "{logs}/{logfile}" "{logfile}";'.format(logs=logs, logfile=logfile),
80-
'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile),
81-
'| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email)
82-
]
83-
self.run_command(command)
72+
reportfile = sorted([filename for filename in os.listdir(reports) if filename not in self.files_to_ignore and report_regexp.match(filename)], reverse=True)
73+
if reportfile:
74+
reportfile = reportfile[0]
75+
changes = report_regexp.match(reportfile).group(1)
76+
77+
email_date = time.strftime('%Y-%m-%d %H:%M:%S')
78+
79+
command = [
80+
'(echo "Changes since the last time: {changes}";'.format(changes=changes),
81+
'uuencode "{logs}/{logfile}" "{logfile}";'.format(logs=logs, logfile=logfile),
82+
'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile),
83+
'| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email)
84+
]
85+
self.run_command(command)
8486

8587
def create_command(self, test_directory, *extra_arguments):
8688
return ['tox', 'tests/' + test_directory, '--', '--url={}'.format(TARGETS[self.url])] + list(extra_arguments)

codebender_testing/disqus.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
# -*- coding: utf-8 -*-
33

44
from codebender_testing.config import get_path
5+
import pytest
56
import disqusapi
67
import simplejson
8+
import requests
79
import base64
810
import hashlib
911
import hmac
@@ -16,6 +18,7 @@
1618
AUTHOR_URL = os.getenv('AUTHOR_URL', 'https://codebender.cc/user/codebender')
1719
DISQUS_REQUESTS_PER_HOUR = 1000
1820
DISQUS_WAIT = (DISQUS_REQUESTS_PER_HOUR / 60) / 60
21+
DISQUS_MIN_REMANING_REQUESTS = 10
1922
CHANGE_LOG = 'examples_compile_log.json'
2023
DISQUS_COMMENTS = 'disqus_comments.json'
2124
EXAMPLES_WITHOUT_LIBRARY_DB = 'examples_without_library.json'
@@ -58,6 +61,10 @@ def get_disqus_sso(self, user):
5861
return "{0} {1} {2}".format(message, sig, timestamp)
5962

6063
def update_comment(self, sketch, results, current_date, log_entry, openFailFlag, total_sketches):
64+
check_rate_limit = self.check_rate_limit()
65+
if not check_rate_limit:
66+
pytest.exit('Disqus API rate limit reached')
67+
6168
"""A comment is added to the library as soon as its first example is compiled.
6269
`library`: The library in which belongs the currently compiled example.
6370
`self.last_library`: The library in which belongs the previously compiled example.
@@ -250,3 +257,16 @@ def update_post(self, post_id, message):
250257
print 'Error:', error
251258

252259
return comment_status
260+
261+
def check_rate_limit(self):
262+
usage_check = False
263+
try:
264+
r = requests.get('https://disqus.com/api/3.0/applications/listUsage.json?api_secret=' + self.DISQUS_API_SECRET + '&access_token=' + self.DISQUS_ACCESS_TOKEN)
265+
api_remaning_limit = r.headers['X-Ratelimit-Remaining']
266+
if api_remaning_limit > DISQUS_MIN_REMANING_REQUESTS:
267+
usage_check = True
268+
269+
except Exception as error:
270+
print 'Error:', error
271+
272+
return usage_check

codebender_testing/utils.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,7 @@ def open_all_libraries_and_examples(self, url, logfile):
543543

544544
print '\nVisiting:', len(urls_to_visit), 'URLs'
545545
tic = time.time()
546+
toc = tic
546547
for url in urls_to_visit:
547548
self.open(url)
548549
self.get_element(By.CSS_SELECTOR, '#mycontainer')
@@ -578,6 +579,8 @@ def open_all_libraries_and_examples(self, url, logfile):
578579

579580
report_creator('fetch', log_entry, log_file)
580581

582+
print '\nTest duration:', int(toc - tic), 'sec'
583+
581584
def compile_sketch(self, url, boards, iframe=False, project_view=False):
582585
"""Compiles the sketch located at `url`, or an iframe within the page
583586
referred to by `url`. Raises an exception if it does not compile.
@@ -643,6 +646,7 @@ def comment_compile_libraries_examples(self, sketches, library_examples_dic={},
643646

644647
total_sketches = len(urls_to_visit)
645648
tic = time.time()
649+
toc = tic
646650
library_re = re.compile(r'^(.+)/library/.+$')
647651

648652
for url in urls_to_visit:
@@ -752,6 +756,7 @@ def comment_compile_libraries_examples(self, sketches, library_examples_dic={},
752756
# Generate a report if requested.
753757
if compile_type != 'target_library' and create_report and self.run_full_compile_tests:
754758
report_creator(compile_type, log_entry, log_file)
759+
755760
print '\nTest duration:', int(toc - tic), 'sec'
756761

757762
def compile_all_sketches(self, url, selector, **kwargs):
@@ -906,9 +911,7 @@ def change_privacy(self, privacy):
906911
privateRadioButton.click()
907912

908913
def change_name(self, name):
909-
print name
910914
nameField = self.get_element(By.CSS_SELECTOR,'#create-sketch-modal .modal-body [id="create-sketch-name"')
911-
print nameField
912915
nameField.clear()
913916
nameField.send_keys(name)
914917
nameField.send_keys(Keys.ENTER)

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ pyyaml
88
pytest
99
selenium
1010
simplejson
11-
disqus-python
11+
disqus-python
12+
requests

0 commit comments

Comments
 (0)