blob: 27b89ebe66240df15ed36588d46e1871d713d9e6 [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
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>
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;
34
John Sirois9bd35302016-02-12 21:07:49 -070035BOOST_AUTO_TEST_SUITE(TSocketInterruptTest)
Ben Craig1684c422015-04-24 08:52:44 -050036
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020037void readerWorker(boost::shared_ptr<TTransport> tt, uint32_t expectedResult) {
38 uint8_t buf[4];
39 BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4));
40}
41
42void readerWorkerMustThrow(boost::shared_ptr<TTransport> tt) {
43 try {
Ben Craig1684c422015-04-24 08:52:44 -050044 uint8_t buf[4];
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020045 tt->read(buf, 4);
46 BOOST_ERROR("should not have gotten here");
47 } catch (const TTransportException& tx) {
48 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType());
49 }
Ben Craig1684c422015-04-24 08:52:44 -050050}
51
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020052BOOST_AUTO_TEST_CASE(test_interruptable_child_read) {
John Sirois9bd35302016-02-12 21:07:49 -070053 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020054 sock1.listen();
John Sirois9bd35302016-02-12 21:07:49 -070055 int port = sock1.getPort();
56 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020057 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 Craig1684c422015-04-24 08:52:44 -050068}
69
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020070BOOST_AUTO_TEST_CASE(test_non_interruptable_child_read) {
John Sirois9bd35302016-02-12 21:07:49 -070071 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020072 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
73 sock1.listen();
John Sirois9bd35302016-02-12 21:07:49 -070074 int port = sock1.getPort();
75 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020076 clientSock.open();
77 boost::shared_ptr<TTransport> accepted = sock1.accept();
78 boost::thread readThread(boost::bind(readerWorker, accepted, 0));
79 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
80 // readThread is practically guaranteed to be blocking here
81 sock1.interruptChildren();
82 BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)),
83 "server socket interruptChildren interrupted child read");
84
85 // only way to proceed is to have the client disconnect
86 clientSock.close();
87 readThread.join();
88 accepted->close();
89 sock1.close();
Ben Craig1684c422015-04-24 08:52:44 -050090}
91
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020092BOOST_AUTO_TEST_CASE(test_cannot_change_after_listen) {
John Sirois9bd35302016-02-12 21:07:49 -070093 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020094 sock1.listen();
95 BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error);
96 sock1.close();
Ben Craig1684c422015-04-24 08:52:44 -050097}
98
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020099void peekerWorker(boost::shared_ptr<TTransport> tt, bool expectedResult) {
100 BOOST_CHECK_EQUAL(expectedResult, tt->peek());
Ben Craig1684c422015-04-24 08:52:44 -0500101}
102
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200103BOOST_AUTO_TEST_CASE(test_interruptable_child_peek) {
John Sirois9bd35302016-02-12 21:07:49 -0700104 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200105 sock1.listen();
John Sirois9bd35302016-02-12 21:07:49 -0700106 int port = sock1.getPort();
107 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200108 clientSock.open();
109 boost::shared_ptr<TTransport> accepted = sock1.accept();
110 // peek() will return false if child is interrupted
111 boost::thread peekThread(boost::bind(peekerWorker, accepted, false));
112 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
113 // peekThread is practically guaranteed to be blocking now
114 sock1.interruptChildren();
115 BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)),
116 "server socket interruptChildren did not interrupt child peek");
117 clientSock.close();
118 accepted->close();
119 sock1.close();
Ben Craig1684c422015-04-24 08:52:44 -0500120}
121
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200122BOOST_AUTO_TEST_CASE(test_non_interruptable_child_peek) {
John Sirois9bd35302016-02-12 21:07:49 -0700123 TServerSocket sock1("localhost", 0);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200124 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
125 sock1.listen();
John Sirois9bd35302016-02-12 21:07:49 -0700126 int port = sock1.getPort();
127 TSocket clientSock("localhost", port);
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200128 clientSock.open();
129 boost::shared_ptr<TTransport> accepted = sock1.accept();
130 // peek() will return false when remote side is closed
131 boost::thread peekThread(boost::bind(peekerWorker, accepted, false));
132 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
133 // peekThread is practically guaranteed to be blocking now
134 sock1.interruptChildren();
135 BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)),
136 "server socket interruptChildren interrupted child peek");
Ben Craig1684c422015-04-24 08:52:44 -0500137
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200138 // only way to proceed is to have the client disconnect
139 clientSock.close();
140 peekThread.join();
141 accepted->close();
142 sock1.close();
Ben Craig1684c422015-04-24 08:52:44 -0500143}
144
145BOOST_AUTO_TEST_SUITE_END()