blob: b732653a4d296324b53891dafb94d6d89f890b05 [file] [log] [blame]
Neil Williams01d53f42020-07-07 07:27:29 -07001import errno
2import unittest
3
4from test_sslsocket import ServerAcceptor
5
6import _import_local_thrift # noqa
7
8from thrift.transport.TSocket import TServerSocket
9from thrift.transport.TSocket import TSocket
10from thrift.transport.TTransport import TTransportException
11
12
13class TSocketTest(unittest.TestCase):
14 def test_isOpen_checks_for_readability(self):
15 # https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts
16 # https://docs.python.org/3/library/socket.html#socket.socket.settimeout
17 timeouts = [
18 None, # blocking mode
19 0, # non-blocking mode
20 1.0, # timeout mode
21 ]
22
23 for timeout in timeouts:
24 acc = ServerAcceptor(TServerSocket(port=0))
25 acc.start()
26
27 sock = TSocket(host="localhost", port=acc.port)
Michael Smithe3eb9af2022-06-08 17:23:27 -070028 self.assertFalse(sock.isOpen())
Neil Williams01d53f42020-07-07 07:27:29 -070029 sock.open()
30 sock.setTimeout(timeout)
31
32 # the socket shows as open immediately after connecting
33 self.assertTrue(sock.isOpen())
34
35 # and remains open during usage
36 sock.write(b"hello")
37 self.assertTrue(sock.isOpen())
38 while True:
39 try:
40 sock.read(5)
41 except TTransportException as exc:
42 if exc.inner.errno == errno.EAGAIN:
43 # try again when we're in non-blocking mode
44 continue
45 raise
46 break
47 self.assertTrue(sock.isOpen())
48
49 # once the server side closes, it no longer shows open
50 acc.client.close() # this also blocks until the other thread is done
51 acc.close()
52 self.assertFalse(sock.isOpen())
53
54 sock.close()
55
56
57if __name__ == "__main__":
58 unittest.main()