Jens Geyer | 72a714e | 2025-08-26 22:12:07 +0200 | [diff] [blame^] | 1 | # |
| 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 Williams | 01d53f4 | 2020-07-07 07:27:29 -0700 | [diff] [blame] | 20 | import errno |
| 21 | import unittest |
| 22 | |
| 23 | from test_sslsocket import ServerAcceptor |
| 24 | |
| 25 | import _import_local_thrift # noqa |
| 26 | |
| 27 | from thrift.transport.TSocket import TServerSocket |
| 28 | from thrift.transport.TSocket import TSocket |
| 29 | from thrift.transport.TTransport import TTransportException |
| 30 | |
| 31 | |
| 32 | class TSocketTest(unittest.TestCase): |
bwangelme | 0ed4a1d | 2024-04-15 12:17:40 +0800 | [diff] [blame] | 33 | 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 Williams | 01d53f4 | 2020-07-07 07:27:29 -0700 | [diff] [blame] | 51 | 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 Smith | e3eb9af | 2022-06-08 17:23:27 -0700 | [diff] [blame] | 65 | self.assertFalse(sock.isOpen()) |
Neil Williams | 01d53f4 | 2020-07-07 07:27:29 -0700 | [diff] [blame] | 66 | 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 Williams | 01d53f4 | 2020-07-07 07:27:29 -0700 | [diff] [blame] | 89 | |
Csaba Ringhofer | efe5e02 | 2024-08-23 14:08:35 +0200 | [diff] [blame] | 90 | 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 Williams | 01d53f4 | 2020-07-07 07:27:29 -0700 | [diff] [blame] | 94 | |
| 95 | |
| 96 | if __name__ == "__main__": |
| 97 | unittest.main() |