From f578bc3a4a6e46f352f755ecd9655c359c8824e6 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 19 May 2014 07:31:05 -0700 Subject: [PATCH] More tests. Fix warnings. --- test/gtest-extra-test.cc | 2 +- test/gtest-extra.cc | 4 +-- test/posix-test.cc | 59 +++++++++++++++++++++++++++++++--------- test/posix-test.h | 2 +- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/test/gtest-extra-test.cc b/test/gtest-extra-test.cc index bdd13805..b2d9ce82 100644 --- a/test/gtest-extra-test.cc +++ b/test/gtest-extra-test.cc @@ -716,7 +716,7 @@ TEST(FileTest, Fdopen) { File read_end, write_end; File::pipe(read_end, write_end); int read_fd = read_end.descriptor(); - EXPECT_EQ(read_fd, fileno(read_end.fdopen("r").get())); + EXPECT_EQ(read_fd, FMT_POSIX(fileno(read_end.fdopen("r").get()))); } TEST(FileTest, FdopenError) { diff --git a/test/gtest-extra.cc b/test/gtest-extra.cc index 28cbe6ee..3e103afd 100644 --- a/test/gtest-extra.cc +++ b/test/gtest-extra.cc @@ -69,14 +69,14 @@ inline std::size_t ConvertRWCount(std::size_t count) { return count; } } BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) { - if (file_ && std::fclose(file_) != 0) + if (file_ && FMT_SYSTEM(fclose(file_)) != 0) fmt::ReportSystemError(errno, "cannot close file"); } void BufferedFile::close() { if (!file_) return; - int result = std::fclose(file_); + int result = FMT_SYSTEM(fclose(file_)); file_ = 0; if (result != 0) fmt::ThrowSystemError(errno, "cannot close file"); diff --git a/test/posix-test.cc b/test/posix-test.cc index 02cb0ce7..4a23698b 100644 --- a/test/posix-test.cc +++ b/test/posix-test.cc @@ -42,11 +42,11 @@ int close_count; int dup_count; int dup2_count; int fdopen_count; -int fileno_count; int read_count; int write_count; int pipe_count; int fclose_count; +int fileno_count; } #define EMULATE_EINTR(func, error_result) \ @@ -72,39 +72,34 @@ errno_t test::sopen_s( int test::close(int fildes) { // Close the file first because close shouldn't be retried. - int result = ::close(fildes); + int result = ::FMT_POSIX(close(fildes)); EMULATE_EINTR(close, -1); return result; } int test::dup(int fildes) { EMULATE_EINTR(dup, -1); - return ::dup(fildes); + return ::FMT_POSIX(dup(fildes)); } int test::dup2(int fildes, int fildes2) { EMULATE_EINTR(dup2, -1); - return ::dup2(fildes, fildes2); + return ::FMT_POSIX(dup2(fildes, fildes2)); } FILE *test::fdopen(int fildes, const char *mode) { EMULATE_EINTR(fdopen, 0); - return ::fdopen(fildes, mode); -} - -int test::fileno(FILE *stream) { - EMULATE_EINTR(fileno, -1); - return ::fileno(stream); + return ::FMT_POSIX(fdopen(fildes, mode)); } test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) { EMULATE_EINTR(read, -1); - return ::read(fildes, buf, nbyte); + return ::FMT_POSIX(read(fildes, buf, nbyte)); } test::ssize_t test::write(int fildes, const void *buf, test::size_t nbyte) { EMULATE_EINTR(write, -1); - return ::write(fildes, buf, nbyte); + return ::FMT_POSIX(write(fildes, buf, nbyte)); } #ifndef _WIN32 @@ -124,6 +119,11 @@ int test::fclose(FILE *stream) { return ::fclose(stream); } +int test::fileno(FILE *stream) { + EMULATE_EINTR(fileno, -1); + return ::fileno(stream); +} + #ifndef _WIN32 # define EXPECT_RETRY(statement, func, message) \ func##_count = 1; \ @@ -248,5 +248,38 @@ TEST(FileTest, FdopenNoRetry) { fdopen_count = 0; } -// TODO: test retry on EINTR in fclose & fileno +TEST(BufferedFileTest, CloseNoRetryInDtor) { + File read_end, write_end; + File::pipe(read_end, write_end); + BufferedFile *f = new BufferedFile(read_end.fdopen("r")); + int saved_fclose_count = 0; + EXPECT_WRITE(stderr, { + fclose_count = 1; + delete f; + saved_fclose_count = fclose_count; + fclose_count = 0; + }, FormatSystemErrorMessage(EINTR, "cannot close file") + "\n"); + EXPECT_EQ(2, saved_fclose_count); +} + +TEST(BufferedFileTest, CloseNoRetry) { + File read_end, write_end; + File::pipe(read_end, write_end); + BufferedFile f = read_end.fdopen("r"); + fclose_count = 1; + EXPECT_SYSTEM_ERROR(f.close(), EINTR, "cannot close file"); + EXPECT_EQ(2, fclose_count); + fclose_count = 0; +} + +TEST(BufferedFileTest, FilenoNoRetry) { + File read_end, write_end; + File::pipe(read_end, write_end); + BufferedFile f = read_end.fdopen("r"); + fileno_count = 1; + EXPECT_SYSTEM_ERROR(f.fileno(), EINTR, "cannot get file descriptor"); + EXPECT_EQ(2, fileno_count); + fileno_count = 0; +} + // TODO: test ConvertRWCount diff --git a/test/posix-test.h b/test/posix-test.h index 3afd7dc6..c42a20e4 100644 --- a/test/posix-test.h +++ b/test/posix-test.h @@ -51,7 +51,6 @@ int dup(int fildes); int dup2(int fildes, int fildes2); FILE *fdopen(int fildes, const char *mode); -int fileno(FILE *stream); ssize_t read(int fildes, void *buf, size_t nbyte); ssize_t write(int fildes, const void *buf, size_t nbyte); @@ -63,6 +62,7 @@ int pipe(int *pfds, unsigned psize, int textmode); #endif int fclose(FILE *stream); +int fileno(FILE *stream); } // namespace test