Skip to content
This repository has been archived by the owner on Dec 10, 2018. It is now read-only.

Reset self.sock if connection fails #298

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,24 @@ def test_client_socket_set_timeout():
conn.close()
client_socket.close()
server_socket.close()


def test_client_socket_not_open_after_failed_open():
client_socket = TSocket(host="localhost", port=12345)
assert not client_socket.is_open()

with pytest.raises(TTransportException):
client_socket.open()

assert not client_socket.is_open()


def test_client_socket_not_open_after_close():
client_socket = TSocket(host="localhost", port=12345)
assert not client_socket.is_open()

with pytest.raises(TTransportException):
client_socket.open()
client_socket.close()

assert not client_socket.is_open()
3 changes: 2 additions & 1 deletion thriftpy/transport/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def open(self):
self.sock.settimeout(self.socket_timeout)

except (socket.error, OSError):
self.sock = None
raise TTransportException(
type=TTransportException.NOT_OPEN,
message="Could not connect to %s" % str(addr))
Expand Down Expand Up @@ -140,7 +141,7 @@ def close(self):
self.sock.close()
self.sock = None
except (socket.error, OSError):
pass
self.sock = None


class TServerSocket(object):
Expand Down