blob: baaf2bd9bb1ad2e2d49ed26b37d62dfa9baf69b6 [file] [log] [blame]
David Reiss35dc7692010-10-06 17:10:19 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19#ifndef _GNU_SOURCE
20#define _GNU_SOURCE // needed for getopt_long
21#endif
22
23#include <stdlib.h>
24#include <time.h>
25#include <unistd.h>
26#include <getopt.h>
27#include <sstream>
28#include <tr1/functional>
29
30#include <boost/mpl/list.hpp>
31#include <boost/shared_array.hpp>
32#include <boost/random.hpp>
33#include <boost/type_traits.hpp>
34#include <boost/test/unit_test.hpp>
35
36#include <transport/TBufferTransports.h>
37#include <transport/TFDTransport.h>
38#include <transport/TFileTransport.h>
David Reisse94fa332010-10-06 17:10:26 +000039#include <transport/TZlibTransport.h>
David Reiss0c025e82010-10-06 17:10:36 +000040#include <transport/TSocket.h>
David Reiss35dc7692010-10-06 17:10:19 +000041
42using namespace apache::thrift::transport;
43
44static boost::mt19937 rng;
45static const char* tmp_dir = "/tmp";
46
David Reiss65e62d32010-10-06 17:10:35 +000047void initrand(unsigned int seed) {
David Reiss35dc7692010-10-06 17:10:19 +000048 rng.seed(seed);
49}
50
51class SizeGenerator {
52 public:
53 virtual ~SizeGenerator() {}
54 virtual uint32_t nextSize() = 0;
55 virtual std::string describe() const = 0;
56};
57
58class ConstantSizeGenerator : public SizeGenerator {
59 public:
60 ConstantSizeGenerator(uint32_t value) : value_(value) {}
61 uint32_t nextSize() { return value_; }
62 std::string describe() const {
63 std::ostringstream desc;
64 desc << value_;
65 return desc.str();
66 }
67
68 private:
69 uint32_t value_;
70};
71
72class RandomSizeGenerator : public SizeGenerator {
73 public:
74 RandomSizeGenerator(uint32_t min, uint32_t max) :
75 generator_(rng, boost::uniform_int<int>(min, max)) {}
76
77 uint32_t nextSize() { return generator_(); }
78
79 std::string describe() const {
80 std::ostringstream desc;
81 desc << "rand(" << getMin() << ", " << getMax() << ")";
82 return desc.str();
83 }
84
85 uint32_t getMin() const { return generator_.distribution().min(); }
86 uint32_t getMax() const { return generator_.distribution().max(); }
87
88 private:
89 boost::variate_generator< boost::mt19937&, boost::uniform_int<int> >
90 generator_;
91};
92
93/**
94 * This class exists solely to make the TEST_RW() macro easier to use.
95 * - it can be constructed implicitly from an integer
96 * - it can contain either a ConstantSizeGenerator or a RandomSizeGenerator
97 * (TEST_RW can't take a SizeGenerator pointer or reference, since it needs
98 * to make a copy of the generator to bind it to the test function.)
99 */
100class GenericSizeGenerator : public SizeGenerator {
101 public:
102 GenericSizeGenerator(uint32_t value) :
103 generator_(new ConstantSizeGenerator(value)) {}
104 GenericSizeGenerator(uint32_t min, uint32_t max) :
105 generator_(new RandomSizeGenerator(min, max)) {}
106
107 uint32_t nextSize() { return generator_->nextSize(); }
108 std::string describe() const { return generator_->describe(); }
109
110 private:
111 boost::shared_ptr<SizeGenerator> generator_;
112};
113
114/**************************************************************************
115 * Classes to set up coupled transports
116 **************************************************************************/
117
David Reiss0c025e82010-10-06 17:10:36 +0000118/**
119 * Helper class to represent a coupled pair of transports.
120 *
121 * Data written to the out transport can be read from the in transport.
122 *
123 * This is used as the base class for the various coupled transport
124 * implementations. It shouldn't be instantiated directly.
125 */
David Reiss35dc7692010-10-06 17:10:19 +0000126template <class Transport_>
127class CoupledTransports {
128 public:
129 typedef Transport_ TransportType;
130
131 CoupledTransports() : in(NULL), out(NULL) {}
132
133 Transport_* in;
134 Transport_* out;
135
136 private:
137 CoupledTransports(const CoupledTransports&);
138 CoupledTransports &operator=(const CoupledTransports&);
139};
140
David Reiss0c025e82010-10-06 17:10:36 +0000141/**
142 * Coupled TMemoryBuffers
143 */
David Reiss35dc7692010-10-06 17:10:19 +0000144class CoupledMemoryBuffers : public CoupledTransports<TMemoryBuffer> {
145 public:
146 CoupledMemoryBuffers() {
147 in = &buf;
148 out = &buf;
149 }
150
151 TMemoryBuffer buf;
152};
153
David Reiss0c025e82010-10-06 17:10:36 +0000154/**
155 * Coupled TBufferedTransports.
156 *
157 * Uses a TMemoryBuffer as the underlying transport.
158 */
David Reiss35dc7692010-10-06 17:10:19 +0000159class CoupledBufferedTransports :
160 public CoupledTransports<TBufferedTransport> {
161 public:
162 CoupledBufferedTransports() :
163 buf(new TMemoryBuffer) {
164 in = new TBufferedTransport(buf);
165 out = new TBufferedTransport(buf);
166 }
167
168 ~CoupledBufferedTransports() {
169 delete in;
170 delete out;
171 }
172
173 boost::shared_ptr<TMemoryBuffer> buf;
174};
175
David Reiss0c025e82010-10-06 17:10:36 +0000176/**
177 * Coupled TFramedTransports.
178 *
179 * Uses a TMemoryBuffer as the underlying transport.
180 */
David Reiss35dc7692010-10-06 17:10:19 +0000181class CoupledFramedTransports : public CoupledTransports<TFramedTransport> {
182 public:
183 CoupledFramedTransports() :
184 buf(new TMemoryBuffer) {
185 in = new TFramedTransport(buf);
186 out = new TFramedTransport(buf);
187 }
188
189 ~CoupledFramedTransports() {
190 delete in;
191 delete out;
192 }
193
194 boost::shared_ptr<TMemoryBuffer> buf;
195};
196
David Reiss0c025e82010-10-06 17:10:36 +0000197/**
198 * Coupled TZlibTransports.
199 */
David Reisse94fa332010-10-06 17:10:26 +0000200class CoupledZlibTransports : public CoupledTransports<TZlibTransport> {
201 public:
202 CoupledZlibTransports() :
203 buf(new TMemoryBuffer) {
David Reissa0e11592010-10-06 17:10:27 +0000204 in = new TZlibTransport(buf);
205 out = new TZlibTransport(buf);
David Reisse94fa332010-10-06 17:10:26 +0000206 }
207
208 ~CoupledZlibTransports() {
209 delete in;
210 delete out;
211 }
212
213 boost::shared_ptr<TMemoryBuffer> buf;
214};
215
David Reiss0c025e82010-10-06 17:10:36 +0000216/**
217 * Coupled TFDTransports.
218 */
David Reiss35dc7692010-10-06 17:10:19 +0000219class CoupledFDTransports : public CoupledTransports<TFDTransport> {
220 public:
221 CoupledFDTransports() {
222 int pipes[2];
223
224 if (pipe(pipes) != 0) {
225 return;
226 }
227
228 in = new TFDTransport(pipes[0], TFDTransport::CLOSE_ON_DESTROY);
229 out = new TFDTransport(pipes[1], TFDTransport::CLOSE_ON_DESTROY);
230 }
231
232 ~CoupledFDTransports() {
233 delete in;
234 delete out;
235 }
236};
237
David Reiss0c025e82010-10-06 17:10:36 +0000238/**
239 * Coupled TSockets
240 */
241class CoupledSocketTransports : public CoupledTransports<TSocket> {
242 public:
243 CoupledSocketTransports() {
244 int sockets[2];
245 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockets) != 0) {
246 return;
247 }
248
249 in = new TSocket(sockets[0]);
250 out = new TSocket(sockets[1]);
251 }
252};
253
254/**
255 * Coupled TFileTransports
256 */
David Reiss35dc7692010-10-06 17:10:19 +0000257class CoupledFileTransports : public CoupledTransports<TFileTransport> {
258 public:
259 CoupledFileTransports() {
260 // Create a temporary file to use
261 size_t filename_len = strlen(tmp_dir) + 32;
262 filename = new char[filename_len];
263 snprintf(filename, filename_len,
264 "%s/thrift.transport_test.XXXXXX", tmp_dir);
265 fd = mkstemp(filename);
266 if (fd < 0) {
267 return;
268 }
269
270 in = new TFileTransport(filename, true);
271 out = new TFileTransport(filename);
272 }
273
274 ~CoupledFileTransports() {
275 delete in;
276 delete out;
277
278 if (fd >= 0) {
279 close(fd);
280 unlink(filename);
281 }
282 delete[] filename;
283 }
284
285 char* filename;
286 int fd;
287};
288
David Reiss0c025e82010-10-06 17:10:36 +0000289/**
290 * Wrapper around another CoupledTransports implementation that exposes the
291 * transports as TTransport pointers.
292 *
293 * This is used since accessing a transport via a "TTransport*" exercises a
294 * different code path than using the base pointer class. As part of the
295 * template code changes, most transport methods are no longer virtual.
296 */
David Reiss35dc7692010-10-06 17:10:19 +0000297template <class CoupledTransports_>
298class CoupledTTransports : public CoupledTransports<TTransport> {
299 public:
300 CoupledTTransports() : transports() {
301 in = transports.in;
302 out = transports.out;
303 }
304
305 CoupledTransports_ transports;
306};
307
David Reiss0c025e82010-10-06 17:10:36 +0000308/**
309 * Wrapper around another CoupledTransports implementation that exposes the
310 * transports as TBufferBase pointers.
311 *
312 * This can only be instantiated with a transport type that is a subclass of
313 * TBufferBase.
314 */
David Reiss35dc7692010-10-06 17:10:19 +0000315template <class CoupledTransports_>
316class CoupledBufferBases : public CoupledTransports<TBufferBase> {
317 public:
318 CoupledBufferBases() : transports() {
319 in = transports.in;
320 out = transports.out;
321 }
322
323 CoupledTransports_ transports;
324};
325
David Reiss35dc7692010-10-06 17:10:19 +0000326/**************************************************************************
327 * Main testing function
328 **************************************************************************/
329
330/**
331 * Test interleaved write and read calls.
332 *
333 * Generates a buffer totalSize bytes long, then writes it to the transport,
334 * and verifies the written data can be read back correctly.
335 *
336 * Mode of operation:
337 * - call wChunkGenerator to figure out how large of a chunk to write
338 * - call wSizeGenerator to get the size for individual write() calls,
339 * and do this repeatedly until the entire chunk is written.
340 * - call rChunkGenerator to figure out how large of a chunk to read
341 * - call rSizeGenerator to get the size for individual read() calls,
342 * and do this repeatedly until the entire chunk is read.
343 * - repeat until the full buffer is written and read back,
344 * then compare the data read back against the original buffer
345 *
346 *
347 * - If any of the size generators return 0, this means to use the maximum
348 * possible size.
349 *
350 * - If maxOutstanding is non-zero, write chunk sizes will be chosen such that
351 * there are never more than maxOutstanding bytes waiting to be read back.
352 */
353template <class CoupledTransports>
354void test_rw(uint32_t totalSize,
355 SizeGenerator& wSizeGenerator,
356 SizeGenerator& rSizeGenerator,
357 SizeGenerator& wChunkGenerator,
358 SizeGenerator& rChunkGenerator,
359 uint32_t maxOutstanding) {
360 CoupledTransports transports;
361 BOOST_REQUIRE(transports.in != NULL);
362 BOOST_REQUIRE(transports.out != NULL);
363
364 boost::shared_array<uint8_t> wbuf =
365 boost::shared_array<uint8_t>(new uint8_t[totalSize]);
366 boost::shared_array<uint8_t> rbuf =
367 boost::shared_array<uint8_t>(new uint8_t[totalSize]);
368
369 // store some data in wbuf
370 for (uint32_t n = 0; n < totalSize; ++n) {
371 wbuf[n] = (n & 0xff);
372 }
373 // clear rbuf
374 memset(rbuf.get(), 0, totalSize);
375
376 uint32_t total_written = 0;
377 uint32_t total_read = 0;
378 while (total_read < totalSize) {
379 // Determine how large a chunk of data to write
380 uint32_t wchunk_size = wChunkGenerator.nextSize();
381 if (wchunk_size == 0 || wchunk_size > totalSize - total_written) {
382 wchunk_size = totalSize - total_written;
383 }
384
385 // Make sure (total_written - total_read) + wchunk_size
386 // is less than maxOutstanding
387 if (maxOutstanding > 0 &&
388 wchunk_size > maxOutstanding - (total_written - total_read)) {
389 wchunk_size = maxOutstanding - (total_written - total_read);
390 }
391
392 // Write the chunk
393 uint32_t chunk_written = 0;
394 while (chunk_written < wchunk_size) {
395 uint32_t write_size = wSizeGenerator.nextSize();
396 if (write_size == 0 || write_size > wchunk_size - chunk_written) {
397 write_size = wchunk_size - chunk_written;
398 }
399
400 transports.out->write(wbuf.get() + total_written, write_size);
401 chunk_written += write_size;
402 total_written += write_size;
403 }
404
405 // Flush the data, so it will be available in the read transport
406 // Don't flush if wchunk_size is 0. (This should only happen if
407 // total_written == totalSize already, and we're only reading now.)
408 if (wchunk_size > 0) {
409 transports.out->flush();
410 }
411
412 // Determine how large a chunk of data to read back
413 uint32_t rchunk_size = rChunkGenerator.nextSize();
414 if (rchunk_size == 0 || rchunk_size > total_written - total_read) {
415 rchunk_size = total_written - total_read;
416 }
417
418 // Read the chunk
419 uint32_t chunk_read = 0;
420 while (chunk_read < rchunk_size) {
421 uint32_t read_size = rSizeGenerator.nextSize();
422 if (read_size == 0 || read_size > rchunk_size - chunk_read) {
423 read_size = rchunk_size - chunk_read;
424 }
425
David Reisse94fa332010-10-06 17:10:26 +0000426 int bytes_read = -1;
427 try {
428 bytes_read = transports.in->read(rbuf.get() + total_read, read_size);
429 } catch (TTransportException& e) {
430 BOOST_FAIL("read(pos=" << total_read << ", size=" << read_size <<
431 ") threw exception \"" << e.what() <<
432 "\"; written so far: " << total_written << " / " <<
433 totalSize << " bytes");
434 }
435
David Reiss35dc7692010-10-06 17:10:19 +0000436 BOOST_REQUIRE_MESSAGE(bytes_read > 0,
437 "read(pos=" << total_read << ", size=" <<
438 read_size << ") returned " << bytes_read <<
439 "; written so far: " << total_written << " / " <<
440 totalSize << " bytes");
441 chunk_read += bytes_read;
442 total_read += bytes_read;
443 }
444 }
445
446 // make sure the data read back is identical to the data written
447 BOOST_CHECK_EQUAL(memcmp(rbuf.get(), wbuf.get(), totalSize), 0);
448}
449
450/**************************************************************************
451 * Test case generation
452 *
453 * Pretty ugly and annoying. This would be much easier if we the unit test
454 * framework didn't force each test to be a separate function.
455 * - Writing a completely separate function definition for each of these would
456 * result in a lot of repetitive boilerplate code.
457 * - Combining many tests into a single function makes it more difficult to
458 * tell precisely which tests failed. It also means you can't get a progress
459 * update after each test, and the tests are already fairly slow.
460 * - Similar registration could be acheived with BOOST_TEST_CASE_TEMPLATE,
461 * but it requires a lot of awkward MPL code, and results in useless test
462 * case names. (The names are generated from std::type_info::name(), which
463 * is compiler-dependent. gcc returns mangled names.)
464 **************************************************************************/
465
466#define TEST_RW(CoupledTransports, totalSize, ...) \
467 do { \
468 /* Add the test as specified, to test the non-virtual function calls */ \
469 addTest<CoupledTransports>(BOOST_STRINGIZE(CoupledTransports), \
470 totalSize, ## __VA_ARGS__); \
471 /* \
472 * Also test using the transport as a TTransport*, to test \
473 * the read_virt()/write_virt() calls \
474 */ \
475 addTest< CoupledTTransports<CoupledTransports> >( \
476 BOOST_STRINGIZE(CoupledTTransports<CoupledTransports>), \
477 totalSize, ## __VA_ARGS__); \
478 } while (0)
479
480#define TEST_RW_BUF(CoupledTransports, totalSize, ...) \
481 do { \
482 /* Add the standard tests */ \
483 TEST_RW(CoupledTransports, totalSize, ## __VA_ARGS__); \
484 /* Also test using the transport as a TBufferBase* */ \
485 addTest< CoupledBufferBases<CoupledTransports> >( \
486 BOOST_STRINGIZE(CoupledBufferBases<CoupledTransports>), \
487 totalSize, ## __VA_ARGS__); \
488 } while (0)
489
490// We use the same tests for all of the buffered transports
491// This is a helper macro so we don't have to copy-and-paste them.
492#define BUFFER_TESTS(CoupledTransports) \
David Reiss65e62d32010-10-06 17:10:35 +0000493 TEST_RW_BUF(CoupledTransports, 1024*1024, 0, 0); \
494 TEST_RW_BUF(CoupledTransports, 1024*256, rand4k, rand4k); \
495 TEST_RW_BUF(CoupledTransports, 1024*256, 167, 163); \
496 TEST_RW_BUF(CoupledTransports, 1024*16, 1, 1); \
David Reiss35dc7692010-10-06 17:10:19 +0000497 \
David Reiss65e62d32010-10-06 17:10:35 +0000498 TEST_RW_BUF(CoupledTransports, 1024*256, 0, 0, rand4k, rand4k); \
499 TEST_RW_BUF(CoupledTransports, 1024*256, \
David Reiss35dc7692010-10-06 17:10:19 +0000500 rand4k, rand4k, rand4k, rand4k); \
David Reiss65e62d32010-10-06 17:10:35 +0000501 TEST_RW_BUF(CoupledTransports, 1024*256, 167, 163, rand4k, rand4k); \
502 TEST_RW_BUF(CoupledTransports, 1024*16, 1, 1, rand4k, rand4k);
David Reiss35dc7692010-10-06 17:10:19 +0000503
504class TransportTestGen {
505 public:
David Reiss65e62d32010-10-06 17:10:35 +0000506 TransportTestGen(boost::unit_test::test_suite* suite,
507 float sizeMultiplier) :
508 suite_(suite),
509 sizeMultiplier_(sizeMultiplier) {}
David Reiss35dc7692010-10-06 17:10:19 +0000510
511 void generate() {
512 GenericSizeGenerator rand4k(1, 4096);
513
514 /*
515 * We do the basically the same set of tests for each transport type,
516 * although we tweak the parameters in some places.
517 */
518
519 // Buffered transport tests
520 BUFFER_TESTS(CoupledMemoryBuffers)
521 BUFFER_TESTS(CoupledBufferedTransports)
522 BUFFER_TESTS(CoupledFramedTransports)
523
David Reiss65e62d32010-10-06 17:10:35 +0000524 TEST_RW(CoupledZlibTransports, 1024*256, 0, 0);
525 TEST_RW(CoupledZlibTransports, 1024*256, rand4k, rand4k);
526 TEST_RW(CoupledZlibTransports, 1024*128, 167, 163);
527 TEST_RW(CoupledZlibTransports, 1024*2, 1, 1);
David Reisse94fa332010-10-06 17:10:26 +0000528
David Reiss65e62d32010-10-06 17:10:35 +0000529 TEST_RW(CoupledZlibTransports, 1024*256, 0, 0, rand4k, rand4k);
530 TEST_RW(CoupledZlibTransports, 1024*256,
David Reisse94fa332010-10-06 17:10:26 +0000531 rand4k, rand4k, rand4k, rand4k);
David Reiss65e62d32010-10-06 17:10:35 +0000532 TEST_RW(CoupledZlibTransports, 1024*128, 167, 163, rand4k, rand4k);
533 TEST_RW(CoupledZlibTransports, 1024*2, 1, 1, rand4k, rand4k);
David Reisse94fa332010-10-06 17:10:26 +0000534
David Reiss35dc7692010-10-06 17:10:19 +0000535 // TFDTransport tests
536 // Since CoupledFDTransports tests with a pipe, writes will block
537 // if there is too much outstanding unread data in the pipe.
538 uint32_t fd_max_outstanding = 4096;
David Reiss65e62d32010-10-06 17:10:35 +0000539 TEST_RW(CoupledFDTransports, 1024*1024, 0, 0,
David Reiss35dc7692010-10-06 17:10:19 +0000540 0, 0, fd_max_outstanding);
David Reiss65e62d32010-10-06 17:10:35 +0000541 TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k,
David Reiss35dc7692010-10-06 17:10:19 +0000542 0, 0, fd_max_outstanding);
David Reiss65e62d32010-10-06 17:10:35 +0000543 TEST_RW(CoupledFDTransports, 1024*256, 167, 163,
David Reiss35dc7692010-10-06 17:10:19 +0000544 0, 0, fd_max_outstanding);
David Reiss65e62d32010-10-06 17:10:35 +0000545 TEST_RW(CoupledFDTransports, 1024*16, 1, 1,
David Reiss35dc7692010-10-06 17:10:19 +0000546 0, 0, fd_max_outstanding);
547
David Reiss65e62d32010-10-06 17:10:35 +0000548 TEST_RW(CoupledFDTransports, 1024*256, 0, 0,
David Reiss35dc7692010-10-06 17:10:19 +0000549 rand4k, rand4k, fd_max_outstanding);
David Reiss65e62d32010-10-06 17:10:35 +0000550 TEST_RW(CoupledFDTransports, 1024*256, rand4k, rand4k,
David Reiss35dc7692010-10-06 17:10:19 +0000551 rand4k, rand4k, fd_max_outstanding);
David Reiss65e62d32010-10-06 17:10:35 +0000552 TEST_RW(CoupledFDTransports, 1024*256, 167, 163,
David Reiss35dc7692010-10-06 17:10:19 +0000553 rand4k, rand4k, fd_max_outstanding);
David Reiss65e62d32010-10-06 17:10:35 +0000554 TEST_RW(CoupledFDTransports, 1024*16, 1, 1,
David Reiss35dc7692010-10-06 17:10:19 +0000555 rand4k, rand4k, fd_max_outstanding);
556
David Reiss0c025e82010-10-06 17:10:36 +0000557 // TSocket tests
558 uint32_t socket_max_outstanding = 4096;
559 TEST_RW(CoupledSocketTransports, 1024*1024, 0, 0,
560 0, 0, socket_max_outstanding);
561 TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k,
562 0, 0, socket_max_outstanding);
563 TEST_RW(CoupledSocketTransports, 1024*256, 167, 163,
564 0, 0, socket_max_outstanding);
565 // Doh. Apparently writing to a socket has some additional overhead for
566 // each send() call. If we have more than ~400 outstanding 1-byte write
567 // requests, additional send() calls start blocking.
568 TEST_RW(CoupledSocketTransports, 1024*16, 1, 1,
569 0, 0, 400);
570 TEST_RW(CoupledSocketTransports, 1024*256, 0, 0,
571 rand4k, rand4k, socket_max_outstanding);
572 TEST_RW(CoupledSocketTransports, 1024*256, rand4k, rand4k,
573 rand4k, rand4k, socket_max_outstanding);
574 TEST_RW(CoupledSocketTransports, 1024*256, 167, 163,
575 rand4k, rand4k, socket_max_outstanding);
576 TEST_RW(CoupledSocketTransports, 1024*16, 1, 1,
577 rand4k, rand4k, 400);
578
David Reiss35dc7692010-10-06 17:10:19 +0000579 // TFileTransport tests
580 // We use smaller buffer sizes here, since TFileTransport is fairly slow.
581 //
582 // TFileTransport can't write more than 16MB at once
583 uint32_t max_write_at_once = 1024*1024*16 - 4;
David Reiss65e62d32010-10-06 17:10:35 +0000584 TEST_RW(CoupledFileTransports, 1024*1024, max_write_at_once, 0);
585 TEST_RW(CoupledFileTransports, 1024*128, rand4k, rand4k);
586 TEST_RW(CoupledFileTransports, 1024*128, 167, 163);
587 TEST_RW(CoupledFileTransports, 1024*2, 1, 1);
David Reiss35dc7692010-10-06 17:10:19 +0000588
David Reiss65e62d32010-10-06 17:10:35 +0000589 TEST_RW(CoupledFileTransports, 1024*64, 0, 0, rand4k, rand4k);
590 TEST_RW(CoupledFileTransports, 1024*64,
David Reiss35dc7692010-10-06 17:10:19 +0000591 rand4k, rand4k, rand4k, rand4k);
David Reiss65e62d32010-10-06 17:10:35 +0000592 TEST_RW(CoupledFileTransports, 1024*64, 167, 163, rand4k, rand4k);
593 TEST_RW(CoupledFileTransports, 1024*2, 1, 1, rand4k, rand4k);
David Reiss35dc7692010-10-06 17:10:19 +0000594 }
595
596 private:
597 template <class CoupledTransports>
598 void addTest(const char* transport_name, uint32_t totalSize,
599 GenericSizeGenerator wSizeGen, GenericSizeGenerator rSizeGen,
600 GenericSizeGenerator wChunkSizeGen = 0,
601 GenericSizeGenerator rChunkSizeGen = 0,
602 uint32_t maxOutstanding = 0,
603 uint32_t expectedFailures = 0) {
David Reiss65e62d32010-10-06 17:10:35 +0000604 // adjust totalSize by the specified sizeMultiplier_ first
605 totalSize = static_cast<uint32_t>(totalSize * sizeMultiplier_);
606
David Reiss35dc7692010-10-06 17:10:19 +0000607 std::ostringstream name;
608 name << transport_name << "::test_rw(" << totalSize << ", " <<
609 wSizeGen.describe() << ", " << rSizeGen.describe() << ", " <<
610 wChunkSizeGen.describe() << ", " << rChunkSizeGen.describe() << ", " <<
611 maxOutstanding << ")";
612
613 boost::unit_test::callback0<> test_func =
614 std::tr1::bind(test_rw<CoupledTransports>, totalSize,
615 wSizeGen, rSizeGen, wChunkSizeGen, rChunkSizeGen,
616 maxOutstanding);
617 boost::unit_test::test_case* tc =
618 boost::unit_test::make_test_case(test_func, name.str());
619 suite_->add(tc, expectedFailures);
620 };
621
622 boost::unit_test::test_suite* suite_;
David Reiss65e62d32010-10-06 17:10:35 +0000623 // sizeMultiplier_ is configurable via the command line, and allows the
624 // user to adjust between smaller buffers that can be tested quickly,
625 // or larger buffers that more thoroughly exercise the code, but take
626 // longer.
627 float sizeMultiplier_;
David Reiss35dc7692010-10-06 17:10:19 +0000628};
629
630/**************************************************************************
631 * General Initialization
632 **************************************************************************/
633
634void print_usage(FILE* f, const char* argv0) {
635 fprintf(f, "Usage: %s [boost_options] [options]\n", argv0);
636 fprintf(f, "Options:\n");
637 fprintf(f, " --seed=<N>, -s <N>\n");
638 fprintf(f, " --tmp-dir=DIR, -t DIR\n");
639 fprintf(f, " --help\n");
640}
641
David Reiss65e62d32010-10-06 17:10:35 +0000642struct Options {
David Reiss35dc7692010-10-06 17:10:19 +0000643 int seed;
David Reiss65e62d32010-10-06 17:10:35 +0000644 bool haveSeed;
645 float sizeMultiplier;
646};
647
648void parse_args(int argc, char* argv[], Options* options) {
649 bool have_seed = false;
650 options->sizeMultiplier = 1;
David Reiss35dc7692010-10-06 17:10:19 +0000651
652 struct option long_opts[] = {
653 { "help", false, NULL, 'h' },
654 { "seed", true, NULL, 's' },
655 { "tmp-dir", true, NULL, 't' },
David Reiss65e62d32010-10-06 17:10:35 +0000656 { "size-multiplier", true, NULL, 'x' },
David Reiss35dc7692010-10-06 17:10:19 +0000657 { NULL, 0, NULL, 0 }
658 };
659
660 while (true) {
661 optopt = 1;
David Reiss65e62d32010-10-06 17:10:35 +0000662 int optchar = getopt_long(argc, argv, "hs:t:x:", long_opts, NULL);
David Reiss35dc7692010-10-06 17:10:19 +0000663 if (optchar == -1) {
664 break;
665 }
666
667 switch (optchar) {
668 case 't':
669 tmp_dir = optarg;
670 break;
671 case 's': {
672 char *endptr;
David Reiss65e62d32010-10-06 17:10:35 +0000673 options->seed = strtol(optarg, &endptr, 0);
David Reiss35dc7692010-10-06 17:10:19 +0000674 if (endptr == optarg || *endptr != '\0') {
675 fprintf(stderr, "invalid seed value \"%s\": must be an integer\n",
676 optarg);
677 exit(1);
678 }
David Reiss65e62d32010-10-06 17:10:35 +0000679 have_seed = true;
David Reiss35dc7692010-10-06 17:10:19 +0000680 break;
681 }
682 case 'h':
683 print_usage(stdout, argv[0]);
684 exit(0);
David Reiss65e62d32010-10-06 17:10:35 +0000685 case 'x': {
686 char *endptr;
687 options->sizeMultiplier = strtof(optarg, &endptr);
688 if (endptr == optarg || *endptr != '\0') {
689 fprintf(stderr, "invalid size multiplier \"%s\": must be a number\n",
690 optarg);
691 exit(1);
692 }
693 if (options->sizeMultiplier < 0) {
694 fprintf(stderr, "invalid size multiplier \"%s\": "
695 "must be non-negative\n", optarg);
696 exit(1);
697 }
698 break;
699 }
David Reiss35dc7692010-10-06 17:10:19 +0000700 case '?':
701 exit(1);
702 default:
703 // Only happens if someone adds another option to the optarg string,
704 // but doesn't update the switch statement to handle it.
705 fprintf(stderr, "unknown option \"-%c\"\n", optchar);
706 exit(1);
707 }
708 }
709
David Reiss65e62d32010-10-06 17:10:35 +0000710 if (!have_seed) {
711 // choose a seed now if the user didn't specify one
712 struct timespec t;
713 clock_gettime(CLOCK_REALTIME, &t);
714 options->seed = t.tv_sec + t.tv_nsec;
715 }
David Reiss35dc7692010-10-06 17:10:19 +0000716}
717
718boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
719 // Parse arguments
David Reiss65e62d32010-10-06 17:10:35 +0000720 Options options;
721 parse_args(argc, argv, &options);
722
723 initrand(options.seed);
David Reiss35dc7692010-10-06 17:10:19 +0000724
725 boost::unit_test::test_suite* suite = BOOST_TEST_SUITE("TransportTests");
David Reiss65e62d32010-10-06 17:10:35 +0000726 TransportTestGen transport_test_generator(suite, options.sizeMultiplier);
David Reiss35dc7692010-10-06 17:10:19 +0000727 transport_test_generator.generate();
728
729 return suite;
730}