blob: a546c23a13e33c92e4b1df2adcc36ba648559666 [file] [log] [blame]
Ben Craig1684c422015-04-24 08:52:44 -05001/*
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 Craig1684c422015-04-24 08:52:44 -050023#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>
cyy316723a2019-01-05 16:35:14 +080028#include <memory>
Ben Craig1684c422015-04-24 08:52:44 -050029
30using apache::thrift::transport::TServerSocket;
31using apache::thrift::transport::TSocket;
32using apache::thrift::transport::TTransport;
33using apache::thrift::transport::TTransportException;
James E. King, III82ae9572017-08-05 12:23:54 -040034using namespace apache::thrift;
Ben Craig1684c422015-04-24 08:52:44 -050035
John Sirois9bd35302016-02-12 21:07:49 -070036BOOST_AUTO_TEST_SUITE(TSocketInterruptTest)
Ben Craig1684c422015-04-24 08:52:44 -050037
cyy316723a2019-01-05 16:35:14 +080038void readerWorker(std::shared_ptr<TTransport> tt, uint32_t expectedResult) {
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020039 uint8_t buf[4];
40 BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4));
41}
42
cyy316723a2019-01-05 16:35:14 +080043void readerWorkerMustThrow(std::shared_ptr<TTransport> tt) {
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020044 try {
Ben Craig1684c422015-04-24 08:52:44 -050045 uint8_t buf[4];
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020046 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 Craig1684c422015-04-24 08:52:44 -050051}
52
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020053BOOST_AUTO_TEST_CASE(test_interruptable_child_read) {
John Sirois9bd35302016-02-12 21:07:49 -070054 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020055 sock1.listen();
Mario Emmenlauered0bad32020-04-24 08:50:43 +020056 BOOST_CHECK(sock1.isOpen());
John Sirois9bd35302016-02-12 21:07:49 -070057 int port = sock1.getPort();
58 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020059 clientSock.open();
cyy316723a2019-01-05 16:35:14 +080060 std::shared_ptr<TTransport> accepted = sock1.accept();
61 boost::thread readThread(std::bind(readerWorkerMustThrow, accepted));
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020062 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 Craig1684c422015-04-24 08:52:44 -050070}
71
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020072BOOST_AUTO_TEST_CASE(test_non_interruptable_child_read) {
John Sirois9bd35302016-02-12 21:07:49 -070073 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020074 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
75 sock1.listen();
John Sirois9bd35302016-02-12 21:07:49 -070076 int port = sock1.getPort();
77 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020078 clientSock.open();
cyy316723a2019-01-05 16:35:14 +080079 std::shared_ptr<TTransport> accepted = sock1.accept();
80 boost::thread readThread(std::bind(readerWorker, accepted, 0));
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020081 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 Craig1684c422015-04-24 08:52:44 -050092}
93
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020094BOOST_AUTO_TEST_CASE(test_cannot_change_after_listen) {
John Sirois9bd35302016-02-12 21:07:49 -070095 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020096 sock1.listen();
97 BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error);
98 sock1.close();
Ben Craig1684c422015-04-24 08:52:44 -050099}
100
cyy316723a2019-01-05 16:35:14 +0800101void peekerWorker(std::shared_ptr<TTransport> tt, bool expectedResult) {
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200102 BOOST_CHECK_EQUAL(expectedResult, tt->peek());
Ben Craig1684c422015-04-24 08:52:44 -0500103}
104
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200105BOOST_AUTO_TEST_CASE(test_interruptable_child_peek) {
John Sirois9bd35302016-02-12 21:07:49 -0700106 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200107 sock1.listen();
John Sirois9bd35302016-02-12 21:07:49 -0700108 int port = sock1.getPort();
109 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200110 clientSock.open();
cyy316723a2019-01-05 16:35:14 +0800111 std::shared_ptr<TTransport> accepted = sock1.accept();
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200112 // peek() will return false if child is interrupted
cyy316723a2019-01-05 16:35:14 +0800113 boost::thread peekThread(std::bind(peekerWorker, accepted, false));
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200114 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 Craig1684c422015-04-24 08:52:44 -0500122}
123
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200124BOOST_AUTO_TEST_CASE(test_non_interruptable_child_peek) {
John Sirois9bd35302016-02-12 21:07:49 -0700125 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200126 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
127 sock1.listen();
John Sirois9bd35302016-02-12 21:07:49 -0700128 int port = sock1.getPort();
129 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200130 clientSock.open();
cyy316723a2019-01-05 16:35:14 +0800131 std::shared_ptr<TTransport> accepted = sock1.accept();
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200132 // peek() will return false when remote side is closed
cyy316723a2019-01-05 16:35:14 +0800133 boost::thread peekThread(std::bind(peekerWorker, accepted, false));
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200134 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 Craig1684c422015-04-24 08:52:44 -0500139
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200140 // only way to proceed is to have the client disconnect
141 clientSock.close();
142 peekThread.join();
143 accepted->close();
144 sock1.close();
Ben Craig1684c422015-04-24 08:52:44 -0500145}
146
147BOOST_AUTO_TEST_SUITE_END()