ben-craig | af2d9c8 | 2015-07-16 08:11:21 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 30 | using apache::thrift::transport::TPipeServer; |
| 31 | using apache::thrift::transport::TPipe; |
| 32 | using apache::thrift::transport::TTransport; |
| 33 | using apache::thrift::transport::TTransportException; |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(TPipeInterruptTest) |
| 36 | |
| 37 | // TODO: duplicate the test cases in TSocketInterruptTest for pipes, |
| 38 | // once pipes implement interruptChildren |
| 39 | |
| 40 | BOOST_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 | |
| 47 | static 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 | |
| 58 | static void interruptWorker(TPipeServer *pipe) { |
| 59 | boost::this_thread::sleep(boost::posix_time::milliseconds(10)); |
| 60 | pipe->interrupt(); |
| 61 | } |
| 62 | |
| 63 | BOOST_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 | |
| 86 | BOOST_AUTO_TEST_SUITE_END() |