Skip to content

Commit 52cfb03

Browse files
author
mpenick
committed
412 - Make timestamp generator monotonic
Fix test build issue on Windows.
1 parent 761bdd1 commit 52cfb03

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

test/unit_tests/src/test_get_time.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
#include "get_time.hpp"
2222

23-
#include <unistd.h>
2423
#include <boost/test/unit_test.hpp>
24+
#include <boost/thread/thread.hpp>
2525

2626
BOOST_AUTO_TEST_SUITE(get_time)
2727

@@ -37,12 +37,21 @@ BOOST_AUTO_TEST_CASE(monotonic)
3737

3838
BOOST_AUTO_TEST_CASE(monotonic_duration)
3939
{
40+
// Sleep can be off by as much as 10+ ms on most systems (or >10% for 100ms)
41+
double tolerance = 15.0;
42+
43+
#ifdef _WIN32
44+
// Sleep can be off more on Windows; increasing tolerance
45+
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx
46+
tolerance = 25.0;
47+
#endif
48+
4049
uint64_t start = cass::get_time_monotonic_ns();
41-
sleep(1);
50+
boost::this_thread::sleep_for(boost::chrono::seconds(1));
4251
uint64_t elapsed = cass::get_time_monotonic_ns() - start;
4352
BOOST_REQUIRE_CLOSE(static_cast<double>(elapsed),
4453
static_cast<double>(NANOSECONDS_PER_SECOND),
45-
1.0); // 1% or +/- 10 milliseconds
54+
tolerance);
4655
}
4756

4857
BOOST_AUTO_TEST_SUITE_END()

test/unit_tests/src/test_timestamp_gen.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,16 @@ int run_monotonic_timestamp_gen(uint64_t warning_threshold_us, uint64_t warning_
6060
elapsed = cass::get_time_since_epoch_ms() - start;
6161
} while (elapsed < duration_ms);
6262

63-
double timestamp_rate = (static_cast<double>(timestamp_count) / elapsed) * 1000;
6463
// We can generate at most 1,000,000 timestamps in a second. If we exceed this
6564
// limit and the clock skew threshold then a warning log should have been printed.
66-
if (timestamp_rate > 1000000.0 &&
67-
elapsed * MICROSECONDS_PER_MILLISECOND > warning_threshold_us) {
68-
BOOST_CHECK(warn_count > 0);
69-
} else {
70-
BOOST_TEST_MESSAGE("Warning: The test did not exceed the timestamp generator maximum rate.");
71-
BOOST_CHECK(warn_count == 0);
65+
double timestamp_rate = (static_cast<double>(timestamp_count) / elapsed) * 1000;
66+
if (timestamp_rate <= 1000000.0 ||
67+
elapsed * MICROSECONDS_PER_MILLISECOND <= warning_threshold_us) {
68+
BOOST_TEST_MESSAGE("Warning: The test may not have exceeded the timestamp generator's maximum rate.");
7269
}
7370

71+
BOOST_CHECK(warn_count > 0);
72+
7473
return warn_count;
7574
}
7675

0 commit comments

Comments
 (0)