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> |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 28 | #include <thrift/stdcxx.h> |
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 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 38 | void readerWorker(stdcxx::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 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 43 | void readerWorkerMustThrow(stdcxx::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(); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 56 | int port = sock1.getPort(); |
| 57 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 58 | clientSock.open(); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 59 | stdcxx::shared_ptr<TTransport> accepted = sock1.accept(); |
| 60 | boost::thread readThread(stdcxx::bind(readerWorkerMustThrow, accepted)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 61 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 62 | // readThread is practically guaranteed to be blocking now |
| 63 | sock1.interruptChildren(); |
| 64 | BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 65 | "server socket interruptChildren did not interrupt child read"); |
| 66 | clientSock.close(); |
| 67 | accepted->close(); |
| 68 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(test_non_interruptable_child_read) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 72 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 73 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 74 | sock1.listen(); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 75 | int port = sock1.getPort(); |
| 76 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 77 | clientSock.open(); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 78 | stdcxx::shared_ptr<TTransport> accepted = sock1.accept(); |
| 79 | boost::thread readThread(stdcxx::bind(readerWorker, accepted, 0)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 80 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 81 | // readThread is practically guaranteed to be blocking here |
| 82 | sock1.interruptChildren(); |
| 83 | BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 84 | "server socket interruptChildren interrupted child read"); |
| 85 | |
| 86 | // only way to proceed is to have the client disconnect |
| 87 | clientSock.close(); |
| 88 | readThread.join(); |
| 89 | accepted->close(); |
| 90 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(test_cannot_change_after_listen) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 94 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 95 | sock1.listen(); |
| 96 | BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error); |
| 97 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 98 | } |
| 99 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 100 | void peekerWorker(stdcxx::shared_ptr<TTransport> tt, bool expectedResult) { |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 101 | BOOST_CHECK_EQUAL(expectedResult, tt->peek()); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 102 | } |
| 103 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 104 | BOOST_AUTO_TEST_CASE(test_interruptable_child_peek) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 105 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 106 | sock1.listen(); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 107 | int port = sock1.getPort(); |
| 108 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 109 | clientSock.open(); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 110 | stdcxx::shared_ptr<TTransport> accepted = sock1.accept(); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 111 | // peek() will return false if child is interrupted |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 112 | boost::thread peekThread(stdcxx::bind(peekerWorker, accepted, false)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 113 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 114 | // peekThread is practically guaranteed to be blocking now |
| 115 | sock1.interruptChildren(); |
| 116 | BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 117 | "server socket interruptChildren did not interrupt child peek"); |
| 118 | clientSock.close(); |
| 119 | accepted->close(); |
| 120 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(test_non_interruptable_child_peek) { |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 124 | TServerSocket sock1("localhost", 0); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 125 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 126 | sock1.listen(); |
John Sirois | 9bd3530 | 2016-02-12 21:07:49 -0700 | [diff] [blame] | 127 | int port = sock1.getPort(); |
| 128 | TSocket clientSock("localhost", port); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 129 | clientSock.open(); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 130 | stdcxx::shared_ptr<TTransport> accepted = sock1.accept(); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 131 | // peek() will return false when remote side is closed |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame^] | 132 | boost::thread peekThread(stdcxx::bind(peekerWorker, accepted, false)); |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 133 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 134 | // peekThread is practically guaranteed to be blocking now |
| 135 | sock1.interruptChildren(); |
| 136 | BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 137 | "server socket interruptChildren interrupted child peek"); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 138 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 139 | // only way to proceed is to have the client disconnect |
| 140 | clientSock.close(); |
| 141 | peekThread.join(); |
| 142 | accepted->close(); |
| 143 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | BOOST_AUTO_TEST_SUITE_END() |