mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-31 19:24:48 +02:00
Make line buffering test less flaky
This commit is contained in:
@@ -227,13 +227,17 @@ class buffered_file {
|
|||||||
|
|
||||||
FMT_API auto descriptor() const -> int;
|
FMT_API auto descriptor() const -> int;
|
||||||
|
|
||||||
void vprint(string_view format_str, format_args args) {
|
void vprint(string_view fmt, format_args args) {
|
||||||
fmt::vprint(file_, format_str, args);
|
fmt::vprint(file_, fmt, args);
|
||||||
|
}
|
||||||
|
void vprint_locked(string_view fmt, format_args args) {
|
||||||
|
fmt::vprint_locked(file_, fmt, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... T>
|
||||||
inline void print(string_view format_str, const Args&... args) {
|
inline void print(string_view fmt, const T&... args) {
|
||||||
vprint(format_str, fmt::make_format_args(args...));
|
const auto& vargs = fmt::make_format_args(args...);
|
||||||
|
detail::is_locking<T...>() ? vprint(fmt, vargs) : vprint_locked(fmt, vargs);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -242,7 +242,8 @@ TEST(util_test, format_system_error) {
|
|||||||
throws_on_alloc = true;
|
throws_on_alloc = true;
|
||||||
}
|
}
|
||||||
if (!throws_on_alloc) {
|
if (!throws_on_alloc) {
|
||||||
fmt::print(stderr, "warning: std::allocator allocates {} chars\n", max_size);
|
fmt::print(stderr, "warning: std::allocator allocates {} chars\n",
|
||||||
|
max_size);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1763,24 +1764,24 @@ TEST(format_test, big_print) {
|
|||||||
TEST(format_test, line_buffering) {
|
TEST(format_test, line_buffering) {
|
||||||
auto pipe = fmt::pipe();
|
auto pipe = fmt::pipe();
|
||||||
|
|
||||||
|
int write_fd = pipe.write_end.descriptor();
|
||||||
auto write_end = pipe.write_end.fdopen("w");
|
auto write_end = pipe.write_end.fdopen("w");
|
||||||
setvbuf(write_end.get(), nullptr, _IOLBF, 4096);
|
setvbuf(write_end.get(), nullptr, _IOLBF, 4096);
|
||||||
write_end.print("42\n");
|
write_end.print("42\n");
|
||||||
|
close(write_fd);
|
||||||
|
try {
|
||||||
|
write_end.close();
|
||||||
|
} catch (const std::system_error&) {
|
||||||
|
}
|
||||||
|
|
||||||
std::mutex mutex;
|
|
||||||
std::condition_variable cv;
|
|
||||||
auto read_end = pipe.read_end.fdopen("r");
|
auto read_end = pipe.read_end.fdopen("r");
|
||||||
std::thread reader([&]() {
|
std::thread reader([&]() {
|
||||||
int n = 0;
|
int n = 0;
|
||||||
int result = fscanf(read_end.get(), "%d", &n);
|
int result = fscanf(read_end.get(), "%d", &n);
|
||||||
(void)result;
|
(void)result;
|
||||||
EXPECT_EQ(n, 42);
|
EXPECT_EQ(n, 42);
|
||||||
cv.notify_one();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
std::unique_lock<std::mutex> lock(mutex);
|
|
||||||
ASSERT_EQ(cv.wait_for(lock, std::chrono::minutes(1)),
|
|
||||||
std::cv_status::no_timeout);
|
|
||||||
reader.join();
|
reader.join();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user