bpo-34246: Make sure test_smtplib always cleans resources when finish… · python/cpython@5b7a2cb (original) (raw)
`@@ -20,6 +20,7 @@
`
20
20
`import unittest
`
21
21
`from test import support, mock_socket
`
22
22
`from test.support import HOST, HOSTv4, HOSTv6
`
``
23
`+
from test.support import threading_setup, threading_cleanup, join_thread
`
23
24
`from unittest.mock import Mock
`
24
25
``
25
26
``
`@@ -193,6 +194,7 @@ class DebuggingServerTests(unittest.TestCase):
`
193
194
`maxDiff = None
`
194
195
``
195
196
`def setUp(self):
`
``
197
`+
self.thread_key = threading_setup()
`
196
198
`self.real_getfqdn = socket.getfqdn
`
197
199
`socket.getfqdn = mock_socket.getfqdn
`
198
200
`# temporarily replace sys.stdout to capture DebuggingServer output
`
`@@ -224,12 +226,15 @@ def tearDown(self):
`
224
226
`self.client_evt.set()
`
225
227
`# wait for the server thread to terminate
`
226
228
`self.serv_evt.wait()
`
227
``
`-
self.thread.join()
`
``
229
`+
join_thread(self.thread)
`
228
230
`# restore sys.stdout
`
229
231
`sys.stdout = self.old_stdout
`
230
232
`# restore DEBUGSTREAM
`
231
233
`smtpd.DEBUGSTREAM.close()
`
232
234
`smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM
`
``
235
`+
del self.thread
`
``
236
`+
self.doCleanups()
`
``
237
`+
threading_cleanup(*self.thread_key)
`
233
238
``
234
239
`def get_output_without_xpeer(self):
`
235
240
`test_output = self.output.getvalue()
`
`@@ -247,6 +252,7 @@ def testSourceAddress(self):
`
247
252
`try:
`
248
253
`smtp = smtplib.SMTP(self.host, self.port, local_hostname='localhost',
`
249
254
`timeout=3, source_address=(self.host, src_port))
`
``
255
`+
self.addCleanup(smtp.close)
`
250
256
`self.assertEqual(smtp.source_address, (self.host, src_port))
`
251
257
`self.assertEqual(smtp.local_hostname, 'localhost')
`
252
258
`smtp.quit()
`
`@@ -257,33 +263,38 @@ def testSourceAddress(self):
`
257
263
``
258
264
`def testNOOP(self):
`
259
265
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
266
`+
self.addCleanup(smtp.close)
`
260
267
`expected = (250, b'OK')
`
261
268
`self.assertEqual(smtp.noop(), expected)
`
262
269
`smtp.quit()
`
263
270
``
264
271
`def testRSET(self):
`
265
272
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
273
`+
self.addCleanup(smtp.close)
`
266
274
`expected = (250, b'OK')
`
267
275
`self.assertEqual(smtp.rset(), expected)
`
268
276
`smtp.quit()
`
269
277
``
270
278
`def testELHO(self):
`
271
279
`# EHLO isn't implemented in DebuggingServer
`
272
280
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
281
`+
self.addCleanup(smtp.close)
`
273
282
`expected = (250, b'\nSIZE 33554432\nHELP')
`
274
283
`self.assertEqual(smtp.ehlo(), expected)
`
275
284
`smtp.quit()
`
276
285
``
277
286
`def testEXPNNotImplemented(self):
`
278
287
`# EXPN isn't implemented in DebuggingServer
`
279
288
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
289
`+
self.addCleanup(smtp.close)
`
280
290
`expected = (502, b'EXPN not implemented')
`
281
291
`smtp.putcmd('EXPN')
`
282
292
`self.assertEqual(smtp.getreply(), expected)
`
283
293
`smtp.quit()
`
284
294
``
285
295
`def testVRFY(self):
`
286
296
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
297
`+
self.addCleanup(smtp.close)
`
287
298
`expected = (252, b'Cannot VRFY user, but will accept message ' + \
`
288
299
`b'and attempt delivery')
`
289
300
`self.assertEqual(smtp.vrfy('nobody@nowhere.com'), expected)
`
`@@ -294,13 +305,15 @@ def testSecondHELO(self):
`
294
305
`# check that a second HELO returns a message that it's a duplicate
`
295
306
`# (this behavior is specific to smtpd.SMTPChannel)
`
296
307
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
308
`+
self.addCleanup(smtp.close)
`
297
309
`smtp.helo()
`
298
310
`expected = (503, b'Duplicate HELO/EHLO')
`
299
311
`self.assertEqual(smtp.helo(), expected)
`
300
312
`smtp.quit()
`
301
313
``
302
314
`def testHELP(self):
`
303
315
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
316
`+
self.addCleanup(smtp.close)
`
304
317
`self.assertEqual(smtp.help(), b'Supported commands: EHLO HELO MAIL ' + \
`
305
318
`b'RCPT DATA RSET NOOP QUIT VRFY')
`
306
319
`smtp.quit()
`
`@@ -309,6 +322,7 @@ def testSend(self):
`
309
322
`# connect and send mail
`
310
323
`m = 'A test message'
`
311
324
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
325
`+
self.addCleanup(smtp.close)
`
312
326
`smtp.sendmail('John', 'Sally', m)
`
313
327
`# XXX(nnorwitz): this test is flaky and dies with a bad file descriptor
`
314
328
`# in asyncore. This sleep might help, but should really be fixed
`
`@@ -325,6 +339,7 @@ def testSend(self):
`
325
339
`def testSendBinary(self):
`
326
340
`m = b'A test message'
`
327
341
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
342
`+
self.addCleanup(smtp.close)
`
328
343
`smtp.sendmail('John', 'Sally', m)
`
329
344
`# XXX (see comment in testSend)
`
330
345
`time.sleep(0.01)
`
`@@ -340,6 +355,7 @@ def testSendNeedingDotQuote(self):
`
340
355
`# Issue 12283
`
341
356
`m = '.A test\n.mes.sage.'
`
342
357
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
358
`+
self.addCleanup(smtp.close)
`
343
359
`smtp.sendmail('John', 'Sally', m)
`
344
360
`# XXX (see comment in testSend)
`
345
361
`time.sleep(0.01)
`
`@@ -354,6 +370,7 @@ def testSendNeedingDotQuote(self):
`
354
370
`def testSendNullSender(self):
`
355
371
`m = 'A test message'
`
356
372
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
373
`+
self.addCleanup(smtp.close)
`
357
374
`smtp.sendmail('<>', 'Sally', m)
`
358
375
`# XXX (see comment in testSend)
`
359
376
`time.sleep(0.01)
`
`@@ -371,6 +388,7 @@ def testSendNullSender(self):
`
371
388
`def testSendMessage(self):
`
372
389
`m = email.mime.text.MIMEText('A test message')
`
373
390
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
391
`+
self.addCleanup(smtp.close)
`
374
392
`smtp.send_message(m, from_addr='John', to_addrs='Sally')
`
375
393
`# XXX (see comment in testSend)
`
376
394
`time.sleep(0.01)
`
`@@ -395,6 +413,7 @@ def testSendMessageWithAddresses(self):
`
395
413
`m['CC'] = 'Sally, Fred'
`
396
414
`m['Bcc'] = 'John Root <root@localhost>, "Dinsdale" warped@silly.walks.com'
`
397
415
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
416
`+
self.addCleanup(smtp.close)
`
398
417
`smtp.send_message(m)
`
399
418
`# XXX (see comment in testSend)
`
400
419
`time.sleep(0.01)
`
`@@ -428,6 +447,7 @@ def testSendMessageWithSomeAddresses(self):
`
428
447
`m['From'] = 'foo@bar.com'
`
429
448
`m['To'] = 'John, Dinsdale'
`
430
449
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
450
`+
self.addCleanup(smtp.close)
`
431
451
`smtp.send_message(m)
`
432
452
`# XXX (see comment in testSend)
`
433
453
`time.sleep(0.01)
`
`@@ -455,6 +475,7 @@ def testSendMessageWithSpecifiedAddresses(self):
`
455
475
`m['From'] = 'foo@bar.com'
`
456
476
`m['To'] = 'John, Dinsdale'
`
457
477
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
478
`+
self.addCleanup(smtp.close)
`
458
479
`smtp.send_message(m, from_addr='joe@example.com', to_addrs='foo@example.net')
`
459
480
`# XXX (see comment in testSend)
`
460
481
`time.sleep(0.01)
`
`@@ -485,6 +506,7 @@ def testSendMessageWithMultipleFrom(self):
`
485
506
`m['Sender'] = 'the_rescuers@Rescue-Aid-Society.com'
`
486
507
`m['To'] = 'John, Dinsdale'
`
487
508
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
509
`+
self.addCleanup(smtp.close)
`
488
510
`smtp.send_message(m)
`
489
511
`# XXX (see comment in testSend)
`
490
512
`time.sleep(0.01)
`
`@@ -517,6 +539,7 @@ def testSendMessageResent(self):
`
517
539
`m['Resent-To'] = 'Martha my_mom@great.cooker.com, Jeff'
`
518
540
`m['Resent-Bcc'] = 'doe@losthope.net'
`
519
541
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
542
`+
self.addCleanup(smtp.close)
`
520
543
`smtp.send_message(m)
`
521
544
`# XXX (see comment in testSend)
`
522
545
`time.sleep(0.01)
`
`@@ -555,6 +578,7 @@ def testSendMessageMultipleResentRaises(self):
`
555
578
`m['Resent-To'] = 'holy@grail.net'
`
556
579
`m['Resent-From'] = 'Martha my_mom@great.cooker.com, Jeff'
`
557
580
`smtp = smtplib.SMTP(HOST, self.port, local_hostname='localhost', timeout=3)
`
``
581
`+
self.addCleanup(smtp.close)
`
558
582
`with self.assertRaises(ValueError):
`
559
583
`smtp.send_message(m)
`
560
584
`smtp.close()
`
`@@ -630,6 +654,7 @@ class TooLongLineTests(unittest.TestCase):
`
630
654
`respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
`
631
655
``
632
656
`def setUp(self):
`
``
657
`+
self.thread_key = threading_setup()
`
633
658
`self.old_stdout = sys.stdout
`
634
659
`self.output = io.StringIO()
`
635
660
`sys.stdout = self.output
`
`@@ -639,15 +664,18 @@ def setUp(self):
`
639
664
`self.sock.settimeout(15)
`
640
665
`self.port = support.bind_port(self.sock)
`
641
666
`servargs = (self.evt, self.respdata, self.sock)
`
642
``
`-
thread = threading.Thread(target=server, args=servargs)
`
643
``
`-
thread.start()
`
644
``
`-
self.addCleanup(thread.join)
`
``
667
`+
self.thread = threading.Thread(target=server, args=servargs)
`
``
668
`+
self.thread.start()
`
645
669
`self.evt.wait()
`
646
670
`self.evt.clear()
`
647
671
``
648
672
`def tearDown(self):
`
649
673
`self.evt.wait()
`
650
674
`sys.stdout = self.old_stdout
`
``
675
`+
join_thread(self.thread)
`
``
676
`+
del self.thread
`
``
677
`+
self.doCleanups()
`
``
678
`+
threading_cleanup(*self.thread_key)
`
651
679
``
652
680
`def testLineTooLong(self):
`
653
681
`self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
`
`@@ -877,6 +905,7 @@ def handle_error(self):
`
877
905
`class SMTPSimTests(unittest.TestCase):
`
878
906
``
879
907
`def setUp(self):
`
``
908
`+
self.thread_key = threading_setup()
`
880
909
`self.real_getfqdn = socket.getfqdn
`
881
910
`socket.getfqdn = mock_socket.getfqdn
`
882
911
`self.serv_evt = threading.Event()
`
`@@ -899,7 +928,10 @@ def tearDown(self):
`
899
928
`self.client_evt.set()
`
900
929
`# wait for the server thread to terminate
`
901
930
`self.serv_evt.wait()
`
902
``
`-
self.thread.join()
`
``
931
`+
join_thread(self.thread)
`
``
932
`+
del self.thread
`
``
933
`+
self.doCleanups()
`
``
934
`+
threading_cleanup(*self.thread_key)
`
903
935
``
904
936
`def testBasic(self):
`
905
937
`# smoke test
`
`@@ -1162,6 +1194,7 @@ class SMTPUTF8SimTests(unittest.TestCase):
`
1162
1194
`maxDiff = None
`
1163
1195
``
1164
1196
`def setUp(self):
`
``
1197
`+
self.thread_key = threading_setup()
`
1165
1198
`self.real_getfqdn = socket.getfqdn
`
1166
1199
`socket.getfqdn = mock_socket.getfqdn
`
1167
1200
`self.serv_evt = threading.Event()
`
`@@ -1186,7 +1219,10 @@ def tearDown(self):
`
1186
1219
`self.client_evt.set()
`
1187
1220
`# wait for the server thread to terminate
`
1188
1221
`self.serv_evt.wait()
`
1189
``
`-
self.thread.join()
`
``
1222
`+
join_thread(self.thread)
`
``
1223
`+
del self.thread
`
``
1224
`+
self.doCleanups()
`
``
1225
`+
threading_cleanup(*self.thread_key)
`
1190
1226
``
1191
1227
`def test_test_server_supports_extensions(self):
`
1192
1228
`smtp = smtplib.SMTP(
`
`@@ -1283,6 +1319,7 @@ class SimSMTPAUTHInitialResponseServer(SimSMTPServer):
`
1283
1319
``
1284
1320
`class SMTPAUTHInitialResponseSimTests(unittest.TestCase):
`
1285
1321
`def setUp(self):
`
``
1322
`+
self.thread_key = threading_setup()
`
1286
1323
`self.real_getfqdn = socket.getfqdn
`
1287
1324
`socket.getfqdn = mock_socket.getfqdn
`
1288
1325
`self.serv_evt = threading.Event()
`
`@@ -1306,7 +1343,10 @@ def tearDown(self):
`
1306
1343
`self.client_evt.set()
`
1307
1344
`# wait for the server thread to terminate
`
1308
1345
`self.serv_evt.wait()
`
1309
``
`-
self.thread.join()
`
``
1346
`+
join_thread(self.thread)
`
``
1347
`+
del self.thread
`
``
1348
`+
self.doCleanups()
`
``
1349
`+
threading_cleanup(*self.thread_key)
`
1310
1350
``
1311
1351
`def testAUTH_PLAIN_initial_response_login(self):
`
1312
1352
`self.serv.add_feature('AUTH PLAIN')
`