More tests. Fix warnings.

This commit is contained in:
Victor Zverovich 2014-05-19 07:31:05 -07:00
parent 952fd7a756
commit f578bc3a4a
4 changed files with 50 additions and 17 deletions

View File

@ -716,7 +716,7 @@ TEST(FileTest, Fdopen) {
File read_end, write_end; File read_end, write_end;
File::pipe(read_end, write_end); File::pipe(read_end, write_end);
int read_fd = read_end.descriptor(); 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) { TEST(FileTest, FdopenError) {

View File

@ -69,14 +69,14 @@ inline std::size_t ConvertRWCount(std::size_t count) { return count; }
} }
BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) { BufferedFile::~BufferedFile() FMT_NOEXCEPT(true) {
if (file_ && std::fclose(file_) != 0) if (file_ && FMT_SYSTEM(fclose(file_)) != 0)
fmt::ReportSystemError(errno, "cannot close file"); fmt::ReportSystemError(errno, "cannot close file");
} }
void BufferedFile::close() { void BufferedFile::close() {
if (!file_) if (!file_)
return; return;
int result = std::fclose(file_); int result = FMT_SYSTEM(fclose(file_));
file_ = 0; file_ = 0;
if (result != 0) if (result != 0)
fmt::ThrowSystemError(errno, "cannot close file"); fmt::ThrowSystemError(errno, "cannot close file");

View File

@ -42,11 +42,11 @@ int close_count;
int dup_count; int dup_count;
int dup2_count; int dup2_count;
int fdopen_count; int fdopen_count;
int fileno_count;
int read_count; int read_count;
int write_count; int write_count;
int pipe_count; int pipe_count;
int fclose_count; int fclose_count;
int fileno_count;
} }
#define EMULATE_EINTR(func, error_result) \ #define EMULATE_EINTR(func, error_result) \
@ -72,39 +72,34 @@ errno_t test::sopen_s(
int test::close(int fildes) { int test::close(int fildes) {
// Close the file first because close shouldn't be retried. // Close the file first because close shouldn't be retried.
int result = ::close(fildes); int result = ::FMT_POSIX(close(fildes));
EMULATE_EINTR(close, -1); EMULATE_EINTR(close, -1);
return result; return result;
} }
int test::dup(int fildes) { int test::dup(int fildes) {
EMULATE_EINTR(dup, -1); EMULATE_EINTR(dup, -1);
return ::dup(fildes); return ::FMT_POSIX(dup(fildes));
} }
int test::dup2(int fildes, int fildes2) { int test::dup2(int fildes, int fildes2) {
EMULATE_EINTR(dup2, -1); EMULATE_EINTR(dup2, -1);
return ::dup2(fildes, fildes2); return ::FMT_POSIX(dup2(fildes, fildes2));
} }
FILE *test::fdopen(int fildes, const char *mode) { FILE *test::fdopen(int fildes, const char *mode) {
EMULATE_EINTR(fdopen, 0); EMULATE_EINTR(fdopen, 0);
return ::fdopen(fildes, mode); return ::FMT_POSIX(fdopen(fildes, mode));
}
int test::fileno(FILE *stream) {
EMULATE_EINTR(fileno, -1);
return ::fileno(stream);
} }
test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) { test::ssize_t test::read(int fildes, void *buf, test::size_t nbyte) {
EMULATE_EINTR(read, -1); 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) { test::ssize_t test::write(int fildes, const void *buf, test::size_t nbyte) {
EMULATE_EINTR(write, -1); EMULATE_EINTR(write, -1);
return ::write(fildes, buf, nbyte); return ::FMT_POSIX(write(fildes, buf, nbyte));
} }
#ifndef _WIN32 #ifndef _WIN32
@ -124,6 +119,11 @@ int test::fclose(FILE *stream) {
return ::fclose(stream); return ::fclose(stream);
} }
int test::fileno(FILE *stream) {
EMULATE_EINTR(fileno, -1);
return ::fileno(stream);
}
#ifndef _WIN32 #ifndef _WIN32
# define EXPECT_RETRY(statement, func, message) \ # define EXPECT_RETRY(statement, func, message) \
func##_count = 1; \ func##_count = 1; \
@ -248,5 +248,38 @@ TEST(FileTest, FdopenNoRetry) {
fdopen_count = 0; 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 // TODO: test ConvertRWCount

View File

@ -51,7 +51,6 @@ int dup(int fildes);
int dup2(int fildes, int fildes2); int dup2(int fildes, int fildes2);
FILE *fdopen(int fildes, const char *mode); FILE *fdopen(int fildes, const char *mode);
int fileno(FILE *stream);
ssize_t read(int fildes, void *buf, size_t nbyte); ssize_t read(int fildes, void *buf, size_t nbyte);
ssize_t write(int fildes, const 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 #endif
int fclose(FILE *stream); int fclose(FILE *stream);
int fileno(FILE *stream);
} // namespace test } // namespace test