Skip to content

Commit

Permalink
Improve coro file (qicosmos#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jun 20, 2023
1 parent f436a17 commit db200c3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
15 changes: 9 additions & 6 deletions include/cinatra/coro_io/coro_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,16 @@ class coro_file {
}
#endif

bool is_open() {
bool is_open() { return stream_file_ && stream_file_->is_open(); }

void flush() {
#if defined(ENABLE_FILE_IO_URING)
return stream_file_ && stream_file_->is_open();

#else
return stream_file_ && stream_file_->is_open();
if (stream_file_) {
stream_file_->flush();
stream_file_->sync();
}
#endif
}

Expand Down Expand Up @@ -215,7 +220,6 @@ class coro_file {
promise.setValue(std::make_error_code(std::errc::io_error));
}
});

co_return co_await promise.getFuture();
}

Expand All @@ -234,7 +238,6 @@ class coro_file {
async_simple::coro::Lazy<std::error_code> async_write_impl(const char* data,
size_t size) {
stream_file_->write(data, size);
stream_file_->flush();
co_return std::error_code{};
}
#endif
Expand All @@ -250,4 +253,4 @@ class coro_file {

std::atomic<bool> eof_ = false;
};
} // namespace coro_io
} // namespace coro_io
12 changes: 10 additions & 2 deletions tests/test_corofile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,8 @@ TEST_CASE("small_file_write_test") {
std::cout << ec.message() << "\n";
}

file.flush();

std::ifstream is(filename, std::ios::binary);
if (!is.is_open()) {
std::cout << "Failed to open file: " << filename << "\n";
Expand All @@ -456,6 +458,7 @@ TEST_CASE("small_file_write_test") {
if (ec) {
std::cout << ec.message() << "\n";
}
file.flush();
is.open(filename, std::ios::binary);
if (!is.is_open()) {
std::cout << "Failed to open file: " << filename << "\n";
Expand Down Expand Up @@ -507,6 +510,7 @@ TEST_CASE("large_file_write_test") {
std::cout << ec.message() << "\n";
}
}
file.flush();
CHECK(fs::file_size(filename) == file_size);
std::ifstream is(filename, std::ios::binary);
if (!is.is_open()) {
Expand Down Expand Up @@ -551,7 +555,7 @@ TEST_CASE("empty_file_write_test") {
if (ec) {
std::cout << ec.message() << "\n";
}

file.flush();
std::ifstream is(filename, std::ios::binary);
if (!is.is_open()) {
std::cout << "Failed to open file: " << filename << "\n";
Expand Down Expand Up @@ -586,6 +590,7 @@ TEST_CASE("small_file_write_with_pool_test") {
if (ec) {
std::cout << ec.message() << "\n";
}
file.flush();

std::ifstream is(filename, std::ios::binary);
if (!is.is_open()) {
Expand All @@ -609,6 +614,7 @@ TEST_CASE("small_file_write_with_pool_test") {
if (ec) {
std::cout << ec.message() << "\n";
}
file.flush();
is.open(filename, std::ios::binary);
if (!is.is_open()) {
std::cout << "Failed to open file: " << filename << "\n";
Expand Down Expand Up @@ -659,7 +665,9 @@ TEST_CASE("large_file_write_with_pool_test") {
std::cout << ec.message() << "\n";
}
}
CHECK(fs::file_size(filename) == file_size);
file.flush();
size_t sz = fs::file_size(filename);
CHECK(sz == file_size);
std::ifstream is(filename, std::ios::binary);
if (!is.is_open()) {
std::cout << "Failed to open file: " << filename << "\n";
Expand Down

0 comments on commit db200c3

Please sign in to comment.