Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de60918

Browse files
committed
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.
1 parent 6a0b5c4 commit de60918

1 file changed

Lines changed: 29 additions & 13 deletions

File tree

Lib/test/test_nntplib.py

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import datetime
33
import textwrap
44
import unittest
5+
import functools
56
import contextlib
7+
import collections
68
from test import support
79
from nntplib import NNTP, GroupInfo, _have_ssl
810
import nntplib
@@ -228,18 +230,42 @@ def test_zzquit(self):
228230
self.server.quit()
229231
cls.server = None
230232

233+
@classmethod
234+
def wrap_methods(cls):
235+
# Wrap all methods in a transient_internet() exception catcher
236+
# XXX put a generic version in test.support?
237+
def wrap_meth(meth):
238+
@functools.wraps(meth)
239+
def wrapped(self):
240+
with support.transient_internet(self.NNTP_HOST):
241+
meth(self)
242+
return wrapped
243+
for name in dir(cls):
244+
if not name.startswith('test_'):
245+
continue
246+
meth = getattr(cls, name)
247+
if not isinstance(meth, collections.Callable):
248+
continue
249+
# Need to use a closure so that meth remains bound to its current
250+
# value
251+
setattr(cls, name, wrap_meth(meth))
252+
253+
NetworkedNNTPTestsMixin.wrap_methods()
254+
231255

232256
class NetworkedNNTPTests(NetworkedNNTPTestsMixin, unittest.TestCase):
233257
# This server supports STARTTLS (gmane doesn't)
234258
NNTP_HOST = 'news.trigofacile.com'
235259
GROUP_NAME = 'fr.comp.lang.python'
236260
GROUP_PAT = 'fr.comp.lang.*'
237261

262+
NNTP_CLASS = NNTP
263+
238264
@classmethod
239265
def setUpClass(cls):
240266
support.requires("network")
241267
with support.transient_internet(cls.NNTP_HOST):
242-
cls.server = NNTP(cls.NNTP_HOST, timeout=TIMEOUT, usenetrc=False)
268+
cls.server = cls.NNTP_CLASS(cls.NNTP_HOST, timeout=TIMEOUT, usenetrc=False)
243269

244270
@classmethod
245271
def tearDownClass(cls):
@@ -248,7 +274,7 @@ def tearDownClass(cls):
248274

249275

250276
if _have_ssl:
251-
class NetworkedNNTP_SSLTests(NetworkedNNTPTestsMixin, unittest.TestCase):
277+
class NetworkedNNTP_SSLTests(NetworkedNNTPTests):
252278

253279
# Technical limits for this public NNTP server (see http://www.aioe.org):
254280
# "Only two concurrent connections per IP address are allowed and
@@ -258,17 +284,7 @@ class NetworkedNNTP_SSLTests(NetworkedNNTPTestsMixin, unittest.TestCase):
258284
GROUP_NAME = 'comp.lang.python'
259285
GROUP_PAT = 'comp.lang.*'
260286

261-
@classmethod
262-
def setUpClass(cls):
263-
support.requires("network")
264-
with support.transient_internet(cls.NNTP_HOST):
265-
cls.server = nntplib.NNTP_SSL(cls.NNTP_HOST, timeout=TIMEOUT,
266-
usenetrc=False)
267-
268-
@classmethod
269-
def tearDownClass(cls):
270-
if cls.server is not None:
271-
cls.server.quit()
287+
NNTP_CLASS = nntplib.NNTP_SSL
272288

273289
# Disabled as it produces too much data
274290
test_list = None

0 commit comments

Comments
 (0)