bpd tests: close only existing sockets

Close sockets in `finally`-clauses only after they have actually been created.
This commit is contained in:
Zsin Skri 2019-07-17 13:38:57 +02:00
parent fb07a5112a
commit 871f79c8f2

View file

@ -324,19 +324,25 @@ class BPDTestHelper(unittest.TestCase, TestHelper):
)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
try:
sock.connect((host, port))
sock2 = None
if second_client:
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock2.connect((host, port))
yield MPCClient(sock, do_hello), MPCClient(sock2, do_hello)
else:
yield MPCClient(sock, do_hello)
if second_client:
sock2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock2.connect((host, port))
yield (
MPCClient(sock, do_hello),
MPCClient(sock2, do_hello),
)
finally:
sock2.close()
else:
yield MPCClient(sock, do_hello)
finally:
sock.close()
finally:
sock.close()
if sock2:
sock2.close()
server.terminate()
server.join(timeout=0.2)