Skip to content

Stopping tests when Disqus API rate limit reached #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions bin/seleniumbender.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,20 @@ def send_mail_with_logs(self, identifier):
logfile_timestamp = re.match(r'(\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2})-.+', logfile).group(1)

report_regexp = re.compile('report_{timestamp}-{identifier}_(\d+)'.format(timestamp=logfile_timestamp, identifier=identifier))
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]
changes = report_regexp.match(reportfile).group(1)

email_date = time.strftime('%Y-%m-%d %H:%M:%S')

command = [
'(echo "Changes since the last time: {changes}";'.format(changes=changes),
'uuencode "{logs}/{logfile}" "{logfile}";'.format(logs=logs, logfile=logfile),
'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile),
'| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email)
]
self.run_command(command)
reportfile = sorted([filename for filename in os.listdir(reports) if filename not in self.files_to_ignore and report_regexp.match(filename)], reverse=True)
if reportfile:
reportfile = reportfile[0]
changes = report_regexp.match(reportfile).group(1)

email_date = time.strftime('%Y-%m-%d %H:%M:%S')

command = [
'(echo "Changes since the last time: {changes}";'.format(changes=changes),
'uuencode "{logs}/{logfile}" "{logfile}";'.format(logs=logs, logfile=logfile),
'uuencode "{reports}/{reportfile}" "{reportfile}")'.format(reports=reports, reportfile=reportfile),
'| mail -s "Selenium Tests Report: {identifier} {email_date} Changes: {changes}" {email}'.format(identifier=identifier, email_date=email_date, changes=changes, email=self.email)
]
self.run_command(command)

def create_command(self, test_directory, *extra_arguments):
return ['tox', 'tests/' + test_directory, '--', '--url={}'.format(TARGETS[self.url])] + list(extra_arguments)
Expand Down
20 changes: 20 additions & 0 deletions codebender_testing/disqus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
# -*- coding: utf-8 -*-

from codebender_testing.config import get_path
import pytest
import disqusapi
import simplejson
import requests
import base64
import hashlib
import hmac
Expand All @@ -16,6 +18,7 @@
AUTHOR_URL = os.getenv('AUTHOR_URL', 'https://codebender.cc/user/codebender')
DISQUS_REQUESTS_PER_HOUR = 1000
DISQUS_WAIT = (DISQUS_REQUESTS_PER_HOUR / 60) / 60
DISQUS_MIN_REMANING_REQUESTS = 10
CHANGE_LOG = 'examples_compile_log.json'
DISQUS_COMMENTS = 'disqus_comments.json'
EXAMPLES_WITHOUT_LIBRARY_DB = 'examples_without_library.json'
Expand Down Expand Up @@ -58,6 +61,10 @@ def get_disqus_sso(self, user):
return "{0} {1} {2}".format(message, sig, timestamp)

def update_comment(self, sketch, results, current_date, log_entry, openFailFlag, total_sketches):
check_rate_limit = self.check_rate_limit()
if not check_rate_limit:
pytest.exit('Disqus API rate limit reached')

"""A comment is added to the library as soon as its first example is compiled.
`library`: The library in which belongs the currently compiled example.
`self.last_library`: The library in which belongs the previously compiled example.
Expand Down Expand Up @@ -250,3 +257,16 @@ def update_post(self, post_id, message):
print 'Error:', error

return comment_status

def check_rate_limit(self):
usage_check = False
try:
r = requests.get('https://disqus.com/api/3.0/applications/listUsage.json?api_secret=' + self.DISQUS_API_SECRET + '&access_token=' + self.DISQUS_ACCESS_TOKEN)
api_remaning_limit = r.headers['X-Ratelimit-Remaining']
if api_remaning_limit > DISQUS_MIN_REMANING_REQUESTS:
usage_check = True

except Exception as error:
print 'Error:', error

return usage_check
7 changes: 5 additions & 2 deletions codebender_testing/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ def open_all_libraries_and_examples(self, url, logfile):

print '\nVisiting:', len(urls_to_visit), 'URLs'
tic = time.time()
toc = tic
for url in urls_to_visit:
self.open(url)
self.get_element(By.CSS_SELECTOR, '#mycontainer')
Expand Down Expand Up @@ -578,6 +579,8 @@ def open_all_libraries_and_examples(self, url, logfile):

report_creator('fetch', log_entry, log_file)

print '\nTest duration:', int(toc - tic), 'sec'

def compile_sketch(self, url, boards, iframe=False, project_view=False):
"""Compiles the sketch located at `url`, or an iframe within the page
referred to by `url`. Raises an exception if it does not compile.
Expand Down Expand Up @@ -643,6 +646,7 @@ def comment_compile_libraries_examples(self, sketches, library_examples_dic={},

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

for url in urls_to_visit:
Expand Down Expand Up @@ -752,6 +756,7 @@ def comment_compile_libraries_examples(self, sketches, library_examples_dic={},
# Generate a report if requested.
if compile_type != 'target_library' and create_report and self.run_full_compile_tests:
report_creator(compile_type, log_entry, log_file)

print '\nTest duration:', int(toc - tic), 'sec'

def compile_all_sketches(self, url, selector, **kwargs):
Expand Down Expand Up @@ -906,9 +911,7 @@ def change_privacy(self, privacy):
privateRadioButton.click()

def change_name(self, name):
print name
nameField = self.get_element(By.CSS_SELECTOR,'#create-sketch-modal .modal-body [id="create-sketch-name"')
print nameField
nameField.clear()
nameField.send_keys(name)
nameField.send_keys(Keys.ENTER)
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pyyaml
pytest
selenium
simplejson
disqus-python
disqus-python
requests