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