Skip to content

Commit a5837ed

Browse files
committed
Try to fix random failure of the clock test
microsoft#1248 (comment)
1 parent 69f9d8c commit a5837ed

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

test/old_tests/UnitTests/clock.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,33 +66,34 @@ TEST_CASE("clock, units")
6666

6767
TEST_CASE("clock, time_t")
6868
{
69+
const DateTime now_dt = clock::now();
70+
const time_t now_tt = time(nullptr);
71+
6972
// Round trip from DateTime to time_t and back.
7073
// confirm that nothing happens other than truncating the fractional seconds
71-
const DateTime now_dt = clock::now();
7274
REQUIRE(clock::from_time_t(clock::to_time_t(now_dt)) == time_point_cast<seconds>(now_dt));
7375

7476
// Same thing in reverse
75-
const time_t now_tt = time(nullptr);
7677
REQUIRE(clock::to_time_t(clock::from_time_t(now_tt)) == now_tt);
7778

7879
// Conversions are verified to be consistent. Now, verify that we're correctly converting epochs
79-
const auto diff = duration_cast<milliseconds>(abs(clock::now() - clock::from_time_t(time(nullptr)))).count();
80+
const auto diff = duration_cast<milliseconds>(abs(now_dt - clock::from_time_t(now_tt))).count();
8081
REQUIRE(diff < 1000);
8182
}
8283

8384
TEST_CASE("clock, FILETIME")
8485
{
85-
// Round trip conversions
8686
const DateTime now_dt = clock::now();
87-
REQUIRE(clock::from_file_time(clock::to_file_time(now_dt)) == now_dt);
88-
8987
FILETIME now_ft;
9088
::GetSystemTimePreciseAsFileTime(&now_ft);
89+
90+
// Round trip conversions
91+
REQUIRE(clock::from_file_time(clock::to_file_time(now_dt)) == now_dt);
92+
9193
REQUIRE(clock::to_file_time(clock::from_file_time(now_ft)) == now_ft);
9294

9395
// Verify epoch
94-
::GetSystemTimePreciseAsFileTime(&now_ft);
95-
const auto diff = abs(clock::now() - clock::from_file_time(now_ft));
96+
const auto diff = abs(now_dt - clock::from_file_time(now_ft));
9697
REQUIRE(diff < milliseconds{ 100 });
9798
}
9899

0 commit comments

Comments
 (0)