blob: 02da3eeeadfabf96115c09d02d8454814c7e261e [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>
29#include "TestPortFixture.h"
30
31using apache::thrift::transport::TServerSocket;
32using apache::thrift::transport::TSocket;
33using apache::thrift::transport::TTransport;
34using apache::thrift::transport::TTransportException;
35
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020036BOOST_FIXTURE_TEST_SUITE(TSocketInterruptTest, TestPortFixture)
Ben Craig1684c422015-04-24 08:52:44 -050037
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020038void 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
43void readerWorkerMustThrow(boost::shared_ptr<TTransport> tt) {
44 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) {
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 Craig1684c422015-04-24 08:52:44 -050068}
69
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020070BOOST_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 Craig1684c422015-04-24 08:52:44 -050089}
90
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020091BOOST_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 Craig1684c422015-04-24 08:52:44 -050096}
97
Konrad Grochowski1f6e3802015-05-18 18:10:06 +020098void peekerWorker(boost::shared_ptr<TTransport> tt, bool expectedResult) {
99 BOOST_CHECK_EQUAL(expectedResult, tt->peek());
Ben Craig1684c422015-04-24 08:52:44 -0500100}
101
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200102BOOST_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 Craig1684c422015-04-24 08:52:44 -0500118}
119
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200120BOOST_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 Craig1684c422015-04-24 08:52:44 -0500134
Konrad Grochowski1f6e3802015-05-18 18:10:06 +0200135 // only way to proceed is to have the client disconnect
136 clientSock.close();
137 peekThread.join();
138 accepted->close();
139 sock1.close();
Ben Craig1684c422015-04-24 08:52:44 -0500140}
141
142BOOST_AUTO_TEST_SUITE_END()