summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_socket_ssl.py
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2006-01-24 22:44:54 (GMT)
committerTim Peters <tim.peters@gmail.com>2006-01-24 22:44:54 (GMT)
commit2a4712dc80bcc873bea2238fcc855c9b92b82095 (patch)
tree8813592aeac54fb513f3e3d0ced3221d972c8a6d /Lib/test/test_socket_ssl.py
parent92037a15a975092d81b6af5933aba985e731cdb7 (diff)
downloadcpython-2a4712dc80bcc873bea2238fcc855c9b92b82095.zip
cpython-2a4712dc80bcc873bea2238fcc855c9b92b82095.tar.gz
cpython-2a4712dc80bcc873bea2238fcc855c9b92b82095.tar.bz2
test_rude_shutdown(): Rewrote to use proper thread
synchronization and termination.
Diffstat (limited to 'Lib/test/test_socket_ssl.py')
-rw-r--r--Lib/test/test_socket_ssl.py30
1 files changed, 19 insertions, 11 deletions
diff --git a/Lib/test/test_socket_ssl.py b/Lib/test/test_socket_ssl.py
index bcdb705..5db5ab1 100644
--- a/Lib/test/test_socket_ssl.py
+++ b/Lib/test/test_socket_ssl.py
@@ -2,7 +2,6 @@
from test import test_support
import socket
-import time
# Optionally test SSL support. This requires the 'network' resource as given
# on the regrtest command line.
@@ -29,34 +28,43 @@ def test_basic():
def test_rude_shutdown():
try:
- import thread
+ import threading
except ImportError:
return
- # some random port to connect to
+ # Some random port to connect to.
PORT = 9934
+
+ listener_gone = threading.Event()
+
+ # `listener` runs in a thread. It opens a socket listening on PORT, and
+ # sits in an accept() until the main thread connects. Then it rudely
+ # closes the socket, and sets Event `listener_gone` to let the main thread
+ # know the socket is gone.
def listener():
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s = socket.socket()
s.bind(('', PORT))
s.listen(5)
s.accept()
- del s
- thread.exit()
+ s = None # reclaim the socket object, which also closes it
+ listener_gone.set()
def connector():
- s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ s = socket.socket()
s.connect(('localhost', PORT))
+ listener_gone.wait()
try:
ssl_sock = socket.ssl(s)
except socket.sslerror:
pass
else:
- raise test_support.TestFailed, \
- 'connecting to closed SSL socket failed'
+ raise test_support.TestFailed(
+ 'connecting to closed SSL socket should have failed')
- thread.start_new_thread(listener, ())
- time.sleep(1)
+ t = threading.Thread(target=listener)
+ t.start()
connector()
+ t.join()
def test_main():
if not hasattr(socket, "ssl"):