mirror of
https://github.com/google/leveldb.git
synced 2026-08-22 10:53:27 +08:00
Switch testing harness to googletest.
PiperOrigin-RevId: 281815695
This commit is contained in:
committed by
Victor Costan
parent
2c9c80bd53
commit
1c58902bdc
@@ -13,10 +13,11 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
#include "third_party/googletest/googletest/include/gtest/gtest.h"
|
||||
#include "leveldb/env.h"
|
||||
#include "port/port.h"
|
||||
#include "util/env_posix_test_helper.h"
|
||||
#include "util/testharness.h"
|
||||
#include "util/testutil.h"
|
||||
|
||||
#if HAVE_O_CLOEXEC
|
||||
|
||||
@@ -168,7 +169,7 @@ namespace leveldb {
|
||||
static const int kReadOnlyFileLimit = 4;
|
||||
static const int kMMapLimit = 4;
|
||||
|
||||
class EnvPosixTest {
|
||||
class EnvPosixTest : public testing::Test {
|
||||
public:
|
||||
static void SetFileLimits(int read_only_file_limit, int mmap_limit) {
|
||||
EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit);
|
||||
@@ -180,10 +181,10 @@ class EnvPosixTest {
|
||||
Env* env_;
|
||||
};
|
||||
|
||||
TEST(EnvPosixTest, TestOpenOnRead) {
|
||||
TEST_F(EnvPosixTest, TestOpenOnRead) {
|
||||
// Write some test data to a single file that will be opened |n| times.
|
||||
std::string test_dir;
|
||||
ASSERT_OK(env_->GetTestDirectory(&test_dir));
|
||||
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
||||
std::string test_file = test_dir + "/open_on_read.txt";
|
||||
|
||||
FILE* f = fopen(test_file.c_str(), "we");
|
||||
@@ -197,133 +198,133 @@ TEST(EnvPosixTest, TestOpenOnRead) {
|
||||
const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5;
|
||||
leveldb::RandomAccessFile* files[kNumFiles] = {0};
|
||||
for (int i = 0; i < kNumFiles; i++) {
|
||||
ASSERT_OK(env_->NewRandomAccessFile(test_file, &files[i]));
|
||||
ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(test_file, &files[i]));
|
||||
}
|
||||
char scratch;
|
||||
Slice read_result;
|
||||
for (int i = 0; i < kNumFiles; i++) {
|
||||
ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
|
||||
ASSERT_LEVELDB_OK(files[i]->Read(i, 1, &read_result, &scratch));
|
||||
ASSERT_EQ(kFileData[i], read_result[0]);
|
||||
}
|
||||
for (int i = 0; i < kNumFiles; i++) {
|
||||
delete files[i];
|
||||
}
|
||||
ASSERT_OK(env_->DeleteFile(test_file));
|
||||
ASSERT_LEVELDB_OK(env_->DeleteFile(test_file));
|
||||
}
|
||||
|
||||
#if HAVE_O_CLOEXEC
|
||||
|
||||
TEST(EnvPosixTest, TestCloseOnExecSequentialFile) {
|
||||
TEST_F(EnvPosixTest, TestCloseOnExecSequentialFile) {
|
||||
std::unordered_set<int> open_fds;
|
||||
GetOpenFileDescriptors(&open_fds);
|
||||
|
||||
std::string test_dir;
|
||||
ASSERT_OK(env_->GetTestDirectory(&test_dir));
|
||||
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
||||
std::string file_path = test_dir + "/close_on_exec_sequential.txt";
|
||||
ASSERT_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
ASSERT_LEVELDB_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
|
||||
leveldb::SequentialFile* file = nullptr;
|
||||
ASSERT_OK(env_->NewSequentialFile(file_path, &file));
|
||||
ASSERT_LEVELDB_OK(env_->NewSequentialFile(file_path, &file));
|
||||
CheckCloseOnExecDoesNotLeakFDs(open_fds);
|
||||
delete file;
|
||||
|
||||
ASSERT_OK(env_->DeleteFile(file_path));
|
||||
ASSERT_LEVELDB_OK(env_->DeleteFile(file_path));
|
||||
}
|
||||
|
||||
TEST(EnvPosixTest, TestCloseOnExecRandomAccessFile) {
|
||||
TEST_F(EnvPosixTest, TestCloseOnExecRandomAccessFile) {
|
||||
std::unordered_set<int> open_fds;
|
||||
GetOpenFileDescriptors(&open_fds);
|
||||
|
||||
std::string test_dir;
|
||||
ASSERT_OK(env_->GetTestDirectory(&test_dir));
|
||||
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
||||
std::string file_path = test_dir + "/close_on_exec_random_access.txt";
|
||||
ASSERT_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
ASSERT_LEVELDB_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
|
||||
// Exhaust the RandomAccessFile mmap limit. This way, the test
|
||||
// RandomAccessFile instance below is backed by a file descriptor, not by an
|
||||
// mmap region.
|
||||
leveldb::RandomAccessFile* mmapped_files[kReadOnlyFileLimit] = {nullptr};
|
||||
for (int i = 0; i < kReadOnlyFileLimit; i++) {
|
||||
ASSERT_OK(env_->NewRandomAccessFile(file_path, &mmapped_files[i]));
|
||||
ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(file_path, &mmapped_files[i]));
|
||||
}
|
||||
|
||||
leveldb::RandomAccessFile* file = nullptr;
|
||||
ASSERT_OK(env_->NewRandomAccessFile(file_path, &file));
|
||||
ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(file_path, &file));
|
||||
CheckCloseOnExecDoesNotLeakFDs(open_fds);
|
||||
delete file;
|
||||
|
||||
for (int i = 0; i < kReadOnlyFileLimit; i++) {
|
||||
delete mmapped_files[i];
|
||||
}
|
||||
ASSERT_OK(env_->DeleteFile(file_path));
|
||||
ASSERT_LEVELDB_OK(env_->DeleteFile(file_path));
|
||||
}
|
||||
|
||||
TEST(EnvPosixTest, TestCloseOnExecWritableFile) {
|
||||
TEST_F(EnvPosixTest, TestCloseOnExecWritableFile) {
|
||||
std::unordered_set<int> open_fds;
|
||||
GetOpenFileDescriptors(&open_fds);
|
||||
|
||||
std::string test_dir;
|
||||
ASSERT_OK(env_->GetTestDirectory(&test_dir));
|
||||
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
||||
std::string file_path = test_dir + "/close_on_exec_writable.txt";
|
||||
ASSERT_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
ASSERT_LEVELDB_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
|
||||
leveldb::WritableFile* file = nullptr;
|
||||
ASSERT_OK(env_->NewWritableFile(file_path, &file));
|
||||
ASSERT_LEVELDB_OK(env_->NewWritableFile(file_path, &file));
|
||||
CheckCloseOnExecDoesNotLeakFDs(open_fds);
|
||||
delete file;
|
||||
|
||||
ASSERT_OK(env_->DeleteFile(file_path));
|
||||
ASSERT_LEVELDB_OK(env_->DeleteFile(file_path));
|
||||
}
|
||||
|
||||
TEST(EnvPosixTest, TestCloseOnExecAppendableFile) {
|
||||
TEST_F(EnvPosixTest, TestCloseOnExecAppendableFile) {
|
||||
std::unordered_set<int> open_fds;
|
||||
GetOpenFileDescriptors(&open_fds);
|
||||
|
||||
std::string test_dir;
|
||||
ASSERT_OK(env_->GetTestDirectory(&test_dir));
|
||||
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
||||
std::string file_path = test_dir + "/close_on_exec_appendable.txt";
|
||||
ASSERT_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
ASSERT_LEVELDB_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
|
||||
leveldb::WritableFile* file = nullptr;
|
||||
ASSERT_OK(env_->NewAppendableFile(file_path, &file));
|
||||
ASSERT_LEVELDB_OK(env_->NewAppendableFile(file_path, &file));
|
||||
CheckCloseOnExecDoesNotLeakFDs(open_fds);
|
||||
delete file;
|
||||
|
||||
ASSERT_OK(env_->DeleteFile(file_path));
|
||||
ASSERT_LEVELDB_OK(env_->DeleteFile(file_path));
|
||||
}
|
||||
|
||||
TEST(EnvPosixTest, TestCloseOnExecLockFile) {
|
||||
TEST_F(EnvPosixTest, TestCloseOnExecLockFile) {
|
||||
std::unordered_set<int> open_fds;
|
||||
GetOpenFileDescriptors(&open_fds);
|
||||
|
||||
std::string test_dir;
|
||||
ASSERT_OK(env_->GetTestDirectory(&test_dir));
|
||||
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
||||
std::string file_path = test_dir + "/close_on_exec_lock.txt";
|
||||
ASSERT_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
ASSERT_LEVELDB_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
|
||||
leveldb::FileLock* lock = nullptr;
|
||||
ASSERT_OK(env_->LockFile(file_path, &lock));
|
||||
ASSERT_LEVELDB_OK(env_->LockFile(file_path, &lock));
|
||||
CheckCloseOnExecDoesNotLeakFDs(open_fds);
|
||||
ASSERT_OK(env_->UnlockFile(lock));
|
||||
ASSERT_LEVELDB_OK(env_->UnlockFile(lock));
|
||||
|
||||
ASSERT_OK(env_->DeleteFile(file_path));
|
||||
ASSERT_LEVELDB_OK(env_->DeleteFile(file_path));
|
||||
}
|
||||
|
||||
TEST(EnvPosixTest, TestCloseOnExecLogger) {
|
||||
TEST_F(EnvPosixTest, TestCloseOnExecLogger) {
|
||||
std::unordered_set<int> open_fds;
|
||||
GetOpenFileDescriptors(&open_fds);
|
||||
|
||||
std::string test_dir;
|
||||
ASSERT_OK(env_->GetTestDirectory(&test_dir));
|
||||
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
||||
std::string file_path = test_dir + "/close_on_exec_logger.txt";
|
||||
ASSERT_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
ASSERT_LEVELDB_OK(WriteStringToFile(env_, "0123456789", file_path));
|
||||
|
||||
leveldb::Logger* file = nullptr;
|
||||
ASSERT_OK(env_->NewLogger(file_path, &file));
|
||||
ASSERT_LEVELDB_OK(env_->NewLogger(file_path, &file));
|
||||
CheckCloseOnExecDoesNotLeakFDs(open_fds);
|
||||
delete file;
|
||||
|
||||
ASSERT_OK(env_->DeleteFile(file_path));
|
||||
ASSERT_LEVELDB_OK(env_->DeleteFile(file_path));
|
||||
}
|
||||
|
||||
#endif // HAVE_O_CLOEXEC
|
||||
@@ -346,5 +347,7 @@ int main(int argc, char** argv) {
|
||||
// All tests currently run with the same read-only file limits.
|
||||
leveldb::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit,
|
||||
leveldb::kMMapLimit);
|
||||
return leveldb::test::RunAllTests();
|
||||
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user