Skip to content

Commit

Permalink
rocksdb: switch to gtest
Browse files Browse the repository at this point in the history
Summary:
Our existing test notation is very similar to what is used in gtest. It makes it easy to adopt what is different.
In this diff I modify existing [[ https://code.google.com/p/googletest/wiki/Primer#Test_Fixtures:_Using_the_Same_Data_Configuration_for_Multiple_Te | test fixture ]] classes to inherit from `testing::Test`. Also for unit tests that use fixture class, `TEST` is replaced with `TEST_F` as required in gtest.

There are several custom `main` functions in our existing tests. To make this transition easier, I modify all `main` functions to fallow gtest notation. But eventually we can remove them and use implementation of `main` that gtest provides.

```lang=bash
% cat ~/transform
#!/bin/sh
files=$(git ls-files '*test\.cc')
for file in $files
do
  if grep -q "rocksdb::test::RunAllTests()" $file
  then
    if grep -Eq '^class \w+Test {' $file
    then
      perl -pi -e 's/^(class \w+Test) {/${1}: public testing::Test {/g' $file
      perl -pi -e 's/^(TEST)/${1}_F/g' $file
    fi
    perl -pi -e 's/(int main.*\{)/${1}::testing::InitGoogleTest(&argc, argv);/g' $file
    perl -pi -e 's/rocksdb::test::RunAllTests/RUN_ALL_TESTS/g' $file
  fi
done
% sh ~/transform
% make format
```

Second iteration of this diff contains only scripted changes.

Third iteration contains manual changes to fix last errors and make it compilable.

Test Plan:
Build and notice no errors.
```lang=bash
% USE_CLANG=1 make check -j55
```
Tests are still testing.

Reviewers: meyering, sdong, rven, igor

Reviewed By: igor

Subscribers: dhruba, leveldb

Differential Revision: https://reviews.facebook.net/D35157
  • Loading branch information
Igor Sugak committed Mar 17, 2015
1 parent 413e352 commit b4b69e4
Show file tree
Hide file tree
Showing 68 changed files with 896 additions and 1,001 deletions.
42 changes: 21 additions & 21 deletions db/column_family_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class EnvCounter : public EnvWrapper {
int num_new_writable_file_;
};

class ColumnFamilyTest {
class ColumnFamilyTest : public testing::Test {
public:
ColumnFamilyTest() : rnd_(139) {
env_ = new EnvCounter(Env::Default());
Expand Down Expand Up @@ -333,7 +333,7 @@ class DumbLogger : public Logger {
virtual size_t GetLogFileSize() const override { return 0; }
};

TEST(ColumnFamilyTest, DontReuseColumnFamilyID) {
TEST_F(ColumnFamilyTest, DontReuseColumnFamilyID) {
for (int iter = 0; iter < 3; ++iter) {
Open();
CreateColumnFamilies({"one", "two", "three"});
Expand All @@ -360,8 +360,7 @@ TEST(ColumnFamilyTest, DontReuseColumnFamilyID) {
}
}


TEST(ColumnFamilyTest, AddDrop) {
TEST_F(ColumnFamilyTest, AddDrop) {
Open();
CreateColumnFamilies({"one", "two", "three"});
ASSERT_EQ("NOT_FOUND", Get(1, "fodor"));
Expand All @@ -387,7 +386,7 @@ TEST(ColumnFamilyTest, AddDrop) {
std::vector<std::string>({"default", "four", "three"}));
}

TEST(ColumnFamilyTest, DropTest) {
TEST_F(ColumnFamilyTest, DropTest) {
// first iteration - dont reopen DB before dropping
// second iteration - reopen DB before dropping
for (int iter = 0; iter < 2; ++iter) {
Expand All @@ -411,7 +410,7 @@ TEST(ColumnFamilyTest, DropTest) {
}
}

TEST(ColumnFamilyTest, WriteBatchFailure) {
TEST_F(ColumnFamilyTest, WriteBatchFailure) {
Open();
CreateColumnFamiliesAndReopen({"one", "two"});
WriteBatch batch;
Expand All @@ -429,7 +428,7 @@ TEST(ColumnFamilyTest, WriteBatchFailure) {
Close();
}

TEST(ColumnFamilyTest, ReadWrite) {
TEST_F(ColumnFamilyTest, ReadWrite) {
Open();
CreateColumnFamiliesAndReopen({"one", "two"});
ASSERT_OK(Put(0, "foo", "v1"));
Expand All @@ -453,7 +452,7 @@ TEST(ColumnFamilyTest, ReadWrite) {
Close();
}

TEST(ColumnFamilyTest, IgnoreRecoveredLog) {
TEST_F(ColumnFamilyTest, IgnoreRecoveredLog) {
std::string backup_logs = dbname_ + "/backup_logs";

// delete old files in backup_logs directory
Expand Down Expand Up @@ -528,7 +527,7 @@ TEST(ColumnFamilyTest, IgnoreRecoveredLog) {
}
}

TEST(ColumnFamilyTest, FlushTest) {
TEST_F(ColumnFamilyTest, FlushTest) {
Open();
CreateColumnFamiliesAndReopen({"one", "two"});
ASSERT_OK(Put(0, "foo", "v1"));
Expand Down Expand Up @@ -577,7 +576,7 @@ TEST(ColumnFamilyTest, FlushTest) {
}

// Makes sure that obsolete log files get deleted
TEST(ColumnFamilyTest, LogDeletionTest) {
TEST_F(ColumnFamilyTest, LogDeletionTest) {
db_options_.max_total_wal_size = std::numeric_limits<uint64_t>::max();
column_family_options_.write_buffer_size = 100000; // 100KB
Open();
Expand Down Expand Up @@ -644,7 +643,7 @@ TEST(ColumnFamilyTest, LogDeletionTest) {
}

// Makes sure that obsolete log files get deleted
TEST(ColumnFamilyTest, DifferentWriteBufferSizes) {
TEST_F(ColumnFamilyTest, DifferentWriteBufferSizes) {
// disable flushing stale column families
db_options_.max_total_wal_size = std::numeric_limits<uint64_t>::max();
Open();
Expand Down Expand Up @@ -738,7 +737,7 @@ TEST(ColumnFamilyTest, DifferentWriteBufferSizes) {
Close();
}

TEST(ColumnFamilyTest, MemtableNotSupportSnapshot) {
TEST_F(ColumnFamilyTest, MemtableNotSupportSnapshot) {
Open();
auto* s1 = dbfull()->GetSnapshot();
ASSERT_TRUE(s1 != nullptr);
Expand All @@ -759,7 +758,7 @@ TEST(ColumnFamilyTest, MemtableNotSupportSnapshot) {
Close();
}

TEST(ColumnFamilyTest, DifferentMergeOperators) {
TEST_F(ColumnFamilyTest, DifferentMergeOperators) {
Open();
CreateColumnFamilies({"first", "second"});
ColumnFamilyOptions default_cf, first, second;
Expand Down Expand Up @@ -789,7 +788,7 @@ TEST(ColumnFamilyTest, DifferentMergeOperators) {
Close();
}

TEST(ColumnFamilyTest, DifferentCompactionStyles) {
TEST_F(ColumnFamilyTest, DifferentCompactionStyles) {
Open();
CreateColumnFamilies({"one", "two"});
ColumnFamilyOptions default_cf, one, two;
Expand Down Expand Up @@ -864,7 +863,7 @@ std::string IterStatus(Iterator* iter) {
}
} // anonymous namespace

TEST(ColumnFamilyTest, NewIteratorsTest) {
TEST_F(ColumnFamilyTest, NewIteratorsTest) {
// iter == 0 -- no tailing
// iter == 2 -- tailing
for (int iter = 0; iter < 2; ++iter) {
Expand Down Expand Up @@ -909,7 +908,7 @@ TEST(ColumnFamilyTest, NewIteratorsTest) {
}
}

TEST(ColumnFamilyTest, ReadOnlyDBTest) {
TEST_F(ColumnFamilyTest, ReadOnlyDBTest) {
Open();
CreateColumnFamiliesAndReopen({"one", "two", "three", "four"});
ASSERT_OK(Put(0, "a", "b"));
Expand Down Expand Up @@ -959,7 +958,7 @@ TEST(ColumnFamilyTest, ReadOnlyDBTest) {
ASSERT_TRUE(!s.ok());
}

TEST(ColumnFamilyTest, DontRollEmptyLogs) {
TEST_F(ColumnFamilyTest, DontRollEmptyLogs) {
Open();
CreateColumnFamiliesAndReopen({"one", "two", "three", "four"});

Expand All @@ -981,7 +980,7 @@ TEST(ColumnFamilyTest, DontRollEmptyLogs) {
Close();
}

TEST(ColumnFamilyTest, FlushStaleColumnFamilies) {
TEST_F(ColumnFamilyTest, FlushStaleColumnFamilies) {
Open();
CreateColumnFamilies({"one", "two"});
ColumnFamilyOptions default_cf, one, two;
Expand Down Expand Up @@ -1010,7 +1009,7 @@ TEST(ColumnFamilyTest, FlushStaleColumnFamilies) {
Close();
}

TEST(ColumnFamilyTest, CreateMissingColumnFamilies) {
TEST_F(ColumnFamilyTest, CreateMissingColumnFamilies) {
Status s = TryOpen({"one", "two"});
ASSERT_TRUE(!s.ok());
db_options_.create_missing_column_families = true;
Expand All @@ -1019,7 +1018,7 @@ TEST(ColumnFamilyTest, CreateMissingColumnFamilies) {
Close();
}

TEST(ColumnFamilyTest, SanitizeOptions) {
TEST_F(ColumnFamilyTest, SanitizeOptions) {
DBOptions db_options;
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
Expand All @@ -1044,5 +1043,6 @@ TEST(ColumnFamilyTest, SanitizeOptions) {
} // namespace rocksdb

int main(int argc, char** argv) {
return rocksdb::test::RunAllTests();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
7 changes: 4 additions & 3 deletions db/compact_files_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace rocksdb {

class CompactFilesTest {
class CompactFilesTest : public testing::Test {
public:
CompactFilesTest() {
env_ = Env::Default();
Expand Down Expand Up @@ -53,7 +53,7 @@ class FlushedFileCollector : public EventListener {
std::mutex mutex_;
};

TEST(CompactFilesTest, ObsoleteFiles) {
TEST_F(CompactFilesTest, ObsoleteFiles) {
Options options;
// to trigger compaction more easily
const int kWriteBufferSize = 10000;
Expand Down Expand Up @@ -100,5 +100,6 @@ TEST(CompactFilesTest, ObsoleteFiles) {
} // namespace rocksdb

int main(int argc, char** argv) {
return rocksdb::test::RunAllTests();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
9 changes: 6 additions & 3 deletions db/compaction_job_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace rocksdb {

// TODO(icanadi) Make it simpler once we mock out VersionSet
class CompactionJobTest {
class CompactionJobTest : public testing::Test {
public:
CompactionJobTest()
: env_(Env::Default()),
Expand Down Expand Up @@ -134,7 +134,7 @@ class CompactionJobTest {
std::shared_ptr<mock::MockTableFactory> mock_table_factory_;
};

TEST(CompactionJobTest, Simple) {
TEST_F(CompactionJobTest, Simple) {
auto cfd = versions_->GetColumnFamilySet()->GetDefault();

auto expected_results = CreateTwoFiles();
Expand Down Expand Up @@ -179,4 +179,7 @@ TEST(CompactionJobTest, Simple) {

} // namespace rocksdb

int main(int argc, char** argv) { return rocksdb::test::RunAllTests(); }
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
35 changes: 19 additions & 16 deletions db/compaction_picker_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CountingLogger : public Logger {
size_t log_count;
};

class CompactionPickerTest {
class CompactionPickerTest : public testing::Test {
public:
const Comparator* ucmp_;
InternalKeyComparator icmp_;
Expand Down Expand Up @@ -93,15 +93,15 @@ class CompactionPickerTest {
}
};

TEST(CompactionPickerTest, Empty) {
TEST_F(CompactionPickerTest, Empty) {
NewVersionStorage(6, kCompactionStyleLevel);
UpdateVersionStorageInfo();
std::unique_ptr<Compaction> compaction(level_compaction_picker.PickCompaction(
cf_name_, mutable_cf_options_, vstorage_.get(), &log_buffer_));
ASSERT_TRUE(compaction.get() == nullptr);
}

TEST(CompactionPickerTest, Single) {
TEST_F(CompactionPickerTest, Single) {
NewVersionStorage(6, kCompactionStyleLevel);
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
Add(0, 1U, "p", "q");
Expand All @@ -112,7 +112,7 @@ TEST(CompactionPickerTest, Single) {
ASSERT_TRUE(compaction.get() == nullptr);
}

TEST(CompactionPickerTest, Level0Trigger) {
TEST_F(CompactionPickerTest, Level0Trigger) {
NewVersionStorage(6, kCompactionStyleLevel);
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
Add(0, 1U, "150", "200");
Expand All @@ -128,7 +128,7 @@ TEST(CompactionPickerTest, Level0Trigger) {
ASSERT_EQ(2U, compaction->input(0, 1)->fd.GetNumber());
}

TEST(CompactionPickerTest, Level1Trigger) {
TEST_F(CompactionPickerTest, Level1Trigger) {
NewVersionStorage(6, kCompactionStyleLevel);
Add(1, 66U, "150", "200", 1000000000U);
UpdateVersionStorageInfo();
Expand All @@ -140,7 +140,7 @@ TEST(CompactionPickerTest, Level1Trigger) {
ASSERT_EQ(66U, compaction->input(0, 0)->fd.GetNumber());
}

TEST(CompactionPickerTest, Level1Trigger2) {
TEST_F(CompactionPickerTest, Level1Trigger2) {
NewVersionStorage(6, kCompactionStyleLevel);
Add(1, 66U, "150", "200", 1000000001U);
Add(1, 88U, "201", "300", 1000000000U);
Expand All @@ -159,7 +159,7 @@ TEST(CompactionPickerTest, Level1Trigger2) {
ASSERT_EQ(7U, compaction->input(1, 1)->fd.GetNumber());
}

TEST(CompactionPickerTest, LevelMaxScore) {
TEST_F(CompactionPickerTest, LevelMaxScore) {
NewVersionStorage(6, kCompactionStyleLevel);
mutable_cf_options_.target_file_size_base = 10000000;
mutable_cf_options_.target_file_size_multiplier = 10;
Expand All @@ -185,7 +185,7 @@ TEST(CompactionPickerTest, LevelMaxScore) {
ASSERT_EQ(7U, compaction->input(0, 0)->fd.GetNumber());
}

TEST(CompactionPickerTest, NeedsCompactionLevel) {
TEST_F(CompactionPickerTest, NeedsCompactionLevel) {
const int kLevels = 6;
const int kFileCount = 20;

Expand All @@ -210,7 +210,7 @@ TEST(CompactionPickerTest, NeedsCompactionLevel) {
}
}

TEST(CompactionPickerTest, Level0TriggerDynamic) {
TEST_F(CompactionPickerTest, Level0TriggerDynamic) {
int num_levels = ioptions_.num_levels;
ioptions_.level_compaction_dynamic_level_bytes = true;
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
Expand All @@ -232,7 +232,7 @@ TEST(CompactionPickerTest, Level0TriggerDynamic) {
ASSERT_EQ(num_levels - 1, compaction->output_level());
}

TEST(CompactionPickerTest, Level0TriggerDynamic2) {
TEST_F(CompactionPickerTest, Level0TriggerDynamic2) {
int num_levels = ioptions_.num_levels;
ioptions_.level_compaction_dynamic_level_bytes = true;
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
Expand All @@ -256,7 +256,7 @@ TEST(CompactionPickerTest, Level0TriggerDynamic2) {
ASSERT_EQ(num_levels - 2, compaction->output_level());
}

TEST(CompactionPickerTest, Level0TriggerDynamic3) {
TEST_F(CompactionPickerTest, Level0TriggerDynamic3) {
int num_levels = ioptions_.num_levels;
ioptions_.level_compaction_dynamic_level_bytes = true;
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
Expand All @@ -281,7 +281,7 @@ TEST(CompactionPickerTest, Level0TriggerDynamic3) {
ASSERT_EQ(num_levels - 3, compaction->output_level());
}

TEST(CompactionPickerTest, Level0TriggerDynamic4) {
TEST_F(CompactionPickerTest, Level0TriggerDynamic4) {
int num_levels = ioptions_.num_levels;
ioptions_.level_compaction_dynamic_level_bytes = true;
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
Expand Down Expand Up @@ -312,7 +312,7 @@ TEST(CompactionPickerTest, Level0TriggerDynamic4) {
ASSERT_EQ(num_levels - 3, compaction->output_level());
}

TEST(CompactionPickerTest, LevelTriggerDynamic4) {
TEST_F(CompactionPickerTest, LevelTriggerDynamic4) {
int num_levels = ioptions_.num_levels;
ioptions_.level_compaction_dynamic_level_bytes = true;
mutable_cf_options_.level0_file_num_compaction_trigger = 2;
Expand Down Expand Up @@ -341,7 +341,7 @@ TEST(CompactionPickerTest, LevelTriggerDynamic4) {
ASSERT_EQ(num_levels - 1, compaction->output_level());
}

TEST(CompactionPickerTest, NeedsCompactionUniversal) {
TEST_F(CompactionPickerTest, NeedsCompactionUniversal) {
NewVersionStorage(1, kCompactionStyleUniversal);
UniversalCompactionPicker universal_compaction_picker(
ioptions_, &icmp_);
Expand All @@ -360,7 +360,7 @@ TEST(CompactionPickerTest, NeedsCompactionUniversal) {
}
}

TEST(CompactionPickerTest, NeedsCompactionFIFO) {
TEST_F(CompactionPickerTest, NeedsCompactionFIFO) {
NewVersionStorage(1, kCompactionStyleFIFO);
const int kFileCount =
mutable_cf_options_.level0_file_num_compaction_trigger * 3;
Expand Down Expand Up @@ -390,4 +390,7 @@ TEST(CompactionPickerTest, NeedsCompactionFIFO) {

} // namespace rocksdb

int main(int argc, char** argv) { return rocksdb::test::RunAllTests(); }
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Loading

0 comments on commit b4b69e4

Please sign in to comment.