blob: 8551b78d053c829f2c09b4191b890330de72c677 [file] [log] [blame]
David Reiss709b69f2010-10-06 17:10:30 +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
Roger Meierba406d32013-07-15 22:41:34 +020023#include <thrift/thrift-config.h>
24
Roger Meier2fa9c312011-09-05 19:15:53 +000025#ifdef HAVE_SYS_TIME_H
David Reiss709b69f2010-10-06 17:10:30 +000026#include <sys/time.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000027#endif
David Reiss709b69f2010-10-06 17:10:30 +000028#include <getopt.h>
29#include <boost/test/unit_test.hpp>
30
Roger Meier49ff8b12012-04-13 09:12:31 +000031#include <thrift/transport/TFileTransport.h>
David Reiss709b69f2010-10-06 17:10:30 +000032
Antonio Di Monaco796667b2016-01-04 23:05:19 +010033#ifdef __MINGW32__
34 #include <io.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <fcntl.h>
38 #include <sys\stat.h>
39#endif
40
David Reiss709b69f2010-10-06 17:10:30 +000041using namespace apache::thrift::transport;
42
43/**************************************************************************
44 * Global state
45 **************************************************************************/
46
47static const char* tmp_dir = "/tmp";
48
49class FsyncLog;
50FsyncLog* fsync_log;
51
David Reiss709b69f2010-10-06 17:10:30 +000052/**************************************************************************
53 * Helper code
54 **************************************************************************/
55
Roger Meier09cc5e72014-01-15 10:13:18 +010056// Provide BOOST_WARN_LT() and BOOST_WARN_GT(), in case we're compiled
David Reiss709b69f2010-10-06 17:10:30 +000057// with an older version of boost
Roger Meier09cc5e72014-01-15 10:13:18 +010058#ifndef BOOST_WARN_LT
Konrad Grochowski16a23a62014-11-13 15:33:38 +010059#define BOOST_WARN_CMP(a, b, op, check_fn) \
60 check_fn((a)op(b), \
61 "check " BOOST_STRINGIZE(a) " " BOOST_STRINGIZE(op) " " BOOST_STRINGIZE( \
62 b) " failed: " BOOST_STRINGIZE(a) "=" \
63 << (a) << " " BOOST_STRINGIZE(b) "=" << (b))
David Reiss709b69f2010-10-06 17:10:30 +000064
Roger Meier09cc5e72014-01-15 10:13:18 +010065#define BOOST_WARN_LT(a, b) BOOST_WARN_CMP(a, b, <, BOOST_WARN_MESSAGE)
66#define BOOST_WARN_GT(a, b) BOOST_WARN_CMP(a, b, >, BOOST_WARN_MESSAGE)
67#define BOOST_WARN_LT(a, b) BOOST_WARN_CMP(a, b, <, BOOST_WARN_MESSAGE)
68#endif // BOOST_WARN_LT
David Reiss709b69f2010-10-06 17:10:30 +000069
70/**
71 * Class to record calls to fsync
72 */
73class FsyncLog {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010074public:
David Reiss709b69f2010-10-06 17:10:30 +000075 struct FsyncCall {
76 struct timeval time;
77 int fd;
78 };
79 typedef std::list<FsyncCall> CallList;
80
81 FsyncLog() {}
82
83 void fsync(int fd) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +010084 (void)fd;
David Reiss709b69f2010-10-06 17:10:30 +000085 FsyncCall call;
86 gettimeofday(&call.time, NULL);
87 calls_.push_back(call);
88 }
89
Konrad Grochowski16a23a62014-11-13 15:33:38 +010090 const CallList* getCalls() const { return &calls_; }
David Reiss709b69f2010-10-06 17:10:30 +000091
Konrad Grochowski16a23a62014-11-13 15:33:38 +010092private:
David Reiss709b69f2010-10-06 17:10:30 +000093 CallList calls_;
94};
95
96/**
97 * Helper class to clean up temporary files
98 */
99class TempFile {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100100public:
David Reiss709b69f2010-10-06 17:10:30 +0000101 TempFile(const char* directory, const char* prefix) {
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100102 #ifdef __MINGW32__
103 size_t path_len = strlen(prefix) + 8;
104 path_ = new char[path_len];
105 snprintf(path_, path_len, "%sXXXXXX", prefix);
106 if (_mktemp_s(path_,path_len) == 0) {
107 fd_ = open(path_,O_CREAT | O_RDWR | O_BINARY,S_IREAD | S_IWRITE);
108 if (fd_ < 0) {
109 throw apache::thrift::TException("_mktemp_s() failed");
110 }
111 } else {
112 throw apache::thrift::TException("_mktemp_s() failed");
113 }
114 #else
David Reiss709b69f2010-10-06 17:10:30 +0000115 size_t path_len = strlen(directory) + strlen(prefix) + 8;
116 path_ = new char[path_len];
117 snprintf(path_, path_len, "%s/%sXXXXXX", directory, prefix);
118
119 fd_ = mkstemp(path_);
120 if (fd_ < 0) {
121 throw apache::thrift::TException("mkstemp() failed");
122 }
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100123 #endif
David Reiss709b69f2010-10-06 17:10:30 +0000124 }
125
126 ~TempFile() {
127 unlink();
128 close();
129 }
130
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100131 const char* getPath() const { return path_; }
David Reiss709b69f2010-10-06 17:10:30 +0000132
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100133 int getFD() const { return fd_; }
David Reiss709b69f2010-10-06 17:10:30 +0000134
135 void unlink() {
136 if (path_) {
137 ::unlink(path_);
138 delete[] path_;
139 path_ = NULL;
140 }
141 }
142
143 void close() {
144 if (fd_ < 0) {
145 return;
146 }
147
148 ::close(fd_);
149 fd_ = -1;
150 }
151
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100152private:
David Reiss709b69f2010-10-06 17:10:30 +0000153 char* path_;
154 int fd_;
155};
156
157// Use our own version of fsync() for testing.
158// This returns immediately, so timing in test_destructor() isn't affected by
159// waiting on the actual filesystem.
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100160extern "C" int fsync(int fd) {
David Reiss709b69f2010-10-06 17:10:30 +0000161 if (fsync_log) {
162 fsync_log->fsync(fd);
163 }
164 return 0;
165}
166
David Reiss709b69f2010-10-06 17:10:30 +0000167int time_diff(const struct timeval* t1, const struct timeval* t2) {
168 return (t2->tv_usec - t1->tv_usec) + (t2->tv_sec - t1->tv_sec) * 1000000;
169}
170
David Reiss709b69f2010-10-06 17:10:30 +0000171/**************************************************************************
172 * Test cases
173 **************************************************************************/
174
175/**
176 * Make sure the TFileTransport destructor exits "quickly".
177 *
178 * Previous versions had a bug causing the writer thread not to exit
179 * right away.
180 *
181 * It's kind of lame that we just check to see how long the destructor takes in
182 * wall-clock time. This could result in false failures on slower systems, or
183 * on heavily loaded machines.
184 */
David Reiss109693c2010-10-06 17:10:42 +0000185BOOST_AUTO_TEST_CASE(test_destructor) {
David Reiss709b69f2010-10-06 17:10:30 +0000186 TempFile f(tmp_dir, "thrift.TFileTransportTest.");
187
188 unsigned int const NUM_ITERATIONS = 1000;
David Reiss709b69f2010-10-06 17:10:30 +0000189
Roger Meier085a3e72010-10-08 21:23:35 +0000190 unsigned int num_over = 0;
David Reiss709b69f2010-10-06 17:10:30 +0000191 for (unsigned int n = 0; n < NUM_ITERATIONS; ++n) {
192 ftruncate(f.getFD(), 0);
193
194 TFileTransport* transport = new TFileTransport(f.getPath());
195
196 // write something so that the writer thread gets started
197 transport->write(reinterpret_cast<const uint8_t*>("foo"), 3);
198
199 // Every other iteration, also call flush(), just in case that potentially
200 // has any effect on how the writer thread wakes up.
201 if (n & 0x1) {
202 transport->flush();
203 }
204
205 /*
206 * Time the call to the destructor
207 */
208 struct timeval start;
209 struct timeval end;
210
211 gettimeofday(&start, NULL);
212 delete transport;
213 gettimeofday(&end, NULL);
214
215 int delta = time_diff(&start, &end);
David Reiss41993772010-10-06 17:10:31 +0000216
Roger Meier1ebeffb2011-06-06 18:00:03 +0000217 // If any attempt takes more than 500ms, treat that as a failure.
David Reiss41993772010-10-06 17:10:31 +0000218 // Treat this as a fatal failure, so we'll return now instead of
219 // looping over a very slow operation.
Roger Meier09cc5e72014-01-15 10:13:18 +0100220 BOOST_WARN_LT(delta, 500000);
David Reiss41993772010-10-06 17:10:31 +0000221
Roger Meier1ebeffb2011-06-06 18:00:03 +0000222 // Normally, it takes less than 100ms on my dev box.
David Reiss41993772010-10-06 17:10:31 +0000223 // However, if the box is heavily loaded, some of the test runs
224 // take longer, since we're just waiting for our turn on the CPU.
Roger Meier1ebeffb2011-06-06 18:00:03 +0000225 if (delta > 100000) {
Roger Meier085a3e72010-10-08 21:23:35 +0000226 ++num_over;
David Reiss41993772010-10-06 17:10:31 +0000227 }
David Reiss709b69f2010-10-06 17:10:30 +0000228 }
David Reiss41993772010-10-06 17:10:31 +0000229
Roger Meier085a3e72010-10-08 21:23:35 +0000230 // Make sure fewer than 10% of the runs took longer than 1000us
Roger Meier09cc5e72014-01-15 10:13:18 +0100231 BOOST_WARN(num_over < (NUM_ITERATIONS / 10));
David Reiss709b69f2010-10-06 17:10:30 +0000232}
233
234/**
235 * Make sure setFlushMaxUs() is honored.
236 */
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100237void test_flush_max_us_impl(uint32_t flush_us, uint32_t write_us, uint32_t test_us) {
David Reiss709b69f2010-10-06 17:10:30 +0000238 // TFileTransport only calls fsync() if data has been written,
239 // so make sure the write interval is smaller than the flush interval.
Roger Meier09cc5e72014-01-15 10:13:18 +0100240 BOOST_WARN(write_us < flush_us);
David Reiss709b69f2010-10-06 17:10:30 +0000241
242 TempFile f(tmp_dir, "thrift.TFileTransportTest.");
243
244 // Record calls to fsync()
245 FsyncLog log;
246 fsync_log = &log;
247
248 TFileTransport* transport = new TFileTransport(f.getPath());
249 // Don't flush because of # of bytes written
250 transport->setFlushMaxBytes(0xffffffff);
251 uint8_t buf[] = "a";
252 uint32_t buflen = sizeof(buf);
253
David Reiss41993772010-10-06 17:10:31 +0000254 // Set the flush interval
David Reiss709b69f2010-10-06 17:10:30 +0000255 transport->setFlushMaxUs(flush_us);
256
David Reiss41993772010-10-06 17:10:31 +0000257 // Make one call to write, to start the writer thread now.
258 // (If we just let the thread get created during our test loop,
259 // the thread creation sometimes takes long enough to make the first
260 // fsync interval fail the check.)
261 transport->write(buf, buflen);
262
David Reiss709b69f2010-10-06 17:10:30 +0000263 // Add one entry to the fsync log, just to mark the start time
264 log.fsync(-1);
265
266 // Loop doing write(), sleep(), ...
267 uint32_t total_time = 0;
268 while (true) {
269 transport->write(buf, buflen);
270 if (total_time > test_us) {
271 break;
272 }
273 usleep(write_us);
274 total_time += write_us;
275 }
276
277 delete transport;
278
279 // Stop logging new fsync() calls
280 fsync_log = NULL;
281
282 // Examine the fsync() log
283 //
284 // TFileTransport uses pthread_cond_timedwait(), which only has millisecond
285 // resolution. In my testing, it normally wakes up about 1 millisecond late.
286 // However, sometimes it takes a bit longer. Allow 5ms leeway.
287 int max_allowed_delta = flush_us + 5000;
288
289 const FsyncLog::CallList* calls = log.getCalls();
290 // We added 1 fsync call above.
291 // Make sure TFileTransport called fsync at least once
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100292 BOOST_WARN_GE(calls->size(), static_cast<FsyncLog::CallList::size_type>(1));
David Reiss709b69f2010-10-06 17:10:30 +0000293
294 const struct timeval* prev_time = NULL;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100295 for (FsyncLog::CallList::const_iterator it = calls->begin(); it != calls->end(); ++it) {
David Reiss709b69f2010-10-06 17:10:30 +0000296 if (prev_time) {
297 int delta = time_diff(prev_time, &it->time);
Roger Meier09cc5e72014-01-15 10:13:18 +0100298 BOOST_WARN_LT(delta, max_allowed_delta);
David Reiss709b69f2010-10-06 17:10:30 +0000299 }
300 prev_time = &it->time;
301 }
302}
303
David Reiss109693c2010-10-06 17:10:42 +0000304BOOST_AUTO_TEST_CASE(test_flush_max_us1) {
David Reiss709b69f2010-10-06 17:10:30 +0000305 // fsync every 10ms, write every 5ms, for 500ms
306 test_flush_max_us_impl(10000, 5000, 500000);
307}
308
David Reiss109693c2010-10-06 17:10:42 +0000309BOOST_AUTO_TEST_CASE(test_flush_max_us2) {
David Reiss709b69f2010-10-06 17:10:30 +0000310 // fsync every 50ms, write every 20ms, for 500ms
311 test_flush_max_us_impl(50000, 20000, 500000);
312}
313
David Reiss109693c2010-10-06 17:10:42 +0000314BOOST_AUTO_TEST_CASE(test_flush_max_us3) {
David Reiss709b69f2010-10-06 17:10:30 +0000315 // fsync every 400ms, write every 300ms, for 1s
316 test_flush_max_us_impl(400000, 300000, 1000000);
317}
318
David Reiss4f9efdb2010-10-06 17:10:33 +0000319/**
320 * Make sure flush() is fast when there is nothing to do.
321 *
322 * TFileTransport used to have a bug where flush() would wait for the fsync
323 * timeout to expire.
324 */
David Reiss109693c2010-10-06 17:10:42 +0000325BOOST_AUTO_TEST_CASE(test_noop_flush) {
David Reiss4f9efdb2010-10-06 17:10:33 +0000326 TempFile f(tmp_dir, "thrift.TFileTransportTest.");
327 TFileTransport transport(f.getPath());
328
329 // Write something to start the writer thread.
330 uint8_t buf[] = "a";
331 transport.write(buf, 1);
332
333 struct timeval start;
334 gettimeofday(&start, NULL);
335
336 for (unsigned int n = 0; n < 10; ++n) {
337 transport.flush();
338
339 struct timeval now;
340 gettimeofday(&now, NULL);
341
342 // Fail if at any point we've been running for longer than half a second.
343 // (With the buggy code, TFileTransport used to take 3 seconds per flush())
344 //
345 // Use a fatal fail so we break out early, rather than continuing to make
346 // many more slow flush() calls.
347 int delta = time_diff(&start, &now);
Roger Meier09cc5e72014-01-15 10:13:18 +0100348 BOOST_WARN_LT(delta, 2000000);
David Reiss4f9efdb2010-10-06 17:10:33 +0000349 }
350}
351
David Reiss709b69f2010-10-06 17:10:30 +0000352/**************************************************************************
353 * General Initialization
354 **************************************************************************/
355
356void print_usage(FILE* f, const char* argv0) {
357 fprintf(f, "Usage: %s [boost_options] [options]\n", argv0);
358 fprintf(f, "Options:\n");
359 fprintf(f, " --tmp-dir=DIR, -t DIR\n");
360 fprintf(f, " --help\n");
361}
362
363void parse_args(int argc, char* argv[]) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100364 struct option long_opts[]
365 = {{"help", false, NULL, 'h'}, {"tmp-dir", true, NULL, 't'}, {NULL, 0, NULL, 0}};
David Reiss709b69f2010-10-06 17:10:30 +0000366
367 while (true) {
368 optopt = 1;
369 int optchar = getopt_long(argc, argv, "ht:", long_opts, NULL);
370 if (optchar == -1) {
371 break;
372 }
373
374 switch (optchar) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100375 case 't':
376 tmp_dir = optarg;
377 break;
378 case 'h':
379 print_usage(stdout, argv[0]);
380 exit(0);
381 case '?':
382 exit(1);
383 default:
384 // Only happens if someone adds another option to the optarg string,
385 // but doesn't update the switch statement to handle it.
386 fprintf(stderr, "unknown option \"-%c\"\n", optchar);
387 exit(1);
David Reiss709b69f2010-10-06 17:10:30 +0000388 }
389 }
390}
391
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100392#ifdef BOOST_TEST_DYN_LINK
393static int myArgc = 0;
394static char **myArgv = NULL;
395
396bool init_unit_test_suite() {
397 boost::unit_test::framework::master_test_suite().p_name.value = "TFileTransportTest";
398
399 // Parse arguments
400 parse_args(myArgc,myArgv);
401 return true;
402}
403
404int main( int argc, char* argv[] ) {
405 myArgc = argc;
406 myArgv = argv;
407 return ::boost::unit_test::unit_test_main(&init_unit_test_suite,argc,argv);
408}
409#else
David Reiss709b69f2010-10-06 17:10:30 +0000410boost::unit_test::test_suite* init_unit_test_suite(int argc, char* argv[]) {
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100411 boost::unit_test::framework::master_test_suite().p_name.value = "TFileTransportTest";
David Reiss109693c2010-10-06 17:10:42 +0000412
David Reiss709b69f2010-10-06 17:10:30 +0000413 // Parse arguments
414 parse_args(argc, argv);
David Reiss109693c2010-10-06 17:10:42 +0000415 return NULL;
David Reiss709b69f2010-10-06 17:10:30 +0000416}
Antonio Di Monaco796667b2016-01-04 23:05:19 +0100417#endif