From f3f139737c666e558864ca8b473959be203edaf6 Mon Sep 17 00:00:00 2001 From: costan Date: Wed, 1 Mar 2017 12:05:47 -0800 Subject: [PATCH] Separate Env tests from PosixEnv tests. env_test.cc defines EnvPosixTest which tests the Env implementation returned by Env::Default(). The naming is a bit unfortunate, as the tests in env_test.cc are written against the Env contract, and therefore are applicable to any Env implementation. An instance of the confusion caused by the naming is [] which added a dependency from env_test.cc to EnvPosixTestHelper, which is closely coupled to EnvPosix. This change disentangles EnvPosix-specific test code into a env_posix_test.cc file. The code there uses EnvPosixTestHelper and specifically targets the EnvPosix implementation. env_test.cc now implements EnvTest, and contains tests that are also applicable to other ports, which may define their own Env implementation. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=148914642 --- Makefile | 4 +++ util/env_posix_test.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ util/env_test.cc | 56 ++++++------------------------------------ 3 files changed, 78 insertions(+), 48 deletions(-) create mode 100644 util/env_posix_test.cc diff --git a/Makefile b/Makefile index 66e3226..5cfd6f9 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,7 @@ TESTS = \ util/cache_test \ util/coding_test \ util/crc32c_test \ + util/env_posix_test \ util/env_test \ util/hash_test @@ -337,6 +338,9 @@ $(STATIC_OUTDIR)/db_test:db/db_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) $(STATIC_OUTDIR)/dbformat_test:db/dbformat_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) $(CXX) $(LDFLAGS) $(CXXFLAGS) db/dbformat_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) -o $@ $(LIBS) +$(STATIC_OUTDIR)/env_posix_test:util/env_posix_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) + $(CXX) $(LDFLAGS) $(CXXFLAGS) util/env_posix_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) -o $@ $(LIBS) + $(STATIC_OUTDIR)/env_test:util/env_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) $(CXX) $(LDFLAGS) $(CXXFLAGS) util/env_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) -o $@ $(LIBS) diff --git a/util/env_posix_test.cc b/util/env_posix_test.cc new file mode 100644 index 0000000..295f8ae --- /dev/null +++ b/util/env_posix_test.cc @@ -0,0 +1,66 @@ +// Copyright (c) 2011 The LevelDB Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. See the AUTHORS file for names of contributors. + +#include "leveldb/env.h" + +#include "port/port.h" +#include "util/testharness.h" +#include "util/env_posix_test_helper.h" + +namespace leveldb { + +static const int kDelayMicros = 100000; +static const int kReadOnlyFileLimit = 4; +static const int kMMapLimit = 4; + +class EnvPosixTest { + public: + Env* env_; + EnvPosixTest() : env_(Env::Default()) { } + + static void SetFileLimits(int read_only_file_limit, int mmap_limit) { + EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit); + EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit); + } +}; + +TEST(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)); + std::string test_file = test_dir + "/open_on_read.txt"; + + FILE* f = fopen(test_file.c_str(), "w"); + ASSERT_TRUE(f != NULL); + const char kFileData[] = "abcdefghijklmnopqrstuvwxyz"; + fputs(kFileData, f); + fclose(f); + + // Open test file some number above the sum of the two limits to force + // open-on-read behavior of POSIX Env leveldb::RandomAccessFile. + 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])); + } + char scratch; + Slice read_result; + for (int i = 0; i < kNumFiles; i++) { + ASSERT_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)); +} + +} // namespace leveldb + +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(); +} diff --git a/util/env_test.cc b/util/env_test.cc index 0cb1577..839ae56 100644 --- a/util/env_test.cc +++ b/util/env_test.cc @@ -6,7 +6,6 @@ #include "port/port.h" #include "util/testharness.h" -#include "util/env_posix_test_helper.h" namespace leveldb { @@ -14,33 +13,28 @@ static const int kDelayMicros = 100000; static const int kReadOnlyFileLimit = 4; static const int kMMapLimit = 4; -class EnvPosixTest { +class EnvTest { private: port::Mutex mu_; std::string events_; public: Env* env_; - EnvPosixTest() : env_(Env::Default()) { } - - static void SetFileLimits(int read_only_file_limit, int mmap_limit) { - EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit); - EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit); - } + EnvTest() : env_(Env::Default()) { } }; static void SetBool(void* ptr) { reinterpret_cast(ptr)->NoBarrier_Store(ptr); } -TEST(EnvPosixTest, RunImmediately) { +TEST(EnvTest, RunImmediately) { port::AtomicPointer called (NULL); env_->Schedule(&SetBool, &called); - Env::Default()->SleepForMicroseconds(kDelayMicros); + env_->SleepForMicroseconds(kDelayMicros); ASSERT_TRUE(called.NoBarrier_Load() != NULL); } -TEST(EnvPosixTest, RunMany) { +TEST(EnvTest, RunMany) { port::AtomicPointer last_id (NULL); struct CB { @@ -67,7 +61,7 @@ TEST(EnvPosixTest, RunMany) { env_->Schedule(&CB::Run, &cb3); env_->Schedule(&CB::Run, &cb4); - Env::Default()->SleepForMicroseconds(kDelayMicros); + env_->SleepForMicroseconds(kDelayMicros); void* cur = last_id.Acquire_Load(); ASSERT_EQ(4, reinterpret_cast(cur)); } @@ -86,7 +80,7 @@ static void ThreadBody(void* arg) { s->mu.Unlock(); } -TEST(EnvPosixTest, StartThread) { +TEST(EnvTest, StartThread) { State state; state.val = 0; state.num_running = 3; @@ -100,47 +94,13 @@ TEST(EnvPosixTest, StartThread) { if (num == 0) { break; } - Env::Default()->SleepForMicroseconds(kDelayMicros); + env_->SleepForMicroseconds(kDelayMicros); } ASSERT_EQ(state.val, 3); } -TEST(EnvPosixTest, TestOpenOnRead) { - // Write some test data to a single file that will be opened |n| times. - std::string test_dir; - ASSERT_OK(Env::Default()->GetTestDirectory(&test_dir)); - std::string test_file = test_dir + "/open_on_read.txt"; - - FILE* f = fopen(test_file.c_str(), "w"); - ASSERT_TRUE(f != NULL); - const char kFileData[] = "abcdefghijklmnopqrstuvwxyz"; - fputs(kFileData, f); - fclose(f); - - // Open test file some number above the sum of the two limits to force - // open-on-read behavior of POSIX Env leveldb::RandomAccessFile. - const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5; - leveldb::RandomAccessFile* files[kNumFiles] = {0}; - for (int i = 0; i < kNumFiles; i++) { - ASSERT_OK(Env::Default()->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_EQ(kFileData[i], read_result[0]); - } - for (int i = 0; i < kNumFiles; i++) { - delete files[i]; - } - ASSERT_OK(Env::Default()->DeleteFile(test_file)); -} - } // namespace leveldb 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(); }