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