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) { |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 344 | (void) signum; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 345 | // The alarm timed out, which almost certainly means we're stuck |
| 346 | // on a transport that is incorrectly blocked. |
| 347 | ++numTriggersFired; |
| 348 | |
| 349 | // Note: we print messages to stdout instead of stderr, since |
| 350 | // tools/test/runner only records stdout messages in the failure messages for |
| 351 | // boost tests. (boost prints its test info to stdout.) |
| 352 | printf("Timeout alarm expired; attempting to unblock transport\n"); |
| 353 | if (triggerInfo == NULL) { |
| 354 | printf(" trigger stack is empty!\n"); |
| 355 | } |
| 356 | |
| 357 | // Pop off the first TriggerInfo. |
| 358 | // If there is another one, schedule an alarm for it. |
| 359 | TriggerInfo* info = triggerInfo; |
| 360 | triggerInfo = info->next; |
| 361 | set_alarm(); |
| 362 | |
| 363 | // Write some data to the transport to hopefully unblock it. |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 364 | uint8_t* buf = new uint8_t[info->writeLength]; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 365 | memset(buf, 'b', info->writeLength); |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 366 | boost::scoped_array<uint8_t> array(buf); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 367 | info->transport->write(buf, info->writeLength); |
| 368 | info->transport->flush(); |
| 369 | |
| 370 | delete info; |
| 371 | } |
| 372 | |
| 373 | void set_alarm() { |
| 374 | if (triggerInfo == NULL) { |
| 375 | // clear any alarm |
| 376 | alarm(0); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | struct sigaction action; |
| 381 | memset(&action, 0, sizeof(action)); |
| 382 | action.sa_handler = alarm_handler; |
Christian Lavoie | 4f42ef7 | 2010-11-04 18:51:42 +0000 | [diff] [blame] | 383 | action.sa_flags = SA_RESETHAND; |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 384 | sigemptyset(&action.sa_mask); |
| 385 | sigaction(SIGALRM, &action, NULL); |
| 386 | |
| 387 | alarm(triggerInfo->timeoutSeconds); |
| 388 | } |
| 389 | |
| 390 | /** |
| 391 | * Add a trigger to be scheduled "seconds" seconds after the |
| 392 | * last currently scheduled trigger. |
| 393 | * |
| 394 | * (Note that this is not "seconds" from now. That might be more logical, but |
| 395 | * would require slightly more complicated sorting, rather than just appending |
| 396 | * to the end.) |
| 397 | */ |
| 398 | void add_trigger(unsigned int seconds, |
| 399 | const boost::shared_ptr<TTransport> &transport, |
| 400 | uint32_t write_len) { |
| 401 | TriggerInfo* info = new TriggerInfo(seconds, transport, write_len); |
| 402 | |
| 403 | if (triggerInfo == NULL) { |
| 404 | // This is the first trigger. |
| 405 | // Set triggerInfo, and schedule the alarm |
| 406 | triggerInfo = info; |
| 407 | set_alarm(); |
| 408 | } else { |
| 409 | // Add this trigger to the end of the list |
| 410 | TriggerInfo* prev = triggerInfo; |
| 411 | while (prev->next) { |
| 412 | prev = prev->next; |
| 413 | } |
| 414 | |
| 415 | prev->next = info; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void clear_triggers() { |
| 420 | TriggerInfo *info = triggerInfo; |
| 421 | alarm(0); |
| 422 | triggerInfo = NULL; |
| 423 | numTriggersFired = 0; |
| 424 | |
| 425 | while (info != NULL) { |
| 426 | TriggerInfo* next = info->next; |
| 427 | delete info; |
| 428 | info = next; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void set_trigger(unsigned int seconds, |
| 433 | const boost::shared_ptr<TTransport> &transport, |
| 434 | uint32_t write_len) { |
| 435 | clear_triggers(); |
| 436 | add_trigger(seconds, transport, write_len); |
| 437 | } |
| 438 | |
| 439 | /************************************************************************** |
| 440 | * Test functions |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 441 | **************************************************************************/ |
| 442 | |
| 443 | /** |
| 444 | * Test interleaved write and read calls. |
| 445 | * |
| 446 | * Generates a buffer totalSize bytes long, then writes it to the transport, |
| 447 | * and verifies the written data can be read back correctly. |
| 448 | * |
| 449 | * Mode of operation: |
| 450 | * - call wChunkGenerator to figure out how large of a chunk to write |
| 451 | * - call wSizeGenerator to get the size for individual write() calls, |
| 452 | * and do this repeatedly until the entire chunk is written. |
| 453 | * - call rChunkGenerator to figure out how large of a chunk to read |
| 454 | * - call rSizeGenerator to get the size for individual read() calls, |
| 455 | * and do this repeatedly until the entire chunk is read. |
| 456 | * - repeat until the full buffer is written and read back, |
| 457 | * then compare the data read back against the original buffer |
| 458 | * |
| 459 | * |
| 460 | * - If any of the size generators return 0, this means to use the maximum |
| 461 | * possible size. |
| 462 | * |
| 463 | * - If maxOutstanding is non-zero, write chunk sizes will be chosen such that |
| 464 | * there are never more than maxOutstanding bytes waiting to be read back. |
| 465 | */ |
| 466 | template <class CoupledTransports> |
| 467 | void test_rw(uint32_t totalSize, |
| 468 | SizeGenerator& wSizeGenerator, |
| 469 | SizeGenerator& rSizeGenerator, |
| 470 | SizeGenerator& wChunkGenerator, |
| 471 | SizeGenerator& rChunkGenerator, |
| 472 | uint32_t maxOutstanding) { |
| 473 | CoupledTransports transports; |
| 474 | BOOST_REQUIRE(transports.in != NULL); |
| 475 | BOOST_REQUIRE(transports.out != NULL); |
| 476 | |
| 477 | boost::shared_array<uint8_t> wbuf = |
| 478 | boost::shared_array<uint8_t>(new uint8_t[totalSize]); |
| 479 | boost::shared_array<uint8_t> rbuf = |
| 480 | boost::shared_array<uint8_t>(new uint8_t[totalSize]); |
| 481 | |
| 482 | // store some data in wbuf |
| 483 | for (uint32_t n = 0; n < totalSize; ++n) { |
| 484 | wbuf[n] = (n & 0xff); |
| 485 | } |
| 486 | // clear rbuf |
| 487 | memset(rbuf.get(), 0, totalSize); |
| 488 | |
| 489 | uint32_t total_written = 0; |
| 490 | uint32_t total_read = 0; |
| 491 | while (total_read < totalSize) { |
| 492 | // Determine how large a chunk of data to write |
| 493 | uint32_t wchunk_size = wChunkGenerator.nextSize(); |
| 494 | if (wchunk_size == 0 || wchunk_size > totalSize - total_written) { |
| 495 | wchunk_size = totalSize - total_written; |
| 496 | } |
| 497 | |
| 498 | // Make sure (total_written - total_read) + wchunk_size |
| 499 | // is less than maxOutstanding |
| 500 | if (maxOutstanding > 0 && |
| 501 | wchunk_size > maxOutstanding - (total_written - total_read)) { |
| 502 | wchunk_size = maxOutstanding - (total_written - total_read); |
| 503 | } |
| 504 | |
| 505 | // Write the chunk |
| 506 | uint32_t chunk_written = 0; |
| 507 | while (chunk_written < wchunk_size) { |
| 508 | uint32_t write_size = wSizeGenerator.nextSize(); |
| 509 | if (write_size == 0 || write_size > wchunk_size - chunk_written) { |
| 510 | write_size = wchunk_size - chunk_written; |
| 511 | } |
| 512 | |
| 513 | transports.out->write(wbuf.get() + total_written, write_size); |
| 514 | chunk_written += write_size; |
| 515 | total_written += write_size; |
| 516 | } |
| 517 | |
| 518 | // Flush the data, so it will be available in the read transport |
| 519 | // Don't flush if wchunk_size is 0. (This should only happen if |
| 520 | // total_written == totalSize already, and we're only reading now.) |
| 521 | if (wchunk_size > 0) { |
| 522 | transports.out->flush(); |
| 523 | } |
| 524 | |
| 525 | // Determine how large a chunk of data to read back |
| 526 | uint32_t rchunk_size = rChunkGenerator.nextSize(); |
| 527 | if (rchunk_size == 0 || rchunk_size > total_written - total_read) { |
| 528 | rchunk_size = total_written - total_read; |
| 529 | } |
| 530 | |
| 531 | // Read the chunk |
| 532 | uint32_t chunk_read = 0; |
| 533 | while (chunk_read < rchunk_size) { |
| 534 | uint32_t read_size = rSizeGenerator.nextSize(); |
| 535 | if (read_size == 0 || read_size > rchunk_size - chunk_read) { |
| 536 | read_size = rchunk_size - chunk_read; |
| 537 | } |
| 538 | |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 539 | int bytes_read = -1; |
| 540 | try { |
| 541 | bytes_read = transports.in->read(rbuf.get() + total_read, read_size); |
| 542 | } catch (TTransportException& e) { |
| 543 | BOOST_FAIL("read(pos=" << total_read << ", size=" << read_size << |
| 544 | ") threw exception \"" << e.what() << |
| 545 | "\"; written so far: " << total_written << " / " << |
| 546 | totalSize << " bytes"); |
| 547 | } |
| 548 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 549 | BOOST_REQUIRE_MESSAGE(bytes_read > 0, |
| 550 | "read(pos=" << total_read << ", size=" << |
| 551 | read_size << ") returned " << bytes_read << |
| 552 | "; written so far: " << total_written << " / " << |
| 553 | totalSize << " bytes"); |
| 554 | chunk_read += bytes_read; |
| 555 | total_read += bytes_read; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | // make sure the data read back is identical to the data written |
| 560 | BOOST_CHECK_EQUAL(memcmp(rbuf.get(), wbuf.get(), totalSize), 0); |
| 561 | } |
| 562 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 563 | template <class CoupledTransports> |
| 564 | void test_read_part_available() { |
| 565 | CoupledTransports transports; |
| 566 | BOOST_REQUIRE(transports.in != NULL); |
| 567 | BOOST_REQUIRE(transports.out != NULL); |
| 568 | |
| 569 | uint8_t write_buf[16]; |
| 570 | uint8_t read_buf[16]; |
| 571 | memset(write_buf, 'a', sizeof(write_buf)); |
| 572 | |
| 573 | // Attemping to read 10 bytes when only 9 are available should return 9 |
| 574 | // immediately. |
| 575 | transports.out->write(write_buf, 9); |
| 576 | transports.out->flush(); |
| 577 | set_trigger(3, transports.out, 1); |
| 578 | uint32_t bytes_read = transports.in->read(read_buf, 10); |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 579 | BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0); |
| 580 | BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 9); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 581 | |
| 582 | clear_triggers(); |
| 583 | } |
| 584 | |
| 585 | template <class CoupledTransports> |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 586 | void test_read_partial_midframe() { |
| 587 | CoupledTransports transports; |
| 588 | BOOST_REQUIRE(transports.in != NULL); |
| 589 | BOOST_REQUIRE(transports.out != NULL); |
| 590 | |
| 591 | uint8_t write_buf[16]; |
| 592 | uint8_t read_buf[16]; |
| 593 | memset(write_buf, 'a', sizeof(write_buf)); |
| 594 | |
| 595 | // Attempt to read 10 bytes, when only 9 are available, but after we have |
| 596 | // already read part of the data that is available. This exercises a |
| 597 | // different code path for several of the transports. |
| 598 | // |
| 599 | // For transports that add their own framing (e.g., TFramedTransport and |
| 600 | // TFileTransport), the two flush calls break up the data in to a 10 byte |
| 601 | // frame and a 3 byte frame. The first read then puts us partway through the |
| 602 | // first frame, and then we attempt to read past the end of that frame, and |
| 603 | // through the next frame, too. |
| 604 | // |
| 605 | // For buffered transports that perform read-ahead (e.g., |
| 606 | // TBufferedTransport), the read-ahead will most likely see all 13 bytes |
| 607 | // written on the first read. The next read will then attempt to read past |
| 608 | // the end of the read-ahead buffer. |
| 609 | // |
| 610 | // Flush 10 bytes, then 3 bytes. This creates 2 separate frames for |
| 611 | // transports that track framing internally. |
| 612 | transports.out->write(write_buf, 10); |
| 613 | transports.out->flush(); |
| 614 | transports.out->write(write_buf, 3); |
| 615 | transports.out->flush(); |
| 616 | |
| 617 | // Now read 4 bytes, so that we are partway through the written data. |
| 618 | uint32_t bytes_read = transports.in->read(read_buf, 4); |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 619 | BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 4); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 620 | |
| 621 | // Now attempt to read 10 bytes. Only 9 more are available. |
| 622 | // |
| 623 | // We should be able to get all 9 bytes, but it might take multiple read |
| 624 | // calls, since it is valid for read() to return fewer bytes than requested. |
| 625 | // (Most transports do immediately return 9 bytes, but the framing transports |
| 626 | // tend to only return to the end of the current frame, which is 6 bytes in |
| 627 | // this case.) |
| 628 | uint32_t total_read = 0; |
| 629 | while (total_read < 9) { |
| 630 | set_trigger(3, transports.out, 1); |
| 631 | bytes_read = transports.in->read(read_buf, 10); |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 632 | BOOST_REQUIRE_EQUAL(numTriggersFired, (unsigned int) 0); |
| 633 | BOOST_REQUIRE_GT(bytes_read, (uint32_t) 0); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 634 | total_read += bytes_read; |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 635 | BOOST_REQUIRE_LE(total_read, (uint32_t) 9); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 638 | BOOST_CHECK_EQUAL(total_read, (uint32_t) 9); |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 639 | |
| 640 | clear_triggers(); |
| 641 | } |
| 642 | |
| 643 | template <class CoupledTransports> |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 644 | void test_borrow_part_available() { |
| 645 | CoupledTransports transports; |
| 646 | BOOST_REQUIRE(transports.in != NULL); |
| 647 | BOOST_REQUIRE(transports.out != NULL); |
| 648 | |
| 649 | uint8_t write_buf[16]; |
| 650 | uint8_t read_buf[16]; |
| 651 | memset(write_buf, 'a', sizeof(write_buf)); |
| 652 | |
| 653 | // Attemping to borrow 10 bytes when only 9 are available should return NULL |
| 654 | // immediately. |
| 655 | transports.out->write(write_buf, 9); |
| 656 | transports.out->flush(); |
| 657 | set_trigger(3, transports.out, 1); |
| 658 | uint32_t borrow_len = 10; |
| 659 | const uint8_t* borrowed_buf = transports.in->borrow(read_buf, &borrow_len); |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 660 | BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 661 | BOOST_CHECK(borrowed_buf == NULL); |
| 662 | |
| 663 | clear_triggers(); |
| 664 | } |
| 665 | |
| 666 | template <class CoupledTransports> |
| 667 | void test_read_none_available() { |
| 668 | CoupledTransports transports; |
| 669 | BOOST_REQUIRE(transports.in != NULL); |
| 670 | BOOST_REQUIRE(transports.out != NULL); |
| 671 | |
| 672 | uint8_t write_buf[16]; |
| 673 | uint8_t read_buf[16]; |
| 674 | memset(write_buf, 'a', sizeof(write_buf)); |
| 675 | |
| 676 | // Attempting to read when no data is available should either block until |
| 677 | // some data is available, or fail immediately. (e.g., TSocket blocks, |
| 678 | // TMemoryBuffer just fails.) |
| 679 | // |
| 680 | // If the transport blocks, it should succeed once some data is available, |
| 681 | // even if less than the amount requested becomes available. |
| 682 | set_trigger(1, transports.out, 2); |
| 683 | add_trigger(1, transports.out, 8); |
| 684 | uint32_t bytes_read = transports.in->read(read_buf, 10); |
| 685 | if (bytes_read == 0) { |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 686 | BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 687 | clear_triggers(); |
| 688 | } else { |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 689 | BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 1); |
| 690 | BOOST_CHECK_EQUAL(bytes_read, (uint32_t) 2); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | clear_triggers(); |
| 694 | } |
| 695 | |
| 696 | template <class CoupledTransports> |
| 697 | void test_borrow_none_available() { |
| 698 | CoupledTransports transports; |
| 699 | BOOST_REQUIRE(transports.in != NULL); |
| 700 | BOOST_REQUIRE(transports.out != NULL); |
| 701 | |
| 702 | uint8_t write_buf[16]; |
| 703 | memset(write_buf, 'a', sizeof(write_buf)); |
| 704 | |
| 705 | // Attempting to borrow when no data is available should fail immediately |
| 706 | set_trigger(1, transports.out, 10); |
| 707 | uint32_t borrow_len = 10; |
| 708 | const uint8_t* borrowed_buf = transports.in->borrow(NULL, &borrow_len); |
| 709 | BOOST_CHECK(borrowed_buf == NULL); |
Christian Lavoie | 01c5ceb | 2010-11-04 20:35:15 +0000 | [diff] [blame] | 710 | BOOST_CHECK_EQUAL(numTriggersFired, (unsigned int) 0); |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 711 | |
| 712 | clear_triggers(); |
| 713 | } |
| 714 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 715 | /************************************************************************** |
| 716 | * Test case generation |
| 717 | * |
| 718 | * Pretty ugly and annoying. This would be much easier if we the unit test |
| 719 | * framework didn't force each test to be a separate function. |
| 720 | * - Writing a completely separate function definition for each of these would |
| 721 | * result in a lot of repetitive boilerplate code. |
| 722 | * - Combining many tests into a single function makes it more difficult to |
| 723 | * tell precisely which tests failed. It also means you can't get a progress |
| 724 | * update after each test, and the tests are already fairly slow. |
| 725 | * - Similar registration could be acheived with BOOST_TEST_CASE_TEMPLATE, |
| 726 | * but it requires a lot of awkward MPL code, and results in useless test |
| 727 | * case names. (The names are generated from std::type_info::name(), which |
| 728 | * is compiler-dependent. gcc returns mangled names.) |
| 729 | **************************************************************************/ |
| 730 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 731 | #define ADD_TEST_RW(CoupledTransports, totalSize, ...) \ |
| 732 | addTestRW< CoupledTransports >(BOOST_STRINGIZE(CoupledTransports), \ |
| 733 | totalSize, ## __VA_ARGS__); |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 734 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 735 | #define TEST_RW(CoupledTransports, totalSize, ...) \ |
| 736 | do { \ |
| 737 | /* Add the test as specified, to test the non-virtual function calls */ \ |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 738 | ADD_TEST_RW(CoupledTransports, totalSize, ## __VA_ARGS__); \ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 739 | /* \ |
| 740 | * Also test using the transport as a TTransport*, to test \ |
| 741 | * the read_virt()/write_virt() calls \ |
| 742 | */ \ |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 743 | ADD_TEST_RW(CoupledTTransports<CoupledTransports>, \ |
| 744 | totalSize, ## __VA_ARGS__); \ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 745 | /* Test wrapping the transport with TBufferedTransport */ \ |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 746 | ADD_TEST_RW(CoupledBufferedTransportsT<CoupledTransports>, \ |
| 747 | totalSize, ## __VA_ARGS__); \ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 748 | /* Test wrapping the transport with TFramedTransports */ \ |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 749 | ADD_TEST_RW(CoupledFramedTransportsT<CoupledTransports>, \ |
| 750 | totalSize, ## __VA_ARGS__); \ |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 751 | /* Test wrapping the transport with TZlibTransport */ \ |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 752 | ADD_TEST_RW(CoupledZlibTransportsT<CoupledTransports>, \ |
| 753 | totalSize, ## __VA_ARGS__); \ |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 754 | } while (0) |
| 755 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 756 | #define ADD_TEST_BLOCKING(CoupledTransports) \ |
| 757 | addTestBlocking< CoupledTransports >(BOOST_STRINGIZE(CoupledTransports)); |
| 758 | |
| 759 | #define TEST_BLOCKING_BEHAVIOR(CoupledTransports) \ |
| 760 | ADD_TEST_BLOCKING(CoupledTransports); \ |
| 761 | ADD_TEST_BLOCKING(CoupledTTransports<CoupledTransports>); \ |
| 762 | ADD_TEST_BLOCKING(CoupledBufferedTransportsT<CoupledTransports>); \ |
| 763 | ADD_TEST_BLOCKING(CoupledFramedTransportsT<CoupledTransports>); \ |
| 764 | ADD_TEST_BLOCKING(CoupledZlibTransportsT<CoupledTransports>); |
| 765 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 766 | class TransportTestGen { |
| 767 | public: |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 768 | TransportTestGen(boost::unit_test::test_suite* suite, |
| 769 | float sizeMultiplier) : |
| 770 | suite_(suite), |
| 771 | sizeMultiplier_(sizeMultiplier) {} |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 772 | |
| 773 | void generate() { |
| 774 | GenericSizeGenerator rand4k(1, 4096); |
| 775 | |
| 776 | /* |
| 777 | * We do the basically the same set of tests for each transport type, |
| 778 | * although we tweak the parameters in some places. |
| 779 | */ |
| 780 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 781 | // TMemoryBuffer tests |
| 782 | TEST_RW(CoupledMemoryBuffers, 1024*1024, 0, 0); |
| 783 | TEST_RW(CoupledMemoryBuffers, 1024*256, rand4k, rand4k); |
| 784 | TEST_RW(CoupledMemoryBuffers, 1024*256, 167, 163); |
| 785 | TEST_RW(CoupledMemoryBuffers, 1024*16, 1, 1); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 786 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 787 | TEST_RW(CoupledMemoryBuffers, 1024*256, 0, 0, rand4k, rand4k); |
| 788 | TEST_RW(CoupledMemoryBuffers, 1024*256, rand4k, rand4k, rand4k, rand4k); |
| 789 | TEST_RW(CoupledMemoryBuffers, 1024*256, 167, 163, rand4k, rand4k); |
| 790 | TEST_RW(CoupledMemoryBuffers, 1024*16, 1, 1, rand4k, rand4k); |
David Reiss | e94fa33 | 2010-10-06 17:10:26 +0000 | [diff] [blame] | 791 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 792 | TEST_BLOCKING_BEHAVIOR(CoupledMemoryBuffers); |
| 793 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 794 | // TFDTransport tests |
| 795 | // Since CoupledFDTransports tests with a pipe, writes will block |
| 796 | // if there is too much outstanding unread data in the pipe. |
| 797 | uint32_t fd_max_outstanding = 4096; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 798 | TEST_RW(CoupledFDTransports, 1024*1024, 0, 0, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 799 | 0, 0, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 800 | TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 801 | 0, 0, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 802 | TEST_RW(CoupledFDTransports, 1024*256, 167, 163, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 803 | 0, 0, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 804 | TEST_RW(CoupledFDTransports, 1024*16, 1, 1, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 805 | 0, 0, fd_max_outstanding); |
| 806 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 807 | TEST_RW(CoupledFDTransports, 1024*256, 0, 0, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 808 | rand4k, rand4k, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 809 | TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 810 | rand4k, rand4k, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 811 | TEST_RW(CoupledFDTransports, 1024*256, 167, 163, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 812 | rand4k, rand4k, fd_max_outstanding); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 813 | TEST_RW(CoupledFDTransports, 1024*16, 1, 1, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 814 | rand4k, rand4k, fd_max_outstanding); |
| 815 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 816 | TEST_BLOCKING_BEHAVIOR(CoupledFDTransports); |
| 817 | |
David Reiss | 0c025e8 | 2010-10-06 17:10:36 +0000 | [diff] [blame] | 818 | // TSocket tests |
| 819 | uint32_t socket_max_outstanding = 4096; |
| 820 | TEST_RW(CoupledSocketTransports, 1024*1024, 0, 0, |
| 821 | 0, 0, socket_max_outstanding); |
| 822 | TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k, |
| 823 | 0, 0, socket_max_outstanding); |
| 824 | TEST_RW(CoupledSocketTransports, 1024*256, 167, 163, |
| 825 | 0, 0, socket_max_outstanding); |
| 826 | // Doh. Apparently writing to a socket has some additional overhead for |
| 827 | // each send() call. If we have more than ~400 outstanding 1-byte write |
| 828 | // requests, additional send() calls start blocking. |
| 829 | TEST_RW(CoupledSocketTransports, 1024*16, 1, 1, |
| 830 | 0, 0, 400); |
| 831 | TEST_RW(CoupledSocketTransports, 1024*256, 0, 0, |
| 832 | rand4k, rand4k, socket_max_outstanding); |
| 833 | TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k, |
| 834 | rand4k, rand4k, socket_max_outstanding); |
| 835 | TEST_RW(CoupledSocketTransports, 1024*256, 167, 163, |
| 836 | rand4k, rand4k, socket_max_outstanding); |
| 837 | TEST_RW(CoupledSocketTransports, 1024*16, 1, 1, |
| 838 | rand4k, rand4k, 400); |
| 839 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 840 | TEST_BLOCKING_BEHAVIOR(CoupledSocketTransports); |
| 841 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 842 | // TFileTransport tests |
| 843 | // We use smaller buffer sizes here, since TFileTransport is fairly slow. |
| 844 | // |
| 845 | // TFileTransport can't write more than 16MB at once |
| 846 | uint32_t max_write_at_once = 1024*1024*16 - 4; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 847 | TEST_RW(CoupledFileTransports, 1024*1024, max_write_at_once, 0); |
| 848 | TEST_RW(CoupledFileTransports, 1024*128, rand4k, rand4k); |
| 849 | TEST_RW(CoupledFileTransports, 1024*128, 167, 163); |
| 850 | TEST_RW(CoupledFileTransports, 1024*2, 1, 1); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 851 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 852 | TEST_RW(CoupledFileTransports, 1024*64, 0, 0, rand4k, rand4k); |
| 853 | TEST_RW(CoupledFileTransports, 1024*64, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 854 | rand4k, rand4k, rand4k, rand4k); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 855 | TEST_RW(CoupledFileTransports, 1024*64, 167, 163, rand4k, rand4k); |
| 856 | TEST_RW(CoupledFileTransports, 1024*2, 1, 1, rand4k, rand4k); |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 857 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 858 | TEST_BLOCKING_BEHAVIOR(CoupledFileTransports); |
| 859 | |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 860 | // Add some tests that access TBufferedTransport and TFramedTransport |
| 861 | // via TTransport pointers and TBufferBase pointers. |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 862 | ADD_TEST_RW(CoupledTTransports<CoupledBufferedTransports>, |
| 863 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
| 864 | ADD_TEST_RW(CoupledBufferBases<CoupledBufferedTransports>, |
| 865 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
| 866 | ADD_TEST_RW(CoupledTTransports<CoupledFramedTransports>, |
| 867 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
| 868 | ADD_TEST_RW(CoupledBufferBases<CoupledFramedTransports>, |
| 869 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
David Reiss | d4788df | 2010-10-06 17:10:37 +0000 | [diff] [blame] | 870 | |
| 871 | // Test using TZlibTransport via a TTransport pointer |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 872 | ADD_TEST_RW(CoupledTTransports<CoupledZlibTransports>, |
| 873 | 1024*1024, rand4k, rand4k, rand4k, rand4k); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | private: |
| 877 | template <class CoupledTransports> |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 878 | void addTestRW(const char* transport_name, uint32_t totalSize, |
| 879 | GenericSizeGenerator wSizeGen, GenericSizeGenerator rSizeGen, |
| 880 | GenericSizeGenerator wChunkSizeGen = 0, |
| 881 | GenericSizeGenerator rChunkSizeGen = 0, |
| 882 | uint32_t maxOutstanding = 0, |
| 883 | uint32_t expectedFailures = 0) { |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 884 | // adjust totalSize by the specified sizeMultiplier_ first |
| 885 | totalSize = static_cast<uint32_t>(totalSize * sizeMultiplier_); |
| 886 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 887 | std::ostringstream name; |
| 888 | name << transport_name << "::test_rw(" << totalSize << ", " << |
| 889 | wSizeGen.describe() << ", " << rSizeGen.describe() << ", " << |
| 890 | wChunkSizeGen.describe() << ", " << rChunkSizeGen.describe() << ", " << |
| 891 | maxOutstanding << ")"; |
| 892 | |
| 893 | boost::unit_test::callback0<> test_func = |
| 894 | std::tr1::bind(test_rw<CoupledTransports>, totalSize, |
| 895 | wSizeGen, rSizeGen, wChunkSizeGen, rChunkSizeGen, |
| 896 | maxOutstanding); |
| 897 | boost::unit_test::test_case* tc = |
| 898 | boost::unit_test::make_test_case(test_func, name.str()); |
| 899 | suite_->add(tc, expectedFailures); |
Roger Meier | 0069cc4 | 2010-10-13 18:10:18 +0000 | [diff] [blame] | 900 | } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 901 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 902 | template <class CoupledTransports> |
| 903 | void addTestBlocking(const char* transportName, |
| 904 | uint32_t expectedFailures = 0) { |
| 905 | char name[1024]; |
| 906 | boost::unit_test::test_case* tc; |
| 907 | |
| 908 | snprintf(name, sizeof(name), "%s::test_read_part_available()", |
| 909 | transportName); |
| 910 | tc = boost::unit_test::make_test_case( |
| 911 | test_read_part_available<CoupledTransports>, name); |
| 912 | suite_->add(tc, expectedFailures); |
| 913 | |
David Reiss | 0a2d81e | 2010-10-06 17:10:40 +0000 | [diff] [blame] | 914 | snprintf(name, sizeof(name), "%s::test_read_partial_midframe()", |
| 915 | transportName); |
| 916 | tc = boost::unit_test::make_test_case( |
| 917 | test_read_partial_midframe<CoupledTransports>, name); |
| 918 | suite_->add(tc, expectedFailures); |
| 919 | |
David Reiss | e5c435c | 2010-10-06 17:10:38 +0000 | [diff] [blame] | 920 | snprintf(name, sizeof(name), "%s::test_read_none_available()", |
| 921 | transportName); |
| 922 | tc = boost::unit_test::make_test_case( |
| 923 | test_read_none_available<CoupledTransports>, name); |
| 924 | suite_->add(tc, expectedFailures); |
| 925 | |
| 926 | snprintf(name, sizeof(name), "%s::test_borrow_part_available()", |
| 927 | transportName); |
| 928 | tc = boost::unit_test::make_test_case( |
| 929 | test_borrow_part_available<CoupledTransports>, name); |
| 930 | suite_->add(tc, expectedFailures); |
| 931 | |
| 932 | snprintf(name, sizeof(name), "%s::test_borrow_none_available()", |
| 933 | transportName); |
| 934 | tc = boost::unit_test::make_test_case( |
| 935 | test_borrow_none_available<CoupledTransports>, name); |
| 936 | suite_->add(tc, expectedFailures); |
| 937 | } |
| 938 | |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 939 | boost::unit_test::test_suite* suite_; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 940 | // sizeMultiplier_ is configurable via the command line, and allows the |
| 941 | // user to adjust between smaller buffers that can be tested quickly, |
| 942 | // or larger buffers that more thoroughly exercise the code, but take |
| 943 | // longer. |
| 944 | float sizeMultiplier_; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 945 | }; |
| 946 | |
| 947 | /************************************************************************** |
| 948 | * General Initialization |
| 949 | **************************************************************************/ |
| 950 | |
| 951 | void print_usage(FILE* f, const char* argv0) { |
| 952 | fprintf(f, "Usage: %s [boost_options] [options]\n", argv0); |
| 953 | fprintf(f, "Options:\n"); |
| 954 | fprintf(f, " --seed=<N>, -s <N>\n"); |
| 955 | fprintf(f, " --tmp-dir=DIR, -t DIR\n"); |
| 956 | fprintf(f, " --help\n"); |
| 957 | } |
| 958 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 959 | struct Options { |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 960 | int seed; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 961 | bool haveSeed; |
| 962 | float sizeMultiplier; |
| 963 | }; |
| 964 | |
| 965 | void parse_args(int argc, char* argv[], Options* options) { |
| 966 | bool have_seed = false; |
| 967 | options->sizeMultiplier = 1; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 968 | |
| 969 | struct option long_opts[] = { |
| 970 | { "help", false, NULL, 'h' }, |
| 971 | { "seed", true, NULL, 's' }, |
| 972 | { "tmp-dir", true, NULL, 't' }, |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 973 | { "size-multiplier", true, NULL, 'x' }, |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 974 | { NULL, 0, NULL, 0 } |
| 975 | }; |
| 976 | |
| 977 | while (true) { |
| 978 | optopt = 1; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 979 | int optchar = getopt_long(argc, argv, "hs:t:x:", long_opts, NULL); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 980 | if (optchar == -1) { |
| 981 | break; |
| 982 | } |
| 983 | |
| 984 | switch (optchar) { |
| 985 | case 't': |
| 986 | tmp_dir = optarg; |
| 987 | break; |
| 988 | case 's': { |
| 989 | char *endptr; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 990 | options->seed = strtol(optarg, &endptr, 0); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 991 | if (endptr == optarg || *endptr != '\0') { |
| 992 | fprintf(stderr, "invalid seed value \"%s\": must be an integer\n", |
| 993 | optarg); |
| 994 | exit(1); |
| 995 | } |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 996 | have_seed = true; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 997 | break; |
| 998 | } |
| 999 | case 'h': |
| 1000 | print_usage(stdout, argv[0]); |
| 1001 | exit(0); |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1002 | case 'x': { |
| 1003 | char *endptr; |
| 1004 | options->sizeMultiplier = strtof(optarg, &endptr); |
| 1005 | if (endptr == optarg || *endptr != '\0') { |
| 1006 | fprintf(stderr, "invalid size multiplier \"%s\": must be a number\n", |
| 1007 | optarg); |
| 1008 | exit(1); |
| 1009 | } |
| 1010 | if (options->sizeMultiplier < 0) { |
| 1011 | fprintf(stderr, "invalid size multiplier \"%s\": " |
| 1012 | "must be non-negative\n", optarg); |
| 1013 | exit(1); |
| 1014 | } |
| 1015 | break; |
| 1016 | } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1017 | case '?': |
| 1018 | exit(1); |
| 1019 | default: |
| 1020 | // Only happens if someone adds another option to the optarg string, |
| 1021 | // but doesn't update the switch statement to handle it. |
| 1022 | fprintf(stderr, "unknown option \"-%c\"\n", optchar); |
| 1023 | exit(1); |
| 1024 | } |
| 1025 | } |
| 1026 | |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1027 | if (!have_seed) { |
| 1028 | // choose a seed now if the user didn't specify one |
Christian Lavoie | 4f42ef7 | 2010-11-04 18:51:42 +0000 | [diff] [blame] | 1029 | struct timeval tv; |
| 1030 | struct timezone tz; |
| 1031 | gettimeofday(&tv, &tz); |
| 1032 | options->seed = tv.tv_sec ^ tv.tv_usec; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1033 | } |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) { |
| 1037 | // Parse arguments |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1038 | Options options; |
| 1039 | parse_args(argc, argv, &options); |
| 1040 | |
| 1041 | initrand(options.seed); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1042 | |
David Reiss | 109693c | 2010-10-06 17:10:42 +0000 | [diff] [blame] | 1043 | boost::unit_test::test_suite* suite = |
| 1044 | &boost::unit_test::framework::master_test_suite(); |
| 1045 | suite->p_name.value = "TransportTest"; |
David Reiss | 65e62d3 | 2010-10-06 17:10:35 +0000 | [diff] [blame] | 1046 | TransportTestGen transport_test_generator(suite, options.sizeMultiplier); |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1047 | transport_test_generator.generate(); |
| 1048 | |
David Reiss | 109693c | 2010-10-06 17:10:42 +0000 | [diff] [blame] | 1049 | return NULL; |
David Reiss | 35dc769 | 2010-10-06 17:10:19 +0000 | [diff] [blame] | 1050 | } |