Skip to content

Commit f0a8edb

Browse files
committed
Increased try_lock_for time in tests
MSVC and AppleClang seems to need more time so an extra grace period of 100ms is added while waiting for an expected successful lock. Also added a missing header to mutex.test.cpp Signed-off-by: Ted Lyngmo <[email protected]>
1 parent 78dc936 commit f0a8edb

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

examples/try_lock_a_bunch.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <beman/timed_lock_alg/mutex.hpp>
77

88
#include <chrono>
9+
#include <functional>
910
#include <iostream>
1011
#include <thread>
1112

tests/beman/timed_lock_alg/mutex.test.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class JThread : public std::thread {
2626
}
2727
};
2828
const auto now = std::chrono::steady_clock::now();
29-
const auto no_duration = std::chrono::milliseconds{0};
29+
const auto no_duration = 0ms;
30+
const auto extra_grace = 100ms;
3031

3132
template <class MutexType, std::size_t N>
3233
void unlocker(std::array<MutexType, N>& mtxs) {
@@ -67,7 +68,7 @@ TEST(Mutex, try_many_one_locked) {
6768
});
6869

6970
std::this_thread::sleep_for(5ms); // approx 10ms left on lock after this
70-
EXPECT_EQ(-1, std::apply([](auto&... mts) { return tla::try_lock_for(20ms, mts...); }, mtxs));
71+
EXPECT_EQ(-1, std::apply([](auto&... mts) { return tla::try_lock_for(20ms + extra_grace, mts...); }, mtxs));
7172

7273
unlocker(mtxs);
7374
}
@@ -108,6 +109,6 @@ TEST(Mutex, succeed_with_three_in_tricky_sequence) {
108109
});
109110

110111
std::this_thread::sleep_for(5ms);
111-
// 20ms is enough for most implementations, but lets give MSVC 10ms more
112-
EXPECT_EQ(-1, std::apply([](auto&... mts) { return tla::try_lock_for(20ms + 10ms, mts...); }, mtxs));
112+
// 20ms is enough for most implementations, but some require a lot more so let them have it:
113+
EXPECT_EQ(-1, std::apply([](auto&... mts) { return tla::try_lock_for(20ms + extra_grace, mts...); }, mtxs));
113114
}

0 commit comments

Comments
 (0)