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 | |
| 23 | #include <boost/bind.hpp> |
| 24 | #include <boost/chrono/duration.hpp> |
| 25 | #include <boost/date_time/posix_time/posix_time_duration.hpp> |
| 26 | #include <boost/thread/thread.hpp> |
| 27 | #include <thrift/transport/TSocket.h> |
| 28 | #include <thrift/transport/TServerSocket.h> |
| 29 | #include "TestPortFixture.h" |
| 30 | |
| 31 | using apache::thrift::transport::TServerSocket; |
| 32 | using apache::thrift::transport::TSocket; |
| 33 | using apache::thrift::transport::TTransport; |
| 34 | using apache::thrift::transport::TTransportException; |
| 35 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TSocketInterruptTest, TestPortFixture) |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 37 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 38 | void readerWorker(boost::shared_ptr<TTransport> tt, uint32_t expectedResult) { |
| 39 | uint8_t buf[4]; |
| 40 | BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4)); |
| 41 | } |
| 42 | |
| 43 | void readerWorkerMustThrow(boost::shared_ptr<TTransport> tt) { |
| 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) { |
| 54 | TServerSocket sock1("localhost", m_serverPort); |
| 55 | sock1.listen(); |
| 56 | TSocket clientSock("localhost", m_serverPort); |
| 57 | clientSock.open(); |
| 58 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 59 | boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted)); |
| 60 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 61 | // readThread is practically guaranteed to be blocking now |
| 62 | sock1.interruptChildren(); |
| 63 | BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 64 | "server socket interruptChildren did not interrupt child read"); |
| 65 | clientSock.close(); |
| 66 | accepted->close(); |
| 67 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 68 | } |
| 69 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 70 | BOOST_AUTO_TEST_CASE(test_non_interruptable_child_read) { |
| 71 | TServerSocket sock1("localhost", m_serverPort); |
| 72 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 73 | sock1.listen(); |
| 74 | TSocket clientSock("localhost", m_serverPort); |
| 75 | clientSock.open(); |
| 76 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 77 | boost::thread readThread(boost::bind(readerWorker, accepted, 0)); |
| 78 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 79 | // readThread is practically guaranteed to be blocking here |
| 80 | sock1.interruptChildren(); |
| 81 | BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)), |
| 82 | "server socket interruptChildren interrupted child read"); |
| 83 | |
| 84 | // only way to proceed is to have the client disconnect |
| 85 | clientSock.close(); |
| 86 | readThread.join(); |
| 87 | accepted->close(); |
| 88 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 89 | } |
| 90 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 91 | BOOST_AUTO_TEST_CASE(test_cannot_change_after_listen) { |
| 92 | TServerSocket sock1("localhost", m_serverPort); |
| 93 | sock1.listen(); |
| 94 | BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error); |
| 95 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 98 | void peekerWorker(boost::shared_ptr<TTransport> tt, bool expectedResult) { |
| 99 | BOOST_CHECK_EQUAL(expectedResult, tt->peek()); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 102 | BOOST_AUTO_TEST_CASE(test_interruptable_child_peek) { |
| 103 | TServerSocket sock1("localhost", m_serverPort); |
| 104 | sock1.listen(); |
| 105 | TSocket clientSock("localhost", m_serverPort); |
| 106 | clientSock.open(); |
| 107 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 108 | // peek() will return false if child is interrupted |
| 109 | boost::thread peekThread(boost::bind(peekerWorker, accepted, false)); |
| 110 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 111 | // peekThread is practically guaranteed to be blocking now |
| 112 | sock1.interruptChildren(); |
| 113 | BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 114 | "server socket interruptChildren did not interrupt child peek"); |
| 115 | clientSock.close(); |
| 116 | accepted->close(); |
| 117 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 120 | BOOST_AUTO_TEST_CASE(test_non_interruptable_child_peek) { |
| 121 | TServerSocket sock1("localhost", m_serverPort); |
| 122 | sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior |
| 123 | sock1.listen(); |
| 124 | TSocket clientSock("localhost", m_serverPort); |
| 125 | clientSock.open(); |
| 126 | boost::shared_ptr<TTransport> accepted = sock1.accept(); |
| 127 | // peek() will return false when remote side is closed |
| 128 | boost::thread peekThread(boost::bind(peekerWorker, accepted, false)); |
| 129 | boost::this_thread::sleep(boost::posix_time::milliseconds(50)); |
| 130 | // peekThread is practically guaranteed to be blocking now |
| 131 | sock1.interruptChildren(); |
| 132 | BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)), |
| 133 | "server socket interruptChildren interrupted child peek"); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 134 | |
Konrad Grochowski | 1f6e380 | 2015-05-18 18:10:06 +0200 | [diff] [blame] | 135 | // only way to proceed is to have the client disconnect |
| 136 | clientSock.close(); |
| 137 | peekThread.join(); |
| 138 | accepted->close(); |
| 139 | sock1.close(); |
Ben Craig | 1684c42 | 2015-04-24 08:52:44 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_SUITE_END() |