From 4c670815a3f6efdf53d4e2d2efe9c238c14dc4dc Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Sat, 7 Mar 2009 10:15:38 +0100 Subject: [PATCH] Add ability to run tests a limited time, and use this in the news checking. --- tests/__init__.py | 48 ++++++++++++++++++++++++++++++++++++++ tests/checker/test_news.py | 33 ++++++++++---------------- 2 files changed, 60 insertions(+), 21 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 5718b9eb..51ca2267 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -14,8 +14,14 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +from __future__ import with_statement # Required in 2.5 +import signal import subprocess import os +from nose import SkipTest +from contextlib import contextmanager +from linkcheck import LinkCheckerInterrupt +from linkcheck.decorators import update_func_meta class memoized (object): @@ -99,5 +105,47 @@ def has_proxy (): return False +@contextmanager +def limit_time (seconds): + """Raises LinkCheckerInterrupt if given number of seconds have passed.""" + def signal_handler(signum, frame): + raise LinkCheckerInterrupt("timed out") + old_handler = signal.getsignal(signal.SIGALRM) + signal.signal(signal.SIGALRM, signal_handler) + signal.alarm(seconds) + yield + signal.alarm(0) + if old_handler is not None: + signal.signal(signal.SIGALRM, old_handler) + + +def limit_time_skip (seconds): + """Skip test if the given number of seconds has passed.""" + def run_limited (func): + def new_func (*args, **kwargs): + try: + with limit_time(seconds): + return func(*args, **kwargs) + except LinkCheckerInterrupt: + raise SkipTest + new_func.func_name = func.func_name + return new_func + return run_limited + + +def limit_time_fail (seconds): + """Fail test if the given number of seconds has passed.""" + def run_limited (func): + def new_func (*args, **kwargs): + try: + with limit_time(seconds): + return func(*args, **kwargs) + except LinkCheckerInterrupt, msg: + assert False, msg + new_func.func_name = func.func_name + return new_func + return run_limited + + if __name__ == '__main__': print has_clamav(), has_network(), has_msgfmt(), has_posix(), has_proxy() diff --git a/tests/checker/test_news.py b/tests/checker/test_news.py index 18edeb71..cb1cc156 100644 --- a/tests/checker/test_news.py +++ b/tests/checker/test_news.py @@ -17,7 +17,7 @@ """ Test news checking. """ -from tests import has_network +from tests import has_network, limit_time_skip from nose import SkipTest from . import LinkCheckTest @@ -25,7 +25,8 @@ from . import LinkCheckTest NNTP_SERVER = "freenews.netfront.net" # info string returned by news server NNTP_INFO = u"200 news.netfront.net InterNetNews NNRP server INN 2.4.6 (20090304 snapshot) ready (posting ok)." - +# NNTP servers are dog slow, so don't waist a lot of time running those. +NNTP_TIMEOUT_SECS = 8 class TestNews (LinkCheckTest): """ @@ -37,10 +38,7 @@ class TestNews (LinkCheckTest): raise SkipTest() self.direct(url, resultlines) - def test_news (self): - """ - Test news: link. - """ + def test_news_without_host (self): # news testing url = u"news:comp.os.linux.misc" resultlines = [ @@ -62,10 +60,7 @@ class TestNews (LinkCheckTest): ] self.newstest(url, resultlines) - def test_snews (self): - """ - Test snews: link. - """ + def test_snews_with_group (self): url = u"snews:de.comp.os.unix.linux.misc" nurl = self.norm(url) resultlines = [ @@ -78,7 +73,7 @@ class TestNews (LinkCheckTest): ] self.newstest(url, resultlines) - def test_illegal (self): + def test_illegal_syntax (self): # illegal syntax url = u"news:§$%&/´`(§%" qurl = self.norm(url) @@ -92,10 +87,8 @@ class TestNews (LinkCheckTest): ] self.newstest(url, resultlines) - def test_nntp (self): - """ - Nttp scheme with host. - """ + @limit_time_skip(NNTP_TIMEOUT_SECS) + def test_nntp_with_host (self): url = u"nntp://%s/comp.lang.python" % NNTP_SERVER resultlines = [ u"url %s" % url, @@ -107,10 +100,8 @@ class TestNews (LinkCheckTest): ] self.newstest(url, resultlines) + @limit_time_skip(NNTP_TIMEOUT_SECS) def test_article_span (self): - """ - Article span. - """ url = u"nntp://%s/comp.lang.python/1-5" % NNTP_SERVER resultlines = [ u"url %s" % url, @@ -121,6 +112,8 @@ class TestNews (LinkCheckTest): u"valid", ] self.newstest(url, resultlines) + + def test_article_span_no_host (self): url = u"news:comp.lang.python/1-5" resultlines = [ u"url %s" % url, @@ -131,10 +124,8 @@ class TestNews (LinkCheckTest): ] self.newstest(url, resultlines) + @limit_time_fail(NNTP_TIMEOUT_SECS) def test_host_no_group (self): - """ - Host but no group. - """ url = u"nntp://%s/" % NNTP_SERVER resultlines = [ u"url %s" % url,