blob: 727aceb0297b49eef9b68874864361f3b5b05b70 [file] [log] [blame]
Jens Geyer72a714e2025-08-26 22:12:07 +02001#
2# Licensed to the Apache Software Foundation (ASF) under one
3# or more contributor license agreements. See the NOTICE file
4# distributed with this work for additional information
5# regarding copyright ownership. The ASF licenses this file
6# to you under the Apache License, Version 2.0 (the
7# "License"); you may not use this file except in compliance
8# with the License. You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied. See the License for the
16# specific language governing permissions and limitations
17# under the License.
18#
19
Neil Williams01d53f42020-07-07 07:27:29 -070020import errno
21import unittest
22
23from test_sslsocket import ServerAcceptor
24
25import _import_local_thrift # noqa
26
27from thrift.transport.TSocket import TServerSocket
28from thrift.transport.TSocket import TSocket
29from thrift.transport.TTransport import TTransportException
30
31
32class TSocketTest(unittest.TestCase):
bwangelme0ed4a1d2024-04-15 12:17:40 +080033 def test_socket_readtimeout_exception(self):
34 acc = ServerAcceptor(TServerSocket(port=0))
35 acc.start()
36
37 sock = TSocket(host="localhost", port=acc.port)
38 sock.open()
39 sock.setTimeout(1)
40 sock.write(b"sleep")
41
42 with self.assertRaises(TTransportException) as ctx:
43 sock.read(5)
44 exc = ctx.exception
45 self.assertEqual(exc.message, "read timeout")
46
47 acc.client.close() # this also blocks until the other thread is done
48 acc.close()
49 sock.close()
50
Neil Williams01d53f42020-07-07 07:27:29 -070051 def test_isOpen_checks_for_readability(self):
52 # https://docs.python.org/3/library/socket.html#notes-on-socket-timeouts
53 # https://docs.python.org/3/library/socket.html#socket.socket.settimeout
54 timeouts = [
55 None, # blocking mode
56 0, # non-blocking mode
57 1.0, # timeout mode
58 ]
59
60 for timeout in timeouts:
61 acc = ServerAcceptor(TServerSocket(port=0))
62 acc.start()
63
64 sock = TSocket(host="localhost", port=acc.port)
Michael Smithe3eb9af2022-06-08 17:23:27 -070065 self.assertFalse(sock.isOpen())
Neil Williams01d53f42020-07-07 07:27:29 -070066 sock.open()
67 sock.setTimeout(timeout)
68
69 # the socket shows as open immediately after connecting
70 self.assertTrue(sock.isOpen())
71
72 # and remains open during usage
73 sock.write(b"hello")
74 self.assertTrue(sock.isOpen())
75 while True:
76 try:
77 sock.read(5)
78 except TTransportException as exc:
79 if exc.inner.errno == errno.EAGAIN:
80 # try again when we're in non-blocking mode
81 continue
82 raise
83 break
84 self.assertTrue(sock.isOpen())
85
86 # once the server side closes, it no longer shows open
87 acc.client.close() # this also blocks until the other thread is done
88 acc.close()
Neil Williams01d53f42020-07-07 07:27:29 -070089
Csaba Ringhoferefe5e022024-08-23 14:08:35 +020090 self.assertIsNotNone(sock.handle)
91 self.assertFalse(sock.isOpen())
92 # after isOpen() returned False the socket should be closed (THRIFT-5813)
93 self.assertIsNone(sock.handle)
Neil Williams01d53f42020-07-07 07:27:29 -070094
95
96if __name__ == "__main__":
97 unittest.main()