blob: b0e246db160a98bb921e406e0ce58f48f07be788 [file] [log] [blame]
ben-craigaf2d9c82015-07-16 08:11:21 -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#include <boost/test/test_tools.hpp>
21#include <boost/test/unit_test_suite.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/TPipe.h>
28#include <thrift/transport/TPipeServer.h>
29
30using apache::thrift::transport::TPipeServer;
31using apache::thrift::transport::TPipe;
32using apache::thrift::transport::TTransport;
33using apache::thrift::transport::TTransportException;
34
35BOOST_AUTO_TEST_SUITE(TPipeInterruptTest)
36
37// TODO: duplicate the test cases in TSocketInterruptTest for pipes,
38// once pipes implement interruptChildren
39
40BOOST_AUTO_TEST_CASE(test_interrupt_before_accept) {
41 TPipeServer pipe1("TPipeInterruptTest");
42 pipe1.listen();
43 pipe1.interrupt();
44 BOOST_CHECK_THROW(pipe1.accept(), TTransportException);
45}
46
47static void acceptWorker(TPipeServer *pipe) {
48 try
49 {
50 for (;;)
51 {
52 boost::shared_ptr<TTransport> temp = pipe->accept();
53 }
54 }
55 catch (...) {/*just want to make sure nothing crashes*/ }
56}
57
58static void interruptWorker(TPipeServer *pipe) {
59 boost::this_thread::sleep(boost::posix_time::milliseconds(10));
60 pipe->interrupt();
61}
62
63BOOST_AUTO_TEST_CASE(stress_pipe_accept_interruption) {
64 int interruptIters = 100;
65
66 for (int i = 0; i < interruptIters; ++i)
67 {
68 TPipeServer pipeServer("TPipeInterruptTest");
69 pipeServer.listen();
70 boost::thread acceptThread(boost::bind(acceptWorker, &pipeServer));
71 boost::thread interruptThread(boost::bind(interruptWorker, &pipeServer));
72 try
73 {
74 for (;;)
75 {
76 TPipe client("TPipeInterruptTest");
77 client.setConnectTimeout(1);
78 client.open();
79 }
80 } catch (...) { /*just testing for crashes*/ }
81 interruptThread.join();
82 acceptThread.join();
83 }
84}
85
86BOOST_AUTO_TEST_SUITE_END()