Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [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 | |
| 20 | #define BOOST_TEST_MODULE TSocketInterruptTest |
| 21 | #include <boost/test/auto_unit_test.hpp> |
| 22 | |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 23 | #include <boost/chrono/duration.hpp> |
| 24 | #include <boost/date_time/posix_time/posix_time_duration.hpp> |
| 25 | #include <boost/thread/thread.hpp> |
| 26 | #include <thrift/transport/TSocket.h> |
| 27 | #include <thrift/transport/TServerSocket.h> |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 28 | #include <memory> |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 29 | |
| 30 | using apache::thrift::transport::TServerSocket; |
| 31 | using apache::thrift::transport::TSocket; |
| 32 | using apache::thrift::transport::TTransport; |
| 33 | using apache::thrift::transport::TTransportException; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 34 | using namespace apache::thrift; |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 35 | |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(TSocketInterruptTest) |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 37 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 38 | void readerWorker(std::shared_ptr<TTransport> tt, uint32_t expectedResult) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 39 | uint8_t buf[4]; |
| 40 | BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4)); |
| 41 | } |
| 42 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 43 | void readerWorkerMustThrow(std::shared_ptr<TTransport> tt) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 44 | try { |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 45 | uint8_t buf[4]; |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 46 | tt->read(buf, 4); |
| 47 | BOOST_ERROR("should not have gotten here"); |
| 48 | } catch (const TTransportException& tx) { |
| 49 | BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType()); |
| 50 | } |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 53 | BOOST_AUTO_TEST_CASE(test_interruptable_child_read) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 54 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 55 | sock1.listen(); |
Mario Emmenlauer | ed0bad3 | 2020-04-24 08:50:43 +0200 | [diff] [blame^] | 56 | BOOST_CHECK(sock1.isOpen()); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 57 | int port = sock1.getPort(); |
| 58 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 59 | clientSock.open(); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 60 | std::shared_ptr<TTransport> accepted = sock1.accept(); |
| 61 | boost::thread readThread(std::bind(readerWorkerMustThrow, accepted)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 62 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 63 | // readThread is practically guaranteed to be blocking now |
| 64 | sock1.interruptChildren(); |
| 65 | BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 66 | "server socket interruptChildren did not interrupt child read"); |
| 67 | clientSock.close(); |
| 68 | accepted->close(); |
| 69 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 72 | BOOST_AUTO_TEST_CASE(test_non_interruptable_child_read) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 73 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 74 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 75 | sock1.listen(); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 76 | int port = sock1.getPort(); |
| 77 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 78 | clientSock.open(); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 79 | std::shared_ptr<TTransport> accepted = sock1.accept(); |
| 80 | boost::thread readThread(std::bind(readerWorker, accepted, 0)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 81 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 82 | // readThread is practically guaranteed to be blocking here |
| 83 | sock1.interruptChildren(); |
| 84 | BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 85 | "server socket interruptChildren interrupted child read"); |
| 86 | |
| 87 | // only way to proceed is to have the client disconnect |
| 88 | clientSock.close(); |
| 89 | readThread.join(); |
| 90 | accepted->close(); |
| 91 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 92 | } |
| 93 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(test_cannot_change_after_listen) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 95 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 96 | sock1.listen(); |
| 97 | BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error); |
| 98 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 99 | } |
| 100 | |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 101 | void peekerWorker(std::shared_ptr<TTransport> tt, bool expectedResult) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(expectedResult, tt->peek()); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(test_interruptable_child_peek) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 106 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 107 | sock1.listen(); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 108 | int port = sock1.getPort(); |
| 109 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 110 | clientSock.open(); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 111 | std::shared_ptr<TTransport> accepted = sock1.accept(); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 112 | // peek() will return false if child is interrupted |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 113 | boost::thread peekThread(std::bind(peekerWorker, accepted, false)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 114 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 115 | // peekThread is practically guaranteed to be blocking now |
| 116 | sock1.interruptChildren(); |
| 117 | BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 118 | "server socket interruptChildren did not interrupt child peek"); |
| 119 | clientSock.close(); |
| 120 | accepted->close(); |
| 121 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 124 | BOOST_AUTO_TEST_CASE(test_non_interruptable_child_peek) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 125 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 126 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 127 | sock1.listen(); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 128 | int port = sock1.getPort(); |
| 129 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 130 | clientSock.open(); |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 131 | std::shared_ptr<TTransport> accepted = sock1.accept(); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 132 | // peek() will return false when remote side is closed |
cyy | 316723a | 2019-01-05 16:35:14 +0800 | [diff] [blame] | 133 | boost::thread peekThread(std::bind(peekerWorker, accepted, false)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 134 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 135 | // peekThread is practically guaranteed to be blocking now |
| 136 | sock1.interruptChildren(); |
| 137 | BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 138 | "server socket interruptChildren interrupted child peek"); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 139 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 140 | // only way to proceed is to have the client disconnect |
| 141 | clientSock.close(); |
| 142 | peekThread.join(); |
| 143 | accepted->close(); |
| 144 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | BOOST_AUTO_TEST_SUITE_END() |