Kaydet (Commit) de609186 authored tarafından Antoine Pitrou's avatar Antoine Pitrou

Wrap all test_nntplib methods accessing a remote server in a transient_internet()

exception catcher.  Wrapping the initial connection routine is not sufficient
as network timeouts can then occur as part of NNTP commands.
üst 6a0b5c41
...@@ -2,7 +2,9 @@ import io ...@@ -2,7 +2,9 @@ import io
import datetime import datetime
import textwrap import textwrap
import unittest import unittest
import functools
import contextlib import contextlib
import collections
from test import support from test import support
from nntplib import NNTP, GroupInfo, _have_ssl from nntplib import NNTP, GroupInfo, _have_ssl
import nntplib import nntplib
...@@ -228,6 +230,28 @@ class NetworkedNNTPTestsMixin: ...@@ -228,6 +230,28 @@ class NetworkedNNTPTestsMixin:
self.server.quit() self.server.quit()
cls.server = None cls.server = None
@classmethod
def wrap_methods(cls):
# Wrap all methods in a transient_internet() exception catcher
# XXX put a generic version in test.support?
def wrap_meth(meth):
@functools.wraps(meth)
def wrapped(self):
with support.transient_internet(self.NNTP_HOST):
meth(self)
return wrapped
for name in dir(cls):
if not name.startswith('test_'):
continue
meth = getattr(cls, name)
if not isinstance(meth, collections.Callable):
continue
# Need to use a closure so that meth remains bound to its current
# value
setattr(cls, name, wrap_meth(meth))
NetworkedNNTPTestsMixin.wrap_methods()
class NetworkedNNTPTests(NetworkedNNTPTestsMixin, unittest.TestCase): class NetworkedNNTPTests(NetworkedNNTPTestsMixin, unittest.TestCase):
# This server supports STARTTLS (gmane doesn't) # This server supports STARTTLS (gmane doesn't)
...@@ -235,11 +259,13 @@ class NetworkedNNTPTests(NetworkedNNTPTestsMixin, unittest.TestCase): ...@@ -235,11 +259,13 @@ class NetworkedNNTPTests(NetworkedNNTPTestsMixin, unittest.TestCase):
GROUP_NAME = 'fr.comp.lang.python' GROUP_NAME = 'fr.comp.lang.python'
GROUP_PAT = 'fr.comp.lang.*' GROUP_PAT = 'fr.comp.lang.*'
NNTP_CLASS = NNTP
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
support.requires("network") support.requires("network")
with support.transient_internet(cls.NNTP_HOST): with support.transient_internet(cls.NNTP_HOST):
cls.server = NNTP(cls.NNTP_HOST, timeout=TIMEOUT, usenetrc=False) cls.server = cls.NNTP_CLASS(cls.NNTP_HOST, timeout=TIMEOUT, usenetrc=False)
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
...@@ -248,7 +274,7 @@ class NetworkedNNTPTests(NetworkedNNTPTestsMixin, unittest.TestCase): ...@@ -248,7 +274,7 @@ class NetworkedNNTPTests(NetworkedNNTPTestsMixin, unittest.TestCase):
if _have_ssl: if _have_ssl:
class NetworkedNNTP_SSLTests(NetworkedNNTPTestsMixin, unittest.TestCase): class NetworkedNNTP_SSLTests(NetworkedNNTPTests):
# Technical limits for this public NNTP server (see http://www.aioe.org): # Technical limits for this public NNTP server (see http://www.aioe.org):
# "Only two concurrent connections per IP address are allowed and # "Only two concurrent connections per IP address are allowed and
...@@ -258,17 +284,7 @@ if _have_ssl: ...@@ -258,17 +284,7 @@ if _have_ssl:
GROUP_NAME = 'comp.lang.python' GROUP_NAME = 'comp.lang.python'
GROUP_PAT = 'comp.lang.*' GROUP_PAT = 'comp.lang.*'
@classmethod NNTP_CLASS = nntplib.NNTP_SSL
def setUpClass(cls):
support.requires("network")
with support.transient_internet(cls.NNTP_HOST):
cls.server = nntplib.NNTP_SSL(cls.NNTP_HOST, timeout=TIMEOUT,
usenetrc=False)
@classmethod
def tearDownClass(cls):
if cls.server is not None:
cls.server.quit()
# Disabled as it produces too much data # Disabled as it produces too much data
test_list = None test_list = None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment