mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-07 13:20:05 +00:00
Make line buffering test less flaky
This commit is contained in:
parent
38881e5acf
commit
53347891cf
@ -227,13 +227,17 @@ class buffered_file {
|
||||
|
||||
FMT_API auto descriptor() const -> int;
|
||||
|
||||
void vprint(string_view format_str, format_args args) {
|
||||
fmt::vprint(file_, format_str, args);
|
||||
void vprint(string_view fmt, format_args args) {
|
||||
fmt::vprint(file_, fmt, args);
|
||||
}
|
||||
void vprint_locked(string_view fmt, format_args args) {
|
||||
fmt::vprint_locked(file_, fmt, args);
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline void print(string_view format_str, const Args&... args) {
|
||||
vprint(format_str, fmt::make_format_args(args...));
|
||||
template <typename... T>
|
||||
inline void print(string_view fmt, const T&... 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;
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -1763,24 +1764,24 @@ TEST(format_test, big_print) {
|
||||
TEST(format_test, line_buffering) {
|
||||
auto pipe = fmt::pipe();
|
||||
|
||||
int write_fd = pipe.write_end.descriptor();
|
||||
auto write_end = pipe.write_end.fdopen("w");
|
||||
setvbuf(write_end.get(), nullptr, _IOLBF, 4096);
|
||||
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");
|
||||
std::thread reader([&]() {
|
||||
int n = 0;
|
||||
int result = fscanf(read_end.get(), "%d", &n);
|
||||
(void)result;
|
||||
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();
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user