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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 #define BOOST_TEST_MODULE TSocketInterruptTest
21 #include <boost/test/auto_unit_test.hpp>
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>
30 using apache::thrift::transport::TServerSocket
;
31 using apache::thrift::transport::TSocket
;
32 using apache::thrift::transport::TTransport
;
33 using apache::thrift::transport::TTransportException
;
34 using namespace apache::thrift
;
36 BOOST_AUTO_TEST_SUITE(TSocketInterruptTest
)
38 void readerWorker(std::shared_ptr
<TTransport
> tt
, uint32_t expectedResult
) {
40 BOOST_CHECK_EQUAL(expectedResult
, tt
->read(buf
, 4));
43 void readerWorkerMustThrow(std::shared_ptr
<TTransport
> tt
) {
47 BOOST_ERROR("should not have gotten here");
48 } catch (const TTransportException
& tx
) {
49 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED
, tx
.getType());
53 BOOST_AUTO_TEST_CASE(test_interruptable_child_read
) {
54 TServerSocket
sock1("localhost", 0);
56 int port
= sock1
.getPort();
57 TSocket
clientSock("localhost", port
);
59 std::shared_ptr
<TTransport
> accepted
= sock1
.accept();
60 boost::thread
readThread(std::bind(readerWorkerMustThrow
, accepted
));
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");
71 BOOST_AUTO_TEST_CASE(test_non_interruptable_child_read
) {
72 TServerSocket
sock1("localhost", 0);
73 sock1
.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
75 int port
= sock1
.getPort();
76 TSocket
clientSock("localhost", port
);
78 std::shared_ptr
<TTransport
> accepted
= sock1
.accept();
79 boost::thread
readThread(std::bind(readerWorker
, accepted
, 0));
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");
86 // only way to proceed is to have the client disconnect
93 BOOST_AUTO_TEST_CASE(test_cannot_change_after_listen
) {
94 TServerSocket
sock1("localhost", 0);
96 BOOST_CHECK_THROW(sock1
.setInterruptableChildren(false), std::logic_error
);
100 void peekerWorker(std::shared_ptr
<TTransport
> tt
, bool expectedResult
) {
101 BOOST_CHECK_EQUAL(expectedResult
, tt
->peek());
104 BOOST_AUTO_TEST_CASE(test_interruptable_child_peek
) {
105 TServerSocket
sock1("localhost", 0);
107 int port
= sock1
.getPort();
108 TSocket
clientSock("localhost", port
);
110 std::shared_ptr
<TTransport
> accepted
= sock1
.accept();
111 // peek() will return false if child is interrupted
112 boost::thread
peekThread(std::bind(peekerWorker
, accepted
, false));
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");
123 BOOST_AUTO_TEST_CASE(test_non_interruptable_child_peek
) {
124 TServerSocket
sock1("localhost", 0);
125 sock1
.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
127 int port
= sock1
.getPort();
128 TSocket
clientSock("localhost", port
);
130 std::shared_ptr
<TTransport
> accepted
= sock1
.accept();
131 // peek() will return false when remote side is closed
132 boost::thread
peekThread(std::bind(peekerWorker
, accepted
, false));
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");
139 // only way to proceed is to have the client disconnect
146 BOOST_AUTO_TEST_SUITE_END()