blob: 4f6b2bccbd74fe0083e3a23122c893ac24fe0def [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
36BOOST_FIXTURE_TEST_SUITE ( TSocketInterruptTest, TestPortFixture )
37
38void readerWorker(boost::shared_ptr<TTransport> tt, uint32_t expectedResult)
39{
40 uint8_t buf[4];
41 BOOST_CHECK_EQUAL(expectedResult, tt->read(buf, 4));
42}
43
44void readerWorkerMustThrow(boost::shared_ptr<TTransport> tt)
45{
46 try
47 {
48 uint8_t buf[4];
49 tt->read(buf, 4);
50 BOOST_ERROR("should not have gotten here");
51 }
52 catch (const TTransportException& tx)
53 {
54 BOOST_CHECK_EQUAL(TTransportException::INTERRUPTED, tx.getType());
55 }
56}
57
58BOOST_AUTO_TEST_CASE( test_interruptable_child_read )
59{
60 TServerSocket sock1("localhost", m_serverPort);
61 sock1.listen();
62 TSocket clientSock("localhost", m_serverPort);
63 clientSock.open();
64 boost::shared_ptr<TTransport> accepted = sock1.accept();
65 boost::thread readThread(boost::bind(readerWorkerMustThrow, accepted));
66 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
67 // readThread is practically guaranteed to be blocking now
68 sock1.interruptChildren();
69 BOOST_CHECK_MESSAGE(readThread.try_join_for(boost::chrono::milliseconds(200)),
70 "server socket interruptChildren did not interrupt child read");
71 clientSock.close();
72 accepted->close();
73 sock1.close();
74}
75
76BOOST_AUTO_TEST_CASE( test_non_interruptable_child_read )
77{
78 TServerSocket sock1("localhost", m_serverPort);
79 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
80 sock1.listen();
81 TSocket clientSock("localhost", m_serverPort);
82 clientSock.open();
83 boost::shared_ptr<TTransport> accepted = sock1.accept();
84 boost::thread readThread(boost::bind(readerWorker, accepted, 0));
85 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
86 // readThread is practically guaranteed to be blocking here
87 sock1.interruptChildren();
88 BOOST_CHECK_MESSAGE(!readThread.try_join_for(boost::chrono::milliseconds(200)),
89 "server socket interruptChildren interrupted child read");
90
91 // only way to proceed is to have the client disconnect
92 clientSock.close();
93 readThread.join();
94 accepted->close();
95 sock1.close();
96}
97
98BOOST_AUTO_TEST_CASE( test_cannot_change_after_listen )
99{
100 TServerSocket sock1("localhost", m_serverPort);
101 sock1.listen();
102 BOOST_CHECK_THROW(sock1.setInterruptableChildren(false), std::logic_error);
103 sock1.close();
104}
105
106void peekerWorker(boost::shared_ptr<TTransport> tt, bool expectedResult)
107{
108 BOOST_CHECK_EQUAL(expectedResult, tt->peek());
109}
110
111BOOST_AUTO_TEST_CASE( test_interruptable_child_peek )
112{
113 TServerSocket sock1("localhost", m_serverPort);
114 sock1.listen();
115 TSocket clientSock("localhost", m_serverPort);
116 clientSock.open();
117 boost::shared_ptr<TTransport> accepted = sock1.accept();
118 // peek() will return false if child is interrupted
119 boost::thread peekThread(boost::bind(peekerWorker, accepted, false));
120 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
121 // peekThread is practically guaranteed to be blocking now
122 sock1.interruptChildren();
123 BOOST_CHECK_MESSAGE(peekThread.try_join_for(boost::chrono::milliseconds(200)),
124 "server socket interruptChildren did not interrupt child peek");
125 clientSock.close();
126 accepted->close();
127 sock1.close();
128}
129
130BOOST_AUTO_TEST_CASE( test_non_interruptable_child_peek )
131{
132 TServerSocket sock1("localhost", m_serverPort);
133 sock1.setInterruptableChildren(false); // returns to pre-THRIFT-2441 behavior
134 sock1.listen();
135 TSocket clientSock("localhost", m_serverPort);
136 clientSock.open();
137 boost::shared_ptr<TTransport> accepted = sock1.accept();
138 // peek() will return false when remote side is closed
139 boost::thread peekThread(boost::bind(peekerWorker, accepted, false));
140 boost::this_thread::sleep(boost::posix_time::milliseconds(50));
141 // peekThread is practically guaranteed to be blocking now
142 sock1.interruptChildren();
143 BOOST_CHECK_MESSAGE(!peekThread.try_join_for(boost::chrono::milliseconds(200)),
144 "server socket interruptChildren interrupted child peek");
145
146 // only way to proceed is to have the client disconnect
147 clientSock.close();
148 peekThread.join();
149 accepted->close();
150 sock1.close();
151}
152
153BOOST_AUTO_TEST_SUITE_END()
154