David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [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 | */ |
Marco Molteni | 8349425 | 2015-04-16 13:50:20 +0200 | [diff] [blame] | 19 | |
tpcwang | f98d59f | 2016-03-23 16:18:52 -0700 | [diff] [blame] | 20 | #include <thrift/thrift-config.h> |
Marco Molteni | 8349425 | 2015-04-16 13:50:20 +0200 | [diff] [blame] | 21 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <time.h> |
Marco Molteni | 8349425 | 2015-04-16 13:50:20 +0200 | [diff] [blame] | 24 | #ifdef HAVE_SYS_SOCKET_H |
| 25 | #include <sys/socket.h> |
| 26 | #endif |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 27 | #include <sstream> |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 28 | #include <fstream> |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 29 | #include <thrift/stdcxx.h> |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 30 | |
| 31 | #include <boost/mpl/list.hpp> |
| 32 | #include <boost/shared_array.hpp> |
| 33 | #include <boost/random.hpp> |
| 34 | #include <boost/type_traits.hpp> |
| 35 | #include <boost/test/unit_test.hpp> |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 36 | #include <boost/version.hpp> |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 37 | |
Roger Meier | 49ff8b1 | 2012-04-13 09:12:31 +0000 | [diff] [blame] | 38 | #include <thrift/transport/TBufferTransports.h> |
| 39 | #include <thrift/transport/TFDTransport.h> |
| 40 | #include <thrift/transport/TFileTransport.h> |
| 41 | #include <thrift/transport/TZlibTransport.h> |
| 42 | #include <thrift/transport/TSocket.h> |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 43 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 44 | #include <thrift/concurrency/FunctionRunner.h> |
| 45 | #if _WIN32 |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 46 | #include <thrift/transport/TPipe.h> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 47 | #include <thrift/windows/TWinsockSingleton.h> |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 48 | #endif |
| 49 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 50 | using namespace apache::thrift::transport; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 51 | using namespace apache::thrift; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 52 | |
| 53 | static boost::mt19937 rng; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 54 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 55 | void initrand(unsigned int seed) { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 56 | rng.seed(seed); |
| 57 | } |
| 58 | |
| 59 | class SizeGenerator { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 60 | public: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 61 | virtual ~SizeGenerator() {} |
| 62 | virtual uint32_t nextSize() = 0; |
| 63 | virtual std::string describe() const = 0; |
| 64 | }; |
| 65 | |
| 66 | class ConstantSizeGenerator : public SizeGenerator { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 67 | public: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 68 | ConstantSizeGenerator(uint32_t value) : value_(value) {} |
| 69 | uint32_t nextSize() { return value_; } |
| 70 | std::string describe() const { |
| 71 | std::ostringstream desc; |
| 72 | desc << value_; |
| 73 | return desc.str(); |
| 74 | } |
| 75 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 76 | private: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 77 | uint32_t value_; |
| 78 | }; |
| 79 | |
| 80 | class RandomSizeGenerator : public SizeGenerator { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 81 | public: |
| 82 | RandomSizeGenerator(uint32_t min, uint32_t max) |
| 83 | : generator_(rng, boost::uniform_int<int>(min, max)) {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 84 | |
| 85 | uint32_t nextSize() { return generator_(); } |
| 86 | |
| 87 | std::string describe() const { |
| 88 | std::ostringstream desc; |
| 89 | desc << "rand(" << getMin() << ", " << getMax() << ")"; |
| 90 | return desc.str(); |
| 91 | } |
| 92 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 93 | uint32_t getMin() const { return (generator_.distribution().min)(); } |
| 94 | uint32_t getMax() const { return (generator_.distribution().max)(); } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 95 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 96 | private: |
| 97 | boost::variate_generator<boost::mt19937&, boost::uniform_int<int> > generator_; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /** |
| 101 | * This class exists solely to make the TEST_RW() macro easier to use. |
| 102 | * - it can be constructed implicitly from an integer |
| 103 | * - it can contain either a ConstantSizeGenerator or a RandomSizeGenerator |
| 104 | * (TEST_RW can't take a SizeGenerator pointer or reference, since it needs |
| 105 | * to make a copy of the generator to bind it to the test function.) |
| 106 | */ |
| 107 | class GenericSizeGenerator : public SizeGenerator { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 108 | public: |
| 109 | GenericSizeGenerator(uint32_t value) : generator_(new ConstantSizeGenerator(value)) {} |
| 110 | GenericSizeGenerator(uint32_t min, uint32_t max) |
| 111 | : generator_(new RandomSizeGenerator(min, max)) {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 112 | |
| 113 | uint32_t nextSize() { return generator_->nextSize(); } |
| 114 | std::string describe() const { return generator_->describe(); } |
| 115 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 116 | private: |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 117 | stdcxx::shared_ptr<SizeGenerator> generator_; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | /************************************************************************** |
| 121 | * Classes to set up coupled transports |
| 122 | **************************************************************************/ |
| 123 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 124 | /** |
| 125 | * Helper class to represent a coupled pair of transports. |
| 126 | * |
| 127 | * Data written to the out transport can be read from the in transport. |
| 128 | * |
| 129 | * This is used as the base class for the various coupled transport |
| 130 | * implementations. It shouldn't be instantiated directly. |
| 131 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 132 | template <class Transport_> |
| 133 | class CoupledTransports { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 134 | public: |
Roger Meier | 6f7681f | 2011-11-06 12:04:28 +0000 | [diff] [blame] | 135 | virtual ~CoupledTransports() {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 136 | typedef Transport_ TransportType; |
| 137 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 138 | CoupledTransports() : in(), out() {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 139 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 140 | stdcxx::shared_ptr<Transport_> in; |
| 141 | stdcxx::shared_ptr<Transport_> out; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 142 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 143 | private: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 144 | CoupledTransports(const CoupledTransports&); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 145 | CoupledTransports& operator=(const CoupledTransports&); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 148 | /** |
| 149 | * Coupled TMemoryBuffers |
| 150 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 151 | class CoupledMemoryBuffers : public CoupledTransports<TMemoryBuffer> { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 152 | public: |
| 153 | CoupledMemoryBuffers() : buf(new TMemoryBuffer) { |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 154 | in = buf; |
| 155 | out = buf; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 156 | } |
| 157 | |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 158 | stdcxx::shared_ptr<TMemoryBuffer> buf; |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | /** |
| 162 | * Helper template class for creating coupled transports that wrap |
| 163 | * another transport. |
| 164 | */ |
| 165 | template <class WrapperTransport_, class InnerCoupledTransports_> |
| 166 | class CoupledWrapperTransportsT : public CoupledTransports<WrapperTransport_> { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 167 | public: |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 168 | CoupledWrapperTransportsT() { |
| 169 | if (inner_.in) { |
| 170 | this->in.reset(new WrapperTransport_(inner_.in)); |
| 171 | } |
| 172 | if (inner_.out) { |
| 173 | this->out.reset(new WrapperTransport_(inner_.out)); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | InnerCoupledTransports_ inner_; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 180 | /** |
| 181 | * Coupled TBufferedTransports. |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 182 | */ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 183 | template <class InnerTransport_> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 184 | class CoupledBufferedTransportsT |
| 185 | : public CoupledWrapperTransportsT<TBufferedTransport, InnerTransport_> {}; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 186 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 187 | typedef CoupledBufferedTransportsT<CoupledMemoryBuffers> CoupledBufferedTransports; |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 188 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 189 | /** |
| 190 | * Coupled TFramedTransports. |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 191 | */ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 192 | template <class InnerTransport_> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 193 | class CoupledFramedTransportsT |
| 194 | : public CoupledWrapperTransportsT<TFramedTransport, InnerTransport_> {}; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 195 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 196 | typedef CoupledFramedTransportsT<CoupledMemoryBuffers> CoupledFramedTransports; |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 197 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 198 | /** |
| 199 | * Coupled TZlibTransports. |
| 200 | */ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 201 | template <class InnerTransport_> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 202 | class CoupledZlibTransportsT : public CoupledWrapperTransportsT<TZlibTransport, InnerTransport_> {}; |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 203 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 204 | typedef CoupledZlibTransportsT<CoupledMemoryBuffers> CoupledZlibTransports; |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 205 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 206 | #ifndef _WIN32 |
| 207 | // FD transport doesn't make much sense on Windows. |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 208 | /** |
| 209 | * Coupled TFDTransports. |
| 210 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 211 | class CoupledFDTransports : public CoupledTransports<TFDTransport> { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 212 | public: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 213 | CoupledFDTransports() { |
| 214 | int pipes[2]; |
| 215 | |
| 216 | if (pipe(pipes) != 0) { |
| 217 | return; |
| 218 | } |
| 219 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 220 | in.reset(new TFDTransport(pipes[0], TFDTransport::CLOSE_ON_DESTROY)); |
| 221 | out.reset(new TFDTransport(pipes[1], TFDTransport::CLOSE_ON_DESTROY)); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 222 | } |
| 223 | }; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 224 | #else |
| 225 | /** |
| 226 | * Coupled pipe transports |
| 227 | */ |
| 228 | class CoupledPipeTransports : public CoupledTransports<TPipe> { |
| 229 | public: |
| 230 | HANDLE hRead; |
| 231 | HANDLE hWrite; |
| 232 | |
| 233 | CoupledPipeTransports() { |
| 234 | BOOST_REQUIRE(CreatePipe(&hRead, &hWrite, NULL, 1048576 * 2)); |
| 235 | in.reset(new TPipe(hRead, hWrite)); |
| 236 | in->open(); |
| 237 | out = in; |
| 238 | } |
| 239 | }; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 240 | #endif |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 241 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 242 | /** |
| 243 | * Coupled TSockets |
| 244 | */ |
| 245 | class CoupledSocketTransports : public CoupledTransports<TSocket> { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 246 | public: |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 247 | CoupledSocketTransports() { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 248 | THRIFT_SOCKET sockets[2] = {0}; |
| 249 | if (THRIFT_SOCKETPAIR(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) { |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 250 | return; |
| 251 | } |
| 252 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 253 | in.reset(new TSocket(sockets[0])); |
| 254 | out.reset(new TSocket(sockets[1])); |
Roger Meier | 967600e | 2013-05-03 22:39:53 +0200 | [diff] [blame] | 255 | out->setSendTimeout(100); |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 256 | } |
| 257 | }; |
| 258 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 259 | // These could be made to work on Windows, but I don't care enough to make it happen |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 260 | #ifndef _WIN32 |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 261 | /** |
| 262 | * Coupled TFileTransports |
| 263 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 264 | class CoupledFileTransports : public CoupledTransports<TFileTransport> { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 265 | public: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 266 | CoupledFileTransports() { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 267 | #ifndef _WIN32 |
| 268 | const char* tmp_dir = "/tmp"; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 269 | #define FILENAME_SUFFIX "/thrift.transport_test" |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 270 | #else |
| 271 | const char* tmp_dir = getenv("TMP"); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 272 | #define FILENAME_SUFFIX "\\thrift.transport_test" |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 273 | #endif |
| 274 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 275 | // Create a temporary file to use |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 276 | filename.resize(strlen(tmp_dir) + strlen(FILENAME_SUFFIX)); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 277 | THRIFT_SNPRINTF(&filename[0], filename.size(), "%s" FILENAME_SUFFIX, tmp_dir); |
| 278 | #undef FILENAME_SUFFIX |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 279 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 280 | { std::ofstream dummy_creation(filename.c_str(), std::ofstream::trunc); } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 281 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 282 | in.reset(new TFileTransport(filename, true)); |
| 283 | out.reset(new TFileTransport(filename)); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 286 | ~CoupledFileTransports() { remove(filename.c_str()); } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 287 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 288 | std::string filename; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 289 | }; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 290 | #endif |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 291 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 292 | /** |
| 293 | * Wrapper around another CoupledTransports implementation that exposes the |
| 294 | * transports as TTransport pointers. |
| 295 | * |
| 296 | * This is used since accessing a transport via a "TTransport*" exercises a |
| 297 | * different code path than using the base pointer class. As part of the |
| 298 | * template code changes, most transport methods are no longer virtual. |
| 299 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 300 | template <class CoupledTransports_> |
| 301 | class CoupledTTransports : public CoupledTransports<TTransport> { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 302 | public: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 303 | CoupledTTransports() : transports() { |
| 304 | in = transports.in; |
| 305 | out = transports.out; |
| 306 | } |
| 307 | |
| 308 | CoupledTransports_ transports; |
| 309 | }; |
| 310 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 311 | /** |
| 312 | * Wrapper around another CoupledTransports implementation that exposes the |
| 313 | * transports as TBufferBase pointers. |
| 314 | * |
| 315 | * This can only be instantiated with a transport type that is a subclass of |
| 316 | * TBufferBase. |
| 317 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 318 | template <class CoupledTransports_> |
| 319 | class CoupledBufferBases : public CoupledTransports<TBufferBase> { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 320 | public: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 321 | CoupledBufferBases() : transports() { |
| 322 | in = transports.in; |
| 323 | out = transports.out; |
| 324 | } |
| 325 | |
| 326 | CoupledTransports_ transports; |
| 327 | }; |
| 328 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 329 | /************************************************************************** |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 330 | * Alarm handling code for use in tests that check the transport blocking |
| 331 | * semantics. |
| 332 | * |
| 333 | * If the transport ends up blocking, we don't want to hang forever. We use |
| 334 | * SIGALRM to fire schedule signal to wake up and try to write data so the |
| 335 | * transport will unblock. |
| 336 | * |
| 337 | * It isn't really the safest thing in the world to be mucking around with |
| 338 | * complicated global data structures in a signal handler. It should probably |
| 339 | * be okay though, since we know the main thread should always be blocked in a |
| 340 | * read() request when the signal handler is running. |
| 341 | **************************************************************************/ |
| 342 | |
| 343 | struct TriggerInfo { |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 344 | TriggerInfo(int seconds, const stdcxx::shared_ptr<TTransport>& transport, uint32_t writeLength) |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 345 | : timeoutSeconds(seconds), transport(transport), writeLength(writeLength), next(NULL) {} |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 346 | |
| 347 | int timeoutSeconds; |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 348 | stdcxx::shared_ptr<TTransport> transport; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 349 | uint32_t writeLength; |
| 350 | TriggerInfo* next; |
| 351 | }; |
| 352 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 353 | apache::thrift::concurrency::Monitor g_alarm_monitor; |
| 354 | TriggerInfo* g_triggerInfo; |
| 355 | unsigned int g_numTriggersFired; |
| 356 | bool g_teardown = false; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 357 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 358 | void alarm_handler() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 359 | TriggerInfo* info = NULL; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 360 | { |
| 361 | apache::thrift::concurrency::Synchronized s(g_alarm_monitor); |
| 362 | // The alarm timed out, which almost certainly means we're stuck |
| 363 | // on a transport that is incorrectly blocked. |
| 364 | ++g_numTriggersFired; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 365 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 366 | // Note: we print messages to stdout instead of stderr, since |
| 367 | // tools/test/runner only records stdout messages in the failure messages for |
| 368 | // boost tests. (boost prints its test info to stdout.) |
| 369 | printf("Timeout alarm expired; attempting to unblock transport\n"); |
| 370 | if (g_triggerInfo == NULL) { |
| 371 | printf(" trigger stack is empty!\n"); |
| 372 | } |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 373 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 374 | // Pop off the first TriggerInfo. |
| 375 | // If there is another one, schedule an alarm for it. |
| 376 | info = g_triggerInfo; |
| 377 | g_triggerInfo = info->next; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 378 | } |
| 379 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 380 | // Write some data to the transport to hopefully unblock it. |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 381 | uint8_t* buf = new uint8_t[info->writeLength]; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 382 | memset(buf, 'b', info->writeLength); |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 383 | boost::scoped_array<uint8_t> array(buf); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 384 | info->transport->write(buf, info->writeLength); |
| 385 | info->transport->flush(); |
| 386 | |
| 387 | delete info; |
| 388 | } |
| 389 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 390 | void alarm_handler_wrapper() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 391 | int64_t timeout = 0; // timeout of 0 means wait forever |
| 392 | while (true) { |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 393 | bool fireHandler = false; |
| 394 | { |
| 395 | apache::thrift::concurrency::Synchronized s(g_alarm_monitor); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 396 | if (g_teardown) |
| 397 | return; |
| 398 | // calculate timeout |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 399 | if (g_triggerInfo == NULL) { |
| 400 | timeout = 0; |
| 401 | } else { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 402 | timeout = g_triggerInfo->timeoutSeconds * 1000; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | int waitResult = g_alarm_monitor.waitForTimeRelative(timeout); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 406 | if (waitResult == THRIFT_ETIMEDOUT) |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 407 | fireHandler = true; |
| 408 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 409 | if (fireHandler) |
| 410 | alarm_handler(); // calling outside the lock |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 411 | } |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Add a trigger to be scheduled "seconds" seconds after the |
| 416 | * last currently scheduled trigger. |
| 417 | * |
| 418 | * (Note that this is not "seconds" from now. That might be more logical, but |
| 419 | * would require slightly more complicated sorting, rather than just appending |
| 420 | * to the end.) |
| 421 | */ |
| 422 | void add_trigger(unsigned int seconds, |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 423 | const stdcxx::shared_ptr<TTransport>& transport, |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 424 | uint32_t write_len) { |
| 425 | TriggerInfo* info = new TriggerInfo(seconds, transport, write_len); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 426 | { |
| 427 | apache::thrift::concurrency::Synchronized s(g_alarm_monitor); |
| 428 | if (g_triggerInfo == NULL) { |
| 429 | // This is the first trigger. |
| 430 | // Set g_triggerInfo, and schedule the alarm |
| 431 | g_triggerInfo = info; |
| 432 | g_alarm_monitor.notify(); |
| 433 | } else { |
| 434 | // Add this trigger to the end of the list |
| 435 | TriggerInfo* prev = g_triggerInfo; |
| 436 | while (prev->next) { |
| 437 | prev = prev->next; |
| 438 | } |
| 439 | prev->next = info; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 440 | } |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
| 444 | void clear_triggers() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 445 | TriggerInfo* info = NULL; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 446 | |
| 447 | { |
| 448 | apache::thrift::concurrency::Synchronized s(g_alarm_monitor); |
| 449 | info = g_triggerInfo; |
| 450 | g_triggerInfo = NULL; |
| 451 | g_numTriggersFired = 0; |
| 452 | g_alarm_monitor.notify(); |
| 453 | } |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 454 | |
| 455 | while (info != NULL) { |
| 456 | TriggerInfo* next = info->next; |
| 457 | delete info; |
| 458 | info = next; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | void set_trigger(unsigned int seconds, |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 463 | const stdcxx::shared_ptr<TTransport>& transport, |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 464 | uint32_t write_len) { |
| 465 | clear_triggers(); |
| 466 | add_trigger(seconds, transport, write_len); |
| 467 | } |
| 468 | |
| 469 | /************************************************************************** |
| 470 | * Test functions |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 471 | **************************************************************************/ |
| 472 | |
| 473 | /** |
| 474 | * Test interleaved write and read calls. |
| 475 | * |
| 476 | * Generates a buffer totalSize bytes long, then writes it to the transport, |
| 477 | * and verifies the written data can be read back correctly. |
| 478 | * |
| 479 | * Mode of operation: |
| 480 | * - call wChunkGenerator to figure out how large of a chunk to write |
| 481 | * - call wSizeGenerator to get the size for individual write() calls, |
| 482 | * and do this repeatedly until the entire chunk is written. |
| 483 | * - call rChunkGenerator to figure out how large of a chunk to read |
| 484 | * - call rSizeGenerator to get the size for individual read() calls, |
| 485 | * and do this repeatedly until the entire chunk is read. |
| 486 | * - repeat until the full buffer is written and read back, |
| 487 | * then compare the data read back against the original buffer |
| 488 | * |
| 489 | * |
| 490 | * - If any of the size generators return 0, this means to use the maximum |
| 491 | * possible size. |
| 492 | * |
| 493 | * - If maxOutstanding is non-zero, write chunk sizes will be chosen such that |
| 494 | * there are never more than maxOutstanding bytes waiting to be read back. |
| 495 | */ |
| 496 | template <class CoupledTransports> |
| 497 | void test_rw(uint32_t totalSize, |
| 498 | SizeGenerator& wSizeGenerator, |
| 499 | SizeGenerator& rSizeGenerator, |
| 500 | SizeGenerator& wChunkGenerator, |
| 501 | SizeGenerator& rChunkGenerator, |
| 502 | uint32_t maxOutstanding) { |
| 503 | CoupledTransports transports; |
| 504 | BOOST_REQUIRE(transports.in != NULL); |
| 505 | BOOST_REQUIRE(transports.out != NULL); |
| 506 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 507 | boost::shared_array<uint8_t> wbuf = boost::shared_array<uint8_t>(new uint8_t[totalSize]); |
| 508 | boost::shared_array<uint8_t> rbuf = boost::shared_array<uint8_t>(new uint8_t[totalSize]); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 509 | |
| 510 | // store some data in wbuf |
| 511 | for (uint32_t n = 0; n < totalSize; ++n) { |
| 512 | wbuf[n] = (n & 0xff); |
| 513 | } |
| 514 | // clear rbuf |
| 515 | memset(rbuf.get(), 0, totalSize); |
| 516 | |
| 517 | uint32_t total_written = 0; |
| 518 | uint32_t total_read = 0; |
| 519 | while (total_read < totalSize) { |
| 520 | // Determine how large a chunk of data to write |
| 521 | uint32_t wchunk_size = wChunkGenerator.nextSize(); |
| 522 | if (wchunk_size == 0 || wchunk_size > totalSize - total_written) { |
| 523 | wchunk_size = totalSize - total_written; |
| 524 | } |
| 525 | |
| 526 | // Make sure (total_written - total_read) + wchunk_size |
| 527 | // is less than maxOutstanding |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 528 | if (maxOutstanding > 0 && wchunk_size > maxOutstanding - (total_written - total_read)) { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 529 | wchunk_size = maxOutstanding - (total_written - total_read); |
| 530 | } |
| 531 | |
| 532 | // Write the chunk |
| 533 | uint32_t chunk_written = 0; |
| 534 | while (chunk_written < wchunk_size) { |
| 535 | uint32_t write_size = wSizeGenerator.nextSize(); |
| 536 | if (write_size == 0 || write_size > wchunk_size - chunk_written) { |
| 537 | write_size = wchunk_size - chunk_written; |
| 538 | } |
| 539 | |
Roger Meier | 967600e | 2013-05-03 22:39:53 +0200 | [diff] [blame] | 540 | try { |
| 541 | transports.out->write(wbuf.get() + total_written, write_size); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 542 | } catch (TTransportException& te) { |
Roger Meier | 967600e | 2013-05-03 22:39:53 +0200 | [diff] [blame] | 543 | if (te.getType() == TTransportException::TIMED_OUT) |
| 544 | break; |
| 545 | throw te; |
| 546 | } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 547 | chunk_written += write_size; |
| 548 | total_written += write_size; |
| 549 | } |
| 550 | |
| 551 | // Flush the data, so it will be available in the read transport |
| 552 | // Don't flush if wchunk_size is 0. (This should only happen if |
| 553 | // total_written == totalSize already, and we're only reading now.) |
| 554 | if (wchunk_size > 0) { |
| 555 | transports.out->flush(); |
| 556 | } |
| 557 | |
| 558 | // Determine how large a chunk of data to read back |
| 559 | uint32_t rchunk_size = rChunkGenerator.nextSize(); |
| 560 | if (rchunk_size == 0 || rchunk_size > total_written - total_read) { |
| 561 | rchunk_size = total_written - total_read; |
| 562 | } |
| 563 | |
| 564 | // Read the chunk |
| 565 | uint32_t chunk_read = 0; |
| 566 | while (chunk_read < rchunk_size) { |
| 567 | uint32_t read_size = rSizeGenerator.nextSize(); |
| 568 | if (read_size == 0 || read_size > rchunk_size - chunk_read) { |
| 569 | read_size = rchunk_size - chunk_read; |
| 570 | } |
| 571 | |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 572 | int bytes_read = -1; |
| 573 | try { |
| 574 | bytes_read = transports.in->read(rbuf.get() + total_read, read_size); |
| 575 | } catch (TTransportException& e) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 576 | BOOST_FAIL("read(pos=" << total_read << ", size=" << read_size << ") threw exception \"" |
| 577 | << e.what() << "\"; written so far: " << total_written << " / " |
| 578 | << totalSize << " bytes"); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 579 | } |
| 580 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 581 | BOOST_REQUIRE_MESSAGE(bytes_read > 0, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 582 | "read(pos=" << total_read << ", size=" << read_size << ") returned " |
| 583 | << bytes_read << "; written so far: " << total_written |
| 584 | << " / " << totalSize << " bytes"); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 585 | chunk_read += bytes_read; |
| 586 | total_read += bytes_read; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // make sure the data read back is identical to the data written |
| 591 | BOOST_CHECK_EQUAL(memcmp(rbuf.get(), wbuf.get(), totalSize), 0); |
| 592 | } |
| 593 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 594 | template <class CoupledTransports> |
| 595 | void test_read_part_available() { |
| 596 | CoupledTransports transports; |
| 597 | BOOST_REQUIRE(transports.in != NULL); |
| 598 | BOOST_REQUIRE(transports.out != NULL); |
| 599 | |
| 600 | uint8_t write_buf[16]; |
| 601 | uint8_t read_buf[16]; |
| 602 | memset(write_buf, 'a', sizeof(write_buf)); |
| 603 | |
| 604 | // Attemping to read 10 bytes when only 9 are available should return 9 |
| 605 | // immediately. |
| 606 | transports.out->write(write_buf, 9); |
| 607 | transports.out->flush(); |
| 608 | set_trigger(3, transports.out, 1); |
| 609 | uint32_t bytes_read = transports.in->read(read_buf, 10); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 610 | BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0); |
| 611 | BOOST_CHECK_EQUAL(bytes_read, (uint32_t)9); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 612 | |
| 613 | clear_triggers(); |
| 614 | } |
| 615 | |
| 616 | template <class CoupledTransports> |
Roger Meier | 02c827b | 2012-04-11 21:59:57 +0000 | [diff] [blame] | 617 | void test_read_part_available_in_chunks() { |
| 618 | CoupledTransports transports; |
| 619 | BOOST_REQUIRE(transports.in != NULL); |
| 620 | BOOST_REQUIRE(transports.out != NULL); |
| 621 | |
| 622 | uint8_t write_buf[16]; |
| 623 | uint8_t read_buf[16]; |
| 624 | memset(write_buf, 'a', sizeof(write_buf)); |
| 625 | |
| 626 | // Write 10 bytes (in a single frame, for transports that use framing) |
| 627 | transports.out->write(write_buf, 10); |
| 628 | transports.out->flush(); |
| 629 | |
| 630 | // Read 1 byte, to force the transport to read the frame |
| 631 | uint32_t bytes_read = transports.in->read(read_buf, 1); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 632 | BOOST_CHECK_EQUAL(bytes_read, 1u); |
Roger Meier | 02c827b | 2012-04-11 21:59:57 +0000 | [diff] [blame] | 633 | |
| 634 | // Read more than what is remaining and verify the transport does not block |
| 635 | set_trigger(3, transports.out, 1); |
| 636 | bytes_read = transports.in->read(read_buf, 10); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 637 | BOOST_CHECK_EQUAL(g_numTriggersFired, 0u); |
| 638 | BOOST_CHECK_EQUAL(bytes_read, 9u); |
Roger Meier | 02c827b | 2012-04-11 21:59:57 +0000 | [diff] [blame] | 639 | |
| 640 | clear_triggers(); |
| 641 | } |
| 642 | |
| 643 | template <class CoupledTransports> |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 644 | void test_read_partial_midframe() { |
| 645 | CoupledTransports transports; |
| 646 | BOOST_REQUIRE(transports.in != NULL); |
| 647 | BOOST_REQUIRE(transports.out != NULL); |
| 648 | |
| 649 | uint8_t write_buf[16]; |
| 650 | uint8_t read_buf[16]; |
| 651 | memset(write_buf, 'a', sizeof(write_buf)); |
| 652 | |
| 653 | // Attempt to read 10 bytes, when only 9 are available, but after we have |
| 654 | // already read part of the data that is available. This exercises a |
| 655 | // different code path for several of the transports. |
| 656 | // |
| 657 | // For transports that add their own framing (e.g., TFramedTransport and |
| 658 | // TFileTransport), the two flush calls break up the data in to a 10 byte |
| 659 | // frame and a 3 byte frame. The first read then puts us partway through the |
| 660 | // first frame, and then we attempt to read past the end of that frame, and |
| 661 | // through the next frame, too. |
| 662 | // |
| 663 | // For buffered transports that perform read-ahead (e.g., |
| 664 | // TBufferedTransport), the read-ahead will most likely see all 13 bytes |
| 665 | // written on the first read. The next read will then attempt to read past |
| 666 | // the end of the read-ahead buffer. |
| 667 | // |
| 668 | // Flush 10 bytes, then 3 bytes. This creates 2 separate frames for |
| 669 | // transports that track framing internally. |
| 670 | transports.out->write(write_buf, 10); |
| 671 | transports.out->flush(); |
| 672 | transports.out->write(write_buf, 3); |
| 673 | transports.out->flush(); |
| 674 | |
| 675 | // Now read 4 bytes, so that we are partway through the written data. |
| 676 | uint32_t bytes_read = transports.in->read(read_buf, 4); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 677 | BOOST_CHECK_EQUAL(bytes_read, (uint32_t)4); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 678 | |
| 679 | // Now attempt to read 10 bytes. Only 9 more are available. |
| 680 | // |
| 681 | // We should be able to get all 9 bytes, but it might take multiple read |
| 682 | // calls, since it is valid for read() to return fewer bytes than requested. |
| 683 | // (Most transports do immediately return 9 bytes, but the framing transports |
| 684 | // tend to only return to the end of the current frame, which is 6 bytes in |
| 685 | // this case.) |
| 686 | uint32_t total_read = 0; |
| 687 | while (total_read < 9) { |
| 688 | set_trigger(3, transports.out, 1); |
| 689 | bytes_read = transports.in->read(read_buf, 10); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 690 | BOOST_REQUIRE_EQUAL(g_numTriggersFired, (unsigned int)0); |
| 691 | BOOST_REQUIRE_GT(bytes_read, (uint32_t)0); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 692 | total_read += bytes_read; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 693 | BOOST_REQUIRE_LE(total_read, (uint32_t)9); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 696 | BOOST_CHECK_EQUAL(total_read, (uint32_t)9); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 697 | |
| 698 | clear_triggers(); |
| 699 | } |
| 700 | |
| 701 | template <class CoupledTransports> |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 702 | void test_borrow_part_available() { |
| 703 | CoupledTransports transports; |
| 704 | BOOST_REQUIRE(transports.in != NULL); |
| 705 | BOOST_REQUIRE(transports.out != NULL); |
| 706 | |
| 707 | uint8_t write_buf[16]; |
| 708 | uint8_t read_buf[16]; |
| 709 | memset(write_buf, 'a', sizeof(write_buf)); |
| 710 | |
| 711 | // Attemping to borrow 10 bytes when only 9 are available should return NULL |
| 712 | // immediately. |
| 713 | transports.out->write(write_buf, 9); |
| 714 | transports.out->flush(); |
| 715 | set_trigger(3, transports.out, 1); |
| 716 | uint32_t borrow_len = 10; |
| 717 | const uint8_t* borrowed_buf = transports.in->borrow(read_buf, &borrow_len); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 718 | BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 719 | BOOST_CHECK(borrowed_buf == NULL); |
| 720 | |
| 721 | clear_triggers(); |
| 722 | } |
| 723 | |
| 724 | template <class CoupledTransports> |
| 725 | void test_read_none_available() { |
| 726 | CoupledTransports transports; |
| 727 | BOOST_REQUIRE(transports.in != NULL); |
| 728 | BOOST_REQUIRE(transports.out != NULL); |
| 729 | |
| 730 | uint8_t write_buf[16]; |
| 731 | uint8_t read_buf[16]; |
| 732 | memset(write_buf, 'a', sizeof(write_buf)); |
| 733 | |
| 734 | // Attempting to read when no data is available should either block until |
| 735 | // some data is available, or fail immediately. (e.g., TSocket blocks, |
| 736 | // TMemoryBuffer just fails.) |
| 737 | // |
| 738 | // If the transport blocks, it should succeed once some data is available, |
| 739 | // even if less than the amount requested becomes available. |
| 740 | set_trigger(1, transports.out, 2); |
| 741 | add_trigger(1, transports.out, 8); |
| 742 | uint32_t bytes_read = transports.in->read(read_buf, 10); |
| 743 | if (bytes_read == 0) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 744 | BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 745 | clear_triggers(); |
| 746 | } else { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 747 | BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)1); |
| 748 | BOOST_CHECK_EQUAL(bytes_read, (uint32_t)2); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | clear_triggers(); |
| 752 | } |
| 753 | |
| 754 | template <class CoupledTransports> |
| 755 | void test_borrow_none_available() { |
| 756 | CoupledTransports transports; |
| 757 | BOOST_REQUIRE(transports.in != NULL); |
| 758 | BOOST_REQUIRE(transports.out != NULL); |
| 759 | |
| 760 | uint8_t write_buf[16]; |
| 761 | memset(write_buf, 'a', sizeof(write_buf)); |
| 762 | |
| 763 | // Attempting to borrow when no data is available should fail immediately |
| 764 | set_trigger(1, transports.out, 10); |
| 765 | uint32_t borrow_len = 10; |
| 766 | const uint8_t* borrowed_buf = transports.in->borrow(NULL, &borrow_len); |
| 767 | BOOST_CHECK(borrowed_buf == NULL); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 768 | BOOST_CHECK_EQUAL(g_numTriggersFired, (unsigned int)0); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 769 | |
| 770 | clear_triggers(); |
| 771 | } |
| 772 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 773 | /************************************************************************** |
| 774 | * Test case generation |
| 775 | * |
| 776 | * Pretty ugly and annoying. This would be much easier if we the unit test |
| 777 | * framework didn't force each test to be a separate function. |
| 778 | * - Writing a completely separate function definition for each of these would |
| 779 | * result in a lot of repetitive boilerplate code. |
| 780 | * - Combining many tests into a single function makes it more difficult to |
| 781 | * tell precisely which tests failed. It also means you can't get a progress |
| 782 | * update after each test, and the tests are already fairly slow. |
Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 783 | * - Similar registration could be achieved with BOOST_TEST_CASE_TEMPLATE, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 784 | * but it requires a lot of awkward MPL code, and results in useless test |
| 785 | * case names. (The names are generated from std::type_info::name(), which |
| 786 | * is compiler-dependent. gcc returns mangled names.) |
| 787 | **************************************************************************/ |
| 788 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 789 | #define ADD_TEST_RW(CoupledTransports, totalSize, ...) \ |
| 790 | addTestRW<CoupledTransports>(BOOST_STRINGIZE(CoupledTransports), totalSize, ##__VA_ARGS__); |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 791 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 792 | #define TEST_RW(CoupledTransports, totalSize, ...) \ |
| 793 | do { \ |
| 794 | /* Add the test as specified, to test the non-virtual function calls */ \ |
| 795 | ADD_TEST_RW(CoupledTransports, totalSize, ##__VA_ARGS__); \ |
| 796 | /* \ |
| 797 | * Also test using the transport as a TTransport*, to test \ |
| 798 | * the read_virt()/write_virt() calls \ |
| 799 | */ \ |
| 800 | ADD_TEST_RW(CoupledTTransports<CoupledTransports>, totalSize, ##__VA_ARGS__); \ |
| 801 | /* Test wrapping the transport with TBufferedTransport */ \ |
| 802 | ADD_TEST_RW(CoupledBufferedTransportsT<CoupledTransports>, totalSize, ##__VA_ARGS__); \ |
| 803 | /* Test wrapping the transport with TFramedTransports */ \ |
| 804 | ADD_TEST_RW(CoupledFramedTransportsT<CoupledTransports>, totalSize, ##__VA_ARGS__); \ |
| 805 | /* Test wrapping the transport with TZlibTransport */ \ |
| 806 | ADD_TEST_RW(CoupledZlibTransportsT<CoupledTransports>, totalSize, ##__VA_ARGS__); \ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 807 | } while (0) |
| 808 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 809 | #define ADD_TEST_BLOCKING(CoupledTransports) \ |
| 810 | addTestBlocking<CoupledTransports>(BOOST_STRINGIZE(CoupledTransports)); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 811 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 812 | #define TEST_BLOCKING_BEHAVIOR(CoupledTransports) \ |
| 813 | ADD_TEST_BLOCKING(CoupledTransports); \ |
| 814 | ADD_TEST_BLOCKING(CoupledTTransports<CoupledTransports>); \ |
| 815 | ADD_TEST_BLOCKING(CoupledBufferedTransportsT<CoupledTransports>); \ |
| 816 | ADD_TEST_BLOCKING(CoupledFramedTransportsT<CoupledTransports>); \ |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 817 | ADD_TEST_BLOCKING(CoupledZlibTransportsT<CoupledTransports>); |
| 818 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 819 | class TransportTestGen { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 820 | public: |
| 821 | TransportTestGen(boost::unit_test::test_suite* suite, float sizeMultiplier) |
| 822 | : suite_(suite), sizeMultiplier_(sizeMultiplier) {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 823 | |
| 824 | void generate() { |
| 825 | GenericSizeGenerator rand4k(1, 4096); |
| 826 | |
| 827 | /* |
| 828 | * We do the basically the same set of tests for each transport type, |
| 829 | * although we tweak the parameters in some places. |
| 830 | */ |
| 831 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 832 | // TMemoryBuffer tests |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 833 | TEST_RW(CoupledMemoryBuffers, 1024 * 1024, 0, 0); |
| 834 | TEST_RW(CoupledMemoryBuffers, 1024 * 256, rand4k, rand4k); |
| 835 | TEST_RW(CoupledMemoryBuffers, 1024 * 256, 167, 163); |
| 836 | TEST_RW(CoupledMemoryBuffers, 1024 * 16, 1, 1); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 837 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 838 | TEST_RW(CoupledMemoryBuffers, 1024 * 256, 0, 0, rand4k, rand4k); |
| 839 | TEST_RW(CoupledMemoryBuffers, 1024 * 256, rand4k, rand4k, rand4k, rand4k); |
| 840 | TEST_RW(CoupledMemoryBuffers, 1024 * 256, 167, 163, rand4k, rand4k); |
| 841 | TEST_RW(CoupledMemoryBuffers, 1024 * 16, 1, 1, rand4k, rand4k); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 842 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 843 | TEST_BLOCKING_BEHAVIOR(CoupledMemoryBuffers); |
| 844 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 845 | #ifndef _WIN32 |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 846 | // TFDTransport tests |
| 847 | // Since CoupledFDTransports tests with a pipe, writes will block |
| 848 | // if there is too much outstanding unread data in the pipe. |
| 849 | uint32_t fd_max_outstanding = 4096; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 850 | TEST_RW(CoupledFDTransports, 1024 * 1024, 0, 0, 0, 0, fd_max_outstanding); |
| 851 | TEST_RW(CoupledFDTransports, 1024 * 256, rand4k, rand4k, 0, 0, fd_max_outstanding); |
| 852 | TEST_RW(CoupledFDTransports, 1024 * 256, 167, 163, 0, 0, fd_max_outstanding); |
| 853 | TEST_RW(CoupledFDTransports, 1024 * 16, 1, 1, 0, 0, fd_max_outstanding); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 854 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 855 | TEST_RW(CoupledFDTransports, 1024 * 256, 0, 0, rand4k, rand4k, fd_max_outstanding); |
| 856 | TEST_RW(CoupledFDTransports, 1024 * 256, rand4k, rand4k, rand4k, rand4k, fd_max_outstanding); |
| 857 | TEST_RW(CoupledFDTransports, 1024 * 256, 167, 163, rand4k, rand4k, fd_max_outstanding); |
| 858 | TEST_RW(CoupledFDTransports, 1024 * 16, 1, 1, rand4k, rand4k, fd_max_outstanding); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 859 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 860 | TEST_BLOCKING_BEHAVIOR(CoupledFDTransports); |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 861 | #else |
| 862 | // TPipe tests (WIN32 only) |
| 863 | TEST_RW(CoupledPipeTransports, 1024 * 1024, 0, 0); |
| 864 | TEST_RW(CoupledPipeTransports, 1024 * 256, rand4k, rand4k); |
| 865 | TEST_RW(CoupledPipeTransports, 1024 * 256, 167, 163); |
| 866 | TEST_RW(CoupledPipeTransports, 1024 * 16, 1, 1); |
| 867 | |
| 868 | TEST_RW(CoupledPipeTransports, 1024 * 256, 0, 0, rand4k, rand4k); |
| 869 | TEST_RW(CoupledPipeTransports, 1024 * 256, rand4k, rand4k, rand4k, rand4k); |
| 870 | TEST_RW(CoupledPipeTransports, 1024 * 256, 167, 163, rand4k, rand4k); |
| 871 | TEST_RW(CoupledPipeTransports, 1024 * 16, 1, 1, rand4k, rand4k); |
| 872 | |
| 873 | TEST_BLOCKING_BEHAVIOR(CoupledPipeTransports); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 874 | #endif //_WIN32 |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 875 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 876 | // TSocket tests |
| 877 | uint32_t socket_max_outstanding = 4096; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 878 | TEST_RW(CoupledSocketTransports, 1024 * 1024, 0, 0, 0, 0, socket_max_outstanding); |
| 879 | TEST_RW(CoupledSocketTransports, 1024 * 256, rand4k, rand4k, 0, 0, socket_max_outstanding); |
| 880 | TEST_RW(CoupledSocketTransports, 1024 * 256, 167, 163, 0, 0, socket_max_outstanding); |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 881 | // Doh. Apparently writing to a socket has some additional overhead for |
| 882 | // each send() call. If we have more than ~400 outstanding 1-byte write |
| 883 | // requests, additional send() calls start blocking. |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 884 | TEST_RW(CoupledSocketTransports, 1024 * 16, 1, 1, 0, 0, socket_max_outstanding); |
| 885 | TEST_RW(CoupledSocketTransports, 1024 * 256, 0, 0, rand4k, rand4k, socket_max_outstanding); |
| 886 | TEST_RW(CoupledSocketTransports, |
| 887 | 1024 * 256, |
| 888 | rand4k, |
| 889 | rand4k, |
| 890 | rand4k, |
| 891 | rand4k, |
| 892 | socket_max_outstanding); |
| 893 | TEST_RW(CoupledSocketTransports, 1024 * 256, 167, 163, rand4k, rand4k, socket_max_outstanding); |
| 894 | TEST_RW(CoupledSocketTransports, 1024 * 16, 1, 1, rand4k, rand4k, socket_max_outstanding); |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 895 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 896 | TEST_BLOCKING_BEHAVIOR(CoupledSocketTransports); |
| 897 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 898 | // These could be made to work on Windows, but I don't care enough to make it happen |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 899 | #ifndef _WIN32 |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 900 | // TFileTransport tests |
| 901 | // We use smaller buffer sizes here, since TFileTransport is fairly slow. |
| 902 | // |
| 903 | // TFileTransport can't write more than 16MB at once |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 904 | uint32_t max_write_at_once = 1024 * 1024 * 16 - 4; |
| 905 | TEST_RW(CoupledFileTransports, 1024 * 1024, max_write_at_once, 0); |
| 906 | TEST_RW(CoupledFileTransports, 1024 * 128, rand4k, rand4k); |
| 907 | TEST_RW(CoupledFileTransports, 1024 * 128, 167, 163); |
| 908 | TEST_RW(CoupledFileTransports, 1024 * 2, 1, 1); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 909 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 910 | TEST_RW(CoupledFileTransports, 1024 * 64, 0, 0, rand4k, rand4k); |
| 911 | TEST_RW(CoupledFileTransports, 1024 * 64, rand4k, rand4k, rand4k, rand4k); |
| 912 | TEST_RW(CoupledFileTransports, 1024 * 64, 167, 163, rand4k, rand4k); |
| 913 | TEST_RW(CoupledFileTransports, 1024 * 2, 1, 1, rand4k, rand4k); |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 914 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 915 | TEST_BLOCKING_BEHAVIOR(CoupledFileTransports); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 916 | #endif |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 917 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 918 | // Add some tests that access TBufferedTransport and TFramedTransport |
| 919 | // via TTransport pointers and TBufferBase pointers. |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 920 | ADD_TEST_RW(CoupledTTransports<CoupledBufferedTransports>, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 921 | 1024 * 1024, |
| 922 | rand4k, |
| 923 | rand4k, |
| 924 | rand4k, |
| 925 | rand4k); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 926 | ADD_TEST_RW(CoupledBufferBases<CoupledBufferedTransports>, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 927 | 1024 * 1024, |
| 928 | rand4k, |
| 929 | rand4k, |
| 930 | rand4k, |
| 931 | rand4k); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 932 | ADD_TEST_RW(CoupledTTransports<CoupledFramedTransports>, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 933 | 1024 * 1024, |
| 934 | rand4k, |
| 935 | rand4k, |
| 936 | rand4k, |
| 937 | rand4k); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 938 | ADD_TEST_RW(CoupledBufferBases<CoupledFramedTransports>, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 939 | 1024 * 1024, |
| 940 | rand4k, |
| 941 | rand4k, |
| 942 | rand4k, |
| 943 | rand4k); |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 944 | |
| 945 | // Test using TZlibTransport via a TTransport pointer |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 946 | ADD_TEST_RW(CoupledTTransports<CoupledZlibTransports>, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 947 | 1024 * 1024, |
| 948 | rand4k, |
| 949 | rand4k, |
| 950 | rand4k, |
| 951 | rand4k); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 954 | #if (BOOST_VERSION >= 105900) |
| 955 | #define MAKE_TEST_CASE(_FUNC, _NAME) boost::unit_test::make_test_case(_FUNC, _NAME, __FILE__, __LINE__) |
| 956 | #else |
| 957 | #define MAKE_TEST_CASE(_FUNC, _NAME) boost::unit_test::make_test_case(_FUNC, _NAME) |
| 958 | #endif |
| 959 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 960 | private: |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 961 | template <class CoupledTransports> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 962 | void addTestRW(const char* transport_name, |
| 963 | uint32_t totalSize, |
| 964 | GenericSizeGenerator wSizeGen, |
| 965 | GenericSizeGenerator rSizeGen, |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 966 | GenericSizeGenerator wChunkSizeGen = 0, |
| 967 | GenericSizeGenerator rChunkSizeGen = 0, |
| 968 | uint32_t maxOutstanding = 0, |
| 969 | uint32_t expectedFailures = 0) { |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 970 | // adjust totalSize by the specified sizeMultiplier_ first |
| 971 | totalSize = static_cast<uint32_t>(totalSize * sizeMultiplier_); |
| 972 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 973 | std::ostringstream name; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 974 | name << transport_name << "::test_rw(" << totalSize << ", " << wSizeGen.describe() << ", " |
| 975 | << rSizeGen.describe() << ", " << wChunkSizeGen.describe() << ", " |
| 976 | << rChunkSizeGen.describe() << ", " << maxOutstanding << ")"; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 977 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 978 | #if (BOOST_VERSION >= 105900) |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 979 | stdcxx::function<void ()> test_func |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 980 | #else |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 981 | boost::unit_test::callback0<> test_func |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 982 | #endif |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 983 | = stdcxx::bind(test_rw<CoupledTransports>, |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 984 | totalSize, |
| 985 | wSizeGen, |
| 986 | rSizeGen, |
| 987 | wChunkSizeGen, |
| 988 | rChunkSizeGen, |
| 989 | maxOutstanding); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 990 | suite_->add(MAKE_TEST_CASE(test_func, name.str()), expectedFailures); |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 991 | } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 992 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 993 | template <class CoupledTransports> |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 994 | void addTestBlocking(const char* transportName, uint32_t expectedFailures = 0) { |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 995 | char name[1024]; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 996 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 997 | THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available()", transportName); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 998 | suite_->add(MAKE_TEST_CASE(test_read_part_available<CoupledTransports>, name), expectedFailures); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 999 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1000 | THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_part_available_in_chunks()", transportName); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 1001 | suite_->add(MAKE_TEST_CASE(test_read_part_available_in_chunks<CoupledTransports>, name), expectedFailures); |
Roger Meier | 02c827b | 2012-04-11 21:59:57 +0000 | [diff] [blame] | 1002 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1003 | THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_partial_midframe()", transportName); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 1004 | suite_->add(MAKE_TEST_CASE(test_read_partial_midframe<CoupledTransports>, name), expectedFailures); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 1005 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1006 | THRIFT_SNPRINTF(name, sizeof(name), "%s::test_read_none_available()", transportName); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 1007 | suite_->add(MAKE_TEST_CASE(test_read_none_available<CoupledTransports>, name), expectedFailures); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 1008 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1009 | THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_part_available()", transportName); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 1010 | suite_->add(MAKE_TEST_CASE(test_borrow_part_available<CoupledTransports>, name), expectedFailures); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 1011 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1012 | THRIFT_SNPRINTF(name, sizeof(name), "%s::test_borrow_none_available()", transportName); |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 1013 | suite_->add(MAKE_TEST_CASE(test_borrow_none_available<CoupledTransports>, name), expectedFailures); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1016 | boost::unit_test::test_suite* suite_; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1017 | // sizeMultiplier_ is configurable via the command line, and allows the |
| 1018 | // user to adjust between smaller buffers that can be tested quickly, |
| 1019 | // or larger buffers that more thoroughly exercise the code, but take |
| 1020 | // longer. |
| 1021 | float sizeMultiplier_; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1022 | }; |
| 1023 | |
| 1024 | /************************************************************************** |
| 1025 | * General Initialization |
| 1026 | **************************************************************************/ |
| 1027 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1028 | struct global_fixture { |
James E. King, III | 82ae957 | 2017-08-05 12:23:54 -0400 | [diff] [blame] | 1029 | stdcxx::shared_ptr<apache::thrift::concurrency::Thread> alarmThread_; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1030 | global_fixture() { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1031 | #if _WIN32 |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1032 | apache::thrift::transport::TWinsockSingleton::create(); |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1033 | #endif |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1034 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1035 | apache::thrift::concurrency::PlatformThreadFactory factory; |
| 1036 | factory.setDetached(false); |
| 1037 | |
| 1038 | alarmThread_ = factory.newThread( |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1039 | apache::thrift::concurrency::FunctionRunner::create(alarm_handler_wrapper)); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1040 | alarmThread_->start(); |
| 1041 | } |
| 1042 | ~global_fixture() { |
| 1043 | { |
| 1044 | apache::thrift::concurrency::Synchronized s(g_alarm_monitor); |
| 1045 | g_teardown = true; |
| 1046 | g_alarm_monitor.notify(); |
| 1047 | } |
| 1048 | alarmThread_->join(); |
| 1049 | } |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1050 | }; |
| 1051 | |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 1052 | #if (BOOST_VERSION >= 105900) |
| 1053 | BOOST_GLOBAL_FIXTURE(global_fixture); |
| 1054 | #else |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1055 | BOOST_GLOBAL_FIXTURE(global_fixture) |
Konrad Grochowski | e9bdb41 | 2015-09-25 20:17:36 +0200 | [diff] [blame] | 1056 | #endif |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1057 | |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 1058 | #ifdef BOOST_TEST_DYN_LINK |
| 1059 | bool init_unit_test_suite() { |
| 1060 | struct timeval tv; |
| 1061 | THRIFT_GETTIMEOFDAY(&tv, NULL); |
| 1062 | int seed = tv.tv_sec ^ tv.tv_usec; |
| 1063 | |
| 1064 | initrand(seed); |
| 1065 | |
| 1066 | boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite(); |
| 1067 | suite->p_name.value = "TransportTest"; |
| 1068 | TransportTestGen transport_test_generator(suite, 1); |
| 1069 | transport_test_generator.generate(); |
| 1070 | return true; |
| 1071 | } |
| 1072 | |
| 1073 | int main( int argc, char* argv[] ) { |
| 1074 | return ::boost::unit_test::unit_test_main(&init_unit_test_suite,argc,argv); |
| 1075 | } |
| 1076 | #else |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1077 | boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { |
Konrad Grochowski | b3f5ffc | 2014-11-06 19:32:59 +0100 | [diff] [blame] | 1078 | THRIFT_UNUSED_VARIABLE(argc); |
| 1079 | THRIFT_UNUSED_VARIABLE(argv); |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1080 | struct timeval tv; |
| 1081 | THRIFT_GETTIMEOFDAY(&tv, NULL); |
| 1082 | int seed = tv.tv_sec ^ tv.tv_usec; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1083 | |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1084 | initrand(seed); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1085 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 1086 | boost::unit_test::test_suite* suite = &boost::unit_test::framework::master_test_suite(); |
David Reiss | 109693c | 2010-10-06 17:10:42 +0000 | [diff] [blame] | 1087 | suite->p_name.value = "TransportTest"; |
Jake Farrell | 5d02b80 | 2014-01-07 21:42:01 -0500 | [diff] [blame] | 1088 | TransportTestGen transport_test_generator(suite, 1); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1089 | transport_test_generator.generate(); |
David Reiss | 109693c | 2010-10-06 17:10:42 +0000 | [diff] [blame] | 1090 | return NULL; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1091 | } |
Antonio Di Monaco | 796667b | 2016-01-04 23:05:19 +0100 | [diff] [blame] | 1092 | #endif |