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 | */ |
| 19 | #ifndef _GNU_SOURCE |
| 20 | #define _GNU_SOURCE // needed for getopt_long |
| 21 | #endif |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <time.h> |
| 25 | #include <unistd.h> |
| 26 | #include <getopt.h> |
| 27 | #include <sstream> |
| 28 | #include <tr1/functional> |
| 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> |
| 35 | |
| 36 | #include <transport/TBufferTransports.h> |
| 37 | #include <transport/TFDTransport.h> |
| 38 | #include <transport/TFileTransport.h> |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 39 | #include <transport/TZlibTransport.h> |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 40 | #include <transport/TSocket.h> |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 41 | |
| 42 | using namespace apache::thrift::transport; |
| 43 | |
| 44 | static boost::mt19937 rng; |
| 45 | static const char* tmp_dir = "/tmp"; |
| 46 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 47 | void initrand(unsigned int seed) { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 48 | rng.seed(seed); |
| 49 | } |
| 50 | |
| 51 | class SizeGenerator { |
| 52 | public: |
| 53 | virtual ~SizeGenerator() {} |
| 54 | virtual uint32_t nextSize() = 0; |
| 55 | virtual std::string describe() const = 0; |
| 56 | }; |
| 57 | |
| 58 | class ConstantSizeGenerator : public SizeGenerator { |
| 59 | public: |
| 60 | ConstantSizeGenerator(uint32_t value) : value_(value) {} |
| 61 | uint32_t nextSize() { return value_; } |
| 62 | std::string describe() const { |
| 63 | std::ostringstream desc; |
| 64 | desc << value_; |
| 65 | return desc.str(); |
| 66 | } |
| 67 | |
| 68 | private: |
| 69 | uint32_t value_; |
| 70 | }; |
| 71 | |
| 72 | class RandomSizeGenerator : public SizeGenerator { |
| 73 | public: |
| 74 | RandomSizeGenerator(uint32_t min, uint32_t max) : |
| 75 | generator_(rng, boost::uniform_int<int>(min, max)) {} |
| 76 | |
| 77 | uint32_t nextSize() { return generator_(); } |
| 78 | |
| 79 | std::string describe() const { |
| 80 | std::ostringstream desc; |
| 81 | desc << "rand(" << getMin() << ", " << getMax() << ")"; |
| 82 | return desc.str(); |
| 83 | } |
| 84 | |
| 85 | uint32_t getMin() const { return generator_.distribution().min(); } |
| 86 | uint32_t getMax() const { return generator_.distribution().max(); } |
| 87 | |
| 88 | private: |
| 89 | boost::variate_generator< boost::mt19937&, boost::uniform_int<int> > |
| 90 | generator_; |
| 91 | }; |
| 92 | |
| 93 | /** |
| 94 | * This class exists solely to make the TEST_RW() macro easier to use. |
| 95 | * - it can be constructed implicitly from an integer |
| 96 | * - it can contain either a ConstantSizeGenerator or a RandomSizeGenerator |
| 97 | * (TEST_RW can't take a SizeGenerator pointer or reference, since it needs |
| 98 | * to make a copy of the generator to bind it to the test function.) |
| 99 | */ |
| 100 | class GenericSizeGenerator : public SizeGenerator { |
| 101 | public: |
| 102 | GenericSizeGenerator(uint32_t value) : |
| 103 | generator_(new ConstantSizeGenerator(value)) {} |
| 104 | GenericSizeGenerator(uint32_t min, uint32_t max) : |
| 105 | generator_(new RandomSizeGenerator(min, max)) {} |
| 106 | |
| 107 | uint32_t nextSize() { return generator_->nextSize(); } |
| 108 | std::string describe() const { return generator_->describe(); } |
| 109 | |
| 110 | private: |
| 111 | boost::shared_ptr<SizeGenerator> generator_; |
| 112 | }; |
| 113 | |
| 114 | /************************************************************************** |
| 115 | * Classes to set up coupled transports |
| 116 | **************************************************************************/ |
| 117 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Helper class to represent a coupled pair of transports. |
| 120 | * |
| 121 | * Data written to the out transport can be read from the in transport. |
| 122 | * |
| 123 | * This is used as the base class for the various coupled transport |
| 124 | * implementations. It shouldn't be instantiated directly. |
| 125 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 126 | template <class Transport_> |
| 127 | class CoupledTransports { |
| 128 | public: |
| 129 | typedef Transport_ TransportType; |
| 130 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 131 | CoupledTransports() : in(), out() {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 132 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 133 | boost::shared_ptr<Transport_> in; |
| 134 | boost::shared_ptr<Transport_> out; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 135 | |
| 136 | private: |
| 137 | CoupledTransports(const CoupledTransports&); |
| 138 | CoupledTransports &operator=(const CoupledTransports&); |
| 139 | }; |
| 140 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 141 | /** |
| 142 | * Coupled TMemoryBuffers |
| 143 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 144 | class CoupledMemoryBuffers : public CoupledTransports<TMemoryBuffer> { |
| 145 | public: |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 146 | CoupledMemoryBuffers() : |
| 147 | buf(new TMemoryBuffer) { |
| 148 | in = buf; |
| 149 | out = buf; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 150 | } |
| 151 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 152 | boost::shared_ptr<TMemoryBuffer> buf; |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * Helper template class for creating coupled transports that wrap |
| 157 | * another transport. |
| 158 | */ |
| 159 | template <class WrapperTransport_, class InnerCoupledTransports_> |
| 160 | class CoupledWrapperTransportsT : public CoupledTransports<WrapperTransport_> { |
| 161 | public: |
| 162 | CoupledWrapperTransportsT() { |
| 163 | if (inner_.in) { |
| 164 | this->in.reset(new WrapperTransport_(inner_.in)); |
| 165 | } |
| 166 | if (inner_.out) { |
| 167 | this->out.reset(new WrapperTransport_(inner_.out)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | InnerCoupledTransports_ inner_; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 172 | }; |
| 173 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 174 | /** |
| 175 | * Coupled TBufferedTransports. |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 176 | */ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 177 | template <class InnerTransport_> |
| 178 | class CoupledBufferedTransportsT : |
| 179 | public CoupledWrapperTransportsT<TBufferedTransport, InnerTransport_> { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 180 | }; |
| 181 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 182 | typedef CoupledBufferedTransportsT<CoupledMemoryBuffers> |
| 183 | CoupledBufferedTransports; |
| 184 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 185 | /** |
| 186 | * Coupled TFramedTransports. |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 187 | */ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 188 | template <class InnerTransport_> |
| 189 | class CoupledFramedTransportsT : |
| 190 | public CoupledWrapperTransportsT<TFramedTransport, InnerTransport_> { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 191 | }; |
| 192 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 193 | typedef CoupledFramedTransportsT<CoupledMemoryBuffers> |
| 194 | CoupledFramedTransports; |
| 195 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 196 | /** |
| 197 | * Coupled TZlibTransports. |
| 198 | */ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 199 | template <class InnerTransport_> |
| 200 | class CoupledZlibTransportsT : |
| 201 | public CoupledWrapperTransportsT<TZlibTransport, InnerTransport_> { |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 204 | typedef CoupledZlibTransportsT<CoupledMemoryBuffers> |
| 205 | CoupledZlibTransports; |
| 206 | |
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> { |
| 211 | public: |
| 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 | }; |
| 223 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 224 | /** |
| 225 | * Coupled TSockets |
| 226 | */ |
| 227 | class CoupledSocketTransports : public CoupledTransports<TSocket> { |
| 228 | public: |
| 229 | CoupledSocketTransports() { |
| 230 | int sockets[2]; |
| 231 | if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) { |
| 232 | return; |
| 233 | } |
| 234 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 235 | in.reset(new TSocket(sockets[0])); |
| 236 | out.reset(new TSocket(sockets[1])); |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 237 | } |
| 238 | }; |
| 239 | |
| 240 | /** |
| 241 | * Coupled TFileTransports |
| 242 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 243 | class CoupledFileTransports : public CoupledTransports<TFileTransport> { |
| 244 | public: |
| 245 | CoupledFileTransports() { |
| 246 | // Create a temporary file to use |
| 247 | size_t filename_len = strlen(tmp_dir) + 32; |
| 248 | filename = new char[filename_len]; |
| 249 | snprintf(filename, filename_len, |
| 250 | "%s/thrift.transport_test.XXXXXX", tmp_dir); |
| 251 | fd = mkstemp(filename); |
| 252 | if (fd < 0) { |
| 253 | return; |
| 254 | } |
| 255 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 256 | in.reset(new TFileTransport(filename, true)); |
| 257 | out.reset(new TFileTransport(filename)); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | ~CoupledFileTransports() { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 261 | if (fd >= 0) { |
| 262 | close(fd); |
| 263 | unlink(filename); |
| 264 | } |
| 265 | delete[] filename; |
| 266 | } |
| 267 | |
| 268 | char* filename; |
| 269 | int fd; |
| 270 | }; |
| 271 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 272 | /** |
| 273 | * Wrapper around another CoupledTransports implementation that exposes the |
| 274 | * transports as TTransport pointers. |
| 275 | * |
| 276 | * This is used since accessing a transport via a "TTransport*" exercises a |
| 277 | * different code path than using the base pointer class. As part of the |
| 278 | * template code changes, most transport methods are no longer virtual. |
| 279 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 280 | template <class CoupledTransports_> |
| 281 | class CoupledTTransports : public CoupledTransports<TTransport> { |
| 282 | public: |
| 283 | CoupledTTransports() : transports() { |
| 284 | in = transports.in; |
| 285 | out = transports.out; |
| 286 | } |
| 287 | |
| 288 | CoupledTransports_ transports; |
| 289 | }; |
| 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 TBufferBase pointers. |
| 294 | * |
| 295 | * This can only be instantiated with a transport type that is a subclass of |
| 296 | * TBufferBase. |
| 297 | */ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 298 | template <class CoupledTransports_> |
| 299 | class CoupledBufferBases : public CoupledTransports<TBufferBase> { |
| 300 | public: |
| 301 | CoupledBufferBases() : transports() { |
| 302 | in = transports.in; |
| 303 | out = transports.out; |
| 304 | } |
| 305 | |
| 306 | CoupledTransports_ transports; |
| 307 | }; |
| 308 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 309 | /************************************************************************** |
| 310 | * Main testing function |
| 311 | **************************************************************************/ |
| 312 | |
| 313 | /** |
| 314 | * Test interleaved write and read calls. |
| 315 | * |
| 316 | * Generates a buffer totalSize bytes long, then writes it to the transport, |
| 317 | * and verifies the written data can be read back correctly. |
| 318 | * |
| 319 | * Mode of operation: |
| 320 | * - call wChunkGenerator to figure out how large of a chunk to write |
| 321 | * - call wSizeGenerator to get the size for individual write() calls, |
| 322 | * and do this repeatedly until the entire chunk is written. |
| 323 | * - call rChunkGenerator to figure out how large of a chunk to read |
| 324 | * - call rSizeGenerator to get the size for individual read() calls, |
| 325 | * and do this repeatedly until the entire chunk is read. |
| 326 | * - repeat until the full buffer is written and read back, |
| 327 | * then compare the data read back against the original buffer |
| 328 | * |
| 329 | * |
| 330 | * - If any of the size generators return 0, this means to use the maximum |
| 331 | * possible size. |
| 332 | * |
| 333 | * - If maxOutstanding is non-zero, write chunk sizes will be chosen such that |
| 334 | * there are never more than maxOutstanding bytes waiting to be read back. |
| 335 | */ |
| 336 | template <class CoupledTransports> |
| 337 | void test_rw(uint32_t totalSize, |
| 338 | SizeGenerator& wSizeGenerator, |
| 339 | SizeGenerator& rSizeGenerator, |
| 340 | SizeGenerator& wChunkGenerator, |
| 341 | SizeGenerator& rChunkGenerator, |
| 342 | uint32_t maxOutstanding) { |
| 343 | CoupledTransports transports; |
| 344 | BOOST_REQUIRE(transports.in != NULL); |
| 345 | BOOST_REQUIRE(transports.out != NULL); |
| 346 | |
| 347 | boost::shared_array<uint8_t> wbuf = |
| 348 | boost::shared_array<uint8_t>(new uint8_t[totalSize]); |
| 349 | boost::shared_array<uint8_t> rbuf = |
| 350 | boost::shared_array<uint8_t>(new uint8_t[totalSize]); |
| 351 | |
| 352 | // store some data in wbuf |
| 353 | for (uint32_t n = 0; n < totalSize; ++n) { |
| 354 | wbuf[n] = (n & 0xff); |
| 355 | } |
| 356 | // clear rbuf |
| 357 | memset(rbuf.get(), 0, totalSize); |
| 358 | |
| 359 | uint32_t total_written = 0; |
| 360 | uint32_t total_read = 0; |
| 361 | while (total_read < totalSize) { |
| 362 | // Determine how large a chunk of data to write |
| 363 | uint32_t wchunk_size = wChunkGenerator.nextSize(); |
| 364 | if (wchunk_size == 0 || wchunk_size > totalSize - total_written) { |
| 365 | wchunk_size = totalSize - total_written; |
| 366 | } |
| 367 | |
| 368 | // Make sure (total_written - total_read) + wchunk_size |
| 369 | // is less than maxOutstanding |
| 370 | if (maxOutstanding > 0 && |
| 371 | wchunk_size > maxOutstanding - (total_written - total_read)) { |
| 372 | wchunk_size = maxOutstanding - (total_written - total_read); |
| 373 | } |
| 374 | |
| 375 | // Write the chunk |
| 376 | uint32_t chunk_written = 0; |
| 377 | while (chunk_written < wchunk_size) { |
| 378 | uint32_t write_size = wSizeGenerator.nextSize(); |
| 379 | if (write_size == 0 || write_size > wchunk_size - chunk_written) { |
| 380 | write_size = wchunk_size - chunk_written; |
| 381 | } |
| 382 | |
| 383 | transports.out->write(wbuf.get() + total_written, write_size); |
| 384 | chunk_written += write_size; |
| 385 | total_written += write_size; |
| 386 | } |
| 387 | |
| 388 | // Flush the data, so it will be available in the read transport |
| 389 | // Don't flush if wchunk_size is 0. (This should only happen if |
| 390 | // total_written == totalSize already, and we're only reading now.) |
| 391 | if (wchunk_size > 0) { |
| 392 | transports.out->flush(); |
| 393 | } |
| 394 | |
| 395 | // Determine how large a chunk of data to read back |
| 396 | uint32_t rchunk_size = rChunkGenerator.nextSize(); |
| 397 | if (rchunk_size == 0 || rchunk_size > total_written - total_read) { |
| 398 | rchunk_size = total_written - total_read; |
| 399 | } |
| 400 | |
| 401 | // Read the chunk |
| 402 | uint32_t chunk_read = 0; |
| 403 | while (chunk_read < rchunk_size) { |
| 404 | uint32_t read_size = rSizeGenerator.nextSize(); |
| 405 | if (read_size == 0 || read_size > rchunk_size - chunk_read) { |
| 406 | read_size = rchunk_size - chunk_read; |
| 407 | } |
| 408 | |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 409 | int bytes_read = -1; |
| 410 | try { |
| 411 | bytes_read = transports.in->read(rbuf.get() + total_read, read_size); |
| 412 | } catch (TTransportException& e) { |
| 413 | BOOST_FAIL("read(pos=" << total_read << ", size=" << read_size << |
| 414 | ") threw exception \"" << e.what() << |
| 415 | "\"; written so far: " << total_written << " / " << |
| 416 | totalSize << " bytes"); |
| 417 | } |
| 418 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 419 | BOOST_REQUIRE_MESSAGE(bytes_read > 0, |
| 420 | "read(pos=" << total_read << ", size=" << |
| 421 | read_size << ") returned " << bytes_read << |
| 422 | "; written so far: " << total_written << " / " << |
| 423 | totalSize << " bytes"); |
| 424 | chunk_read += bytes_read; |
| 425 | total_read += bytes_read; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | // make sure the data read back is identical to the data written |
| 430 | BOOST_CHECK_EQUAL(memcmp(rbuf.get(), wbuf.get(), totalSize), 0); |
| 431 | } |
| 432 | |
| 433 | /************************************************************************** |
| 434 | * Test case generation |
| 435 | * |
| 436 | * Pretty ugly and annoying. This would be much easier if we the unit test |
| 437 | * framework didn't force each test to be a separate function. |
| 438 | * - Writing a completely separate function definition for each of these would |
| 439 | * result in a lot of repetitive boilerplate code. |
| 440 | * - Combining many tests into a single function makes it more difficult to |
| 441 | * tell precisely which tests failed. It also means you can't get a progress |
| 442 | * update after each test, and the tests are already fairly slow. |
| 443 | * - Similar registration could be acheived with BOOST_TEST_CASE_TEMPLATE, |
| 444 | * but it requires a lot of awkward MPL code, and results in useless test |
| 445 | * case names. (The names are generated from std::type_info::name(), which |
| 446 | * is compiler-dependent. gcc returns mangled names.) |
| 447 | **************************************************************************/ |
| 448 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 449 | #define ADD_TEST(CoupledTransports, totalSize, ...) \ |
| 450 | addTest< CoupledTransports >(BOOST_STRINGIZE(CoupledTransports), \ |
| 451 | totalSize, ## __VA_ARGS__); |
| 452 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 453 | #define TEST_RW(CoupledTransports, totalSize, ...) \ |
| 454 | do { \ |
| 455 | /* Add the test as specified, to test the non-virtual function calls */ \ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 456 | ADD_TEST(CoupledTransports, totalSize, ## __VA_ARGS__); \ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 457 | /* \ |
| 458 | * Also test using the transport as a TTransport*, to test \ |
| 459 | * the read_virt()/write_virt() calls \ |
| 460 | */ \ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 461 | ADD_TEST(CoupledTTransports<CoupledTransports>, \ |
| 462 | totalSize, ## __VA_ARGS__); \ |
| 463 | /* Test wrapping the transport with TBufferedTransport */ \ |
| 464 | ADD_TEST(CoupledBufferedTransportsT<CoupledTransports>, \ |
| 465 | totalSize, ## __VA_ARGS__); \ |
| 466 | /* Test wrapping the transport with TFramedTransports */ \ |
| 467 | ADD_TEST(CoupledFramedTransportsT<CoupledTransports>, \ |
| 468 | totalSize, ## __VA_ARGS__); \ |
| 469 | /* Test wrapping the transport with TZlibTransport */ \ |
| 470 | ADD_TEST(CoupledZlibTransportsT<CoupledTransports>, \ |
| 471 | totalSize, ## __VA_ARGS__); \ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 472 | } while (0) |
| 473 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 474 | class TransportTestGen { |
| 475 | public: |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 476 | TransportTestGen(boost::unit_test::test_suite* suite, |
| 477 | float sizeMultiplier) : |
| 478 | suite_(suite), |
| 479 | sizeMultiplier_(sizeMultiplier) {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 480 | |
| 481 | void generate() { |
| 482 | GenericSizeGenerator rand4k(1, 4096); |
| 483 | |
| 484 | /* |
| 485 | * We do the basically the same set of tests for each transport type, |
| 486 | * although we tweak the parameters in some places. |
| 487 | */ |
| 488 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 489 | // TMemoryBuffer tests |
| 490 | TEST_RW(CoupledMemoryBuffers, 1024*1024, 0, 0); |
| 491 | TEST_RW(CoupledMemoryBuffers, 1024*256, rand4k, rand4k); |
| 492 | TEST_RW(CoupledMemoryBuffers, 1024*256, 167, 163); |
| 493 | TEST_RW(CoupledMemoryBuffers, 1024*16, 1, 1); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 494 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 495 | TEST_RW(CoupledMemoryBuffers, 1024*256, 0, 0, rand4k, rand4k); |
| 496 | TEST_RW(CoupledMemoryBuffers, 1024*256, rand4k, rand4k, rand4k, rand4k); |
| 497 | TEST_RW(CoupledMemoryBuffers, 1024*256, 167, 163, rand4k, rand4k); |
| 498 | TEST_RW(CoupledMemoryBuffers, 1024*16, 1, 1, rand4k, rand4k); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 499 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 500 | // TFDTransport tests |
| 501 | // Since CoupledFDTransports tests with a pipe, writes will block |
| 502 | // if there is too much outstanding unread data in the pipe. |
| 503 | uint32_t fd_max_outstanding = 4096; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 504 | TEST_RW(CoupledFDTransports, 1024*1024, 0, 0, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 505 | 0, 0, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 506 | TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 507 | 0, 0, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 508 | TEST_RW(CoupledFDTransports, 1024*256, 167, 163, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 509 | 0, 0, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 510 | TEST_RW(CoupledFDTransports, 1024*16, 1, 1, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 511 | 0, 0, fd_max_outstanding); |
| 512 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 513 | TEST_RW(CoupledFDTransports, 1024*256, 0, 0, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 514 | rand4k, rand4k, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 515 | TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 516 | rand4k, rand4k, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 517 | TEST_RW(CoupledFDTransports, 1024*256, 167, 163, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 518 | rand4k, rand4k, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 519 | TEST_RW(CoupledFDTransports, 1024*16, 1, 1, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 520 | rand4k, rand4k, fd_max_outstanding); |
| 521 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 522 | // TSocket tests |
| 523 | uint32_t socket_max_outstanding = 4096; |
| 524 | TEST_RW(CoupledSocketTransports, 1024*1024, 0, 0, |
| 525 | 0, 0, socket_max_outstanding); |
| 526 | TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k, |
| 527 | 0, 0, socket_max_outstanding); |
| 528 | TEST_RW(CoupledSocketTransports, 1024*256, 167, 163, |
| 529 | 0, 0, socket_max_outstanding); |
| 530 | // Doh. Apparently writing to a socket has some additional overhead for |
| 531 | // each send() call. If we have more than ~400 outstanding 1-byte write |
| 532 | // requests, additional send() calls start blocking. |
| 533 | TEST_RW(CoupledSocketTransports, 1024*16, 1, 1, |
| 534 | 0, 0, 400); |
| 535 | TEST_RW(CoupledSocketTransports, 1024*256, 0, 0, |
| 536 | rand4k, rand4k, socket_max_outstanding); |
| 537 | TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k, |
| 538 | rand4k, rand4k, socket_max_outstanding); |
| 539 | TEST_RW(CoupledSocketTransports, 1024*256, 167, 163, |
| 540 | rand4k, rand4k, socket_max_outstanding); |
| 541 | TEST_RW(CoupledSocketTransports, 1024*16, 1, 1, |
| 542 | rand4k, rand4k, 400); |
| 543 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 544 | // TFileTransport tests |
| 545 | // We use smaller buffer sizes here, since TFileTransport is fairly slow. |
| 546 | // |
| 547 | // TFileTransport can't write more than 16MB at once |
| 548 | uint32_t max_write_at_once = 1024*1024*16 - 4; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 549 | TEST_RW(CoupledFileTransports, 1024*1024, max_write_at_once, 0); |
| 550 | TEST_RW(CoupledFileTransports, 1024*128, rand4k, rand4k); |
| 551 | TEST_RW(CoupledFileTransports, 1024*128, 167, 163); |
| 552 | TEST_RW(CoupledFileTransports, 1024*2, 1, 1); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 553 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 554 | TEST_RW(CoupledFileTransports, 1024*64, 0, 0, rand4k, rand4k); |
| 555 | TEST_RW(CoupledFileTransports, 1024*64, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 556 | rand4k, rand4k, rand4k, rand4k); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 557 | TEST_RW(CoupledFileTransports, 1024*64, 167, 163, rand4k, rand4k); |
| 558 | TEST_RW(CoupledFileTransports, 1024*2, 1, 1, rand4k, rand4k); |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame^] | 559 | |
| 560 | // Add some tests that access TBufferedTransport and TFramedTransport |
| 561 | // via TTransport pointers and TBufferBase pointers. |
| 562 | ADD_TEST(CoupledTTransports<CoupledBufferedTransports>, |
| 563 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
| 564 | ADD_TEST(CoupledBufferBases<CoupledBufferedTransports>, |
| 565 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
| 566 | ADD_TEST(CoupledTTransports<CoupledFramedTransports>, |
| 567 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
| 568 | ADD_TEST(CoupledBufferBases<CoupledFramedTransports>, |
| 569 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
| 570 | |
| 571 | // Test using TZlibTransport via a TTransport pointer |
| 572 | ADD_TEST(CoupledTTransports<CoupledZlibTransports>, |
| 573 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | private: |
| 577 | template <class CoupledTransports> |
| 578 | void addTest(const char* transport_name, uint32_t totalSize, |
| 579 | GenericSizeGenerator wSizeGen, GenericSizeGenerator rSizeGen, |
| 580 | GenericSizeGenerator wChunkSizeGen = 0, |
| 581 | GenericSizeGenerator rChunkSizeGen = 0, |
| 582 | uint32_t maxOutstanding = 0, |
| 583 | uint32_t expectedFailures = 0) { |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 584 | // adjust totalSize by the specified sizeMultiplier_ first |
| 585 | totalSize = static_cast<uint32_t>(totalSize * sizeMultiplier_); |
| 586 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 587 | std::ostringstream name; |
| 588 | name << transport_name << "::test_rw(" << totalSize << ", " << |
| 589 | wSizeGen.describe() << ", " << rSizeGen.describe() << ", " << |
| 590 | wChunkSizeGen.describe() << ", " << rChunkSizeGen.describe() << ", " << |
| 591 | maxOutstanding << ")"; |
| 592 | |
| 593 | boost::unit_test::callback0<> test_func = |
| 594 | std::tr1::bind(test_rw<CoupledTransports>, totalSize, |
| 595 | wSizeGen, rSizeGen, wChunkSizeGen, rChunkSizeGen, |
| 596 | maxOutstanding); |
| 597 | boost::unit_test::test_case* tc = |
| 598 | boost::unit_test::make_test_case(test_func, name.str()); |
| 599 | suite_->add(tc, expectedFailures); |
| 600 | }; |
| 601 | |
| 602 | boost::unit_test::test_suite* suite_; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 603 | // sizeMultiplier_ is configurable via the command line, and allows the |
| 604 | // user to adjust between smaller buffers that can be tested quickly, |
| 605 | // or larger buffers that more thoroughly exercise the code, but take |
| 606 | // longer. |
| 607 | float sizeMultiplier_; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 608 | }; |
| 609 | |
| 610 | /************************************************************************** |
| 611 | * General Initialization |
| 612 | **************************************************************************/ |
| 613 | |
| 614 | void print_usage(FILE* f, const char* argv0) { |
| 615 | fprintf(f, "Usage: %s [boost_options] [options]\n", argv0); |
| 616 | fprintf(f, "Options:\n"); |
| 617 | fprintf(f, " --seed=<N>, -s <N>\n"); |
| 618 | fprintf(f, " --tmp-dir=DIR, -t DIR\n"); |
| 619 | fprintf(f, " --help\n"); |
| 620 | } |
| 621 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 622 | struct Options { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 623 | int seed; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 624 | bool haveSeed; |
| 625 | float sizeMultiplier; |
| 626 | }; |
| 627 | |
| 628 | void parse_args(int argc, char* argv[], Options* options) { |
| 629 | bool have_seed = false; |
| 630 | options->sizeMultiplier = 1; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 631 | |
| 632 | struct option long_opts[] = { |
| 633 | { "help", false, NULL, 'h' }, |
| 634 | { "seed", true, NULL, 's' }, |
| 635 | { "tmp-dir", true, NULL, 't' }, |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 636 | { "size-multiplier", true, NULL, 'x' }, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 637 | { NULL, 0, NULL, 0 } |
| 638 | }; |
| 639 | |
| 640 | while (true) { |
| 641 | optopt = 1; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 642 | int optchar = getopt_long(argc, argv, "hs:t:x:", long_opts, NULL); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 643 | if (optchar == -1) { |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | switch (optchar) { |
| 648 | case 't': |
| 649 | tmp_dir = optarg; |
| 650 | break; |
| 651 | case 's': { |
| 652 | char *endptr; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 653 | options->seed = strtol(optarg, &endptr, 0); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 654 | if (endptr == optarg || *endptr != '\0') { |
| 655 | fprintf(stderr, "invalid seed value \"%s\": must be an integer\n", |
| 656 | optarg); |
| 657 | exit(1); |
| 658 | } |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 659 | have_seed = true; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | case 'h': |
| 663 | print_usage(stdout, argv[0]); |
| 664 | exit(0); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 665 | case 'x': { |
| 666 | char *endptr; |
| 667 | options->sizeMultiplier = strtof(optarg, &endptr); |
| 668 | if (endptr == optarg || *endptr != '\0') { |
| 669 | fprintf(stderr, "invalid size multiplier \"%s\": must be a number\n", |
| 670 | optarg); |
| 671 | exit(1); |
| 672 | } |
| 673 | if (options->sizeMultiplier < 0) { |
| 674 | fprintf(stderr, "invalid size multiplier \"%s\": " |
| 675 | "must be non-negative\n", optarg); |
| 676 | exit(1); |
| 677 | } |
| 678 | break; |
| 679 | } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 680 | case '?': |
| 681 | exit(1); |
| 682 | default: |
| 683 | // Only happens if someone adds another option to the optarg string, |
| 684 | // but doesn't update the switch statement to handle it. |
| 685 | fprintf(stderr, "unknown option \"-%c\"\n", optchar); |
| 686 | exit(1); |
| 687 | } |
| 688 | } |
| 689 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 690 | if (!have_seed) { |
| 691 | // choose a seed now if the user didn't specify one |
| 692 | struct timespec t; |
| 693 | clock_gettime(CLOCK_REALTIME, &t); |
| 694 | options->seed = t.tv_sec + t.tv_nsec; |
| 695 | } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { |
| 699 | // Parse arguments |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 700 | Options options; |
| 701 | parse_args(argc, argv, &options); |
| 702 | |
| 703 | initrand(options.seed); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 704 | |
| 705 | boost::unit_test::test_suite* suite = BOOST_TEST_SUITE("TransportTests"); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 706 | TransportTestGen transport_test_generator(suite, options.sizeMultiplier); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 707 | transport_test_generator.generate(); |
| 708 | |
| 709 | return suite; |
| 710 | } |