Skip to content

Commit f3f1397

Browse files
pwnallcmumford
authored andcommitted
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
1 parent eb4f097 commit f3f1397

File tree

3 files changed

+78
-48
lines changed

3 files changed

+78
-48
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ TESTS = \
4444
util/cache_test \
4545
util/coding_test \
4646
util/crc32c_test \
47+
util/env_posix_test \
4748
util/env_test \
4849
util/hash_test
4950

@@ -337,6 +338,9 @@ $(STATIC_OUTDIR)/db_test:db/db_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS)
337338
$(STATIC_OUTDIR)/dbformat_test:db/dbformat_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS)
338339
$(CXX) $(LDFLAGS) $(CXXFLAGS) db/dbformat_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) -o $@ $(LIBS)
339340

341+
$(STATIC_OUTDIR)/env_posix_test:util/env_posix_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS)
342+
$(CXX) $(LDFLAGS) $(CXXFLAGS) util/env_posix_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) -o $@ $(LIBS)
343+
340344
$(STATIC_OUTDIR)/env_test:util/env_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS)
341345
$(CXX) $(LDFLAGS) $(CXXFLAGS) util/env_test.cc $(STATIC_LIBOBJECTS) $(TESTHARNESS) -o $@ $(LIBS)
342346

util/env_posix_test.cc

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4+
5+
#include "leveldb/env.h"
6+
7+
#include "port/port.h"
8+
#include "util/testharness.h"
9+
#include "util/env_posix_test_helper.h"
10+
11+
namespace leveldb {
12+
13+
static const int kDelayMicros = 100000;
14+
static const int kReadOnlyFileLimit = 4;
15+
static const int kMMapLimit = 4;
16+
17+
class EnvPosixTest {
18+
public:
19+
Env* env_;
20+
EnvPosixTest() : env_(Env::Default()) { }
21+
22+
static void SetFileLimits(int read_only_file_limit, int mmap_limit) {
23+
EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit);
24+
EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit);
25+
}
26+
};
27+
28+
TEST(EnvPosixTest, TestOpenOnRead) {
29+
// Write some test data to a single file that will be opened |n| times.
30+
std::string test_dir;
31+
ASSERT_OK(env_->GetTestDirectory(&test_dir));
32+
std::string test_file = test_dir + "/open_on_read.txt";
33+
34+
FILE* f = fopen(test_file.c_str(), "w");
35+
ASSERT_TRUE(f != NULL);
36+
const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
37+
fputs(kFileData, f);
38+
fclose(f);
39+
40+
// Open test file some number above the sum of the two limits to force
41+
// open-on-read behavior of POSIX Env leveldb::RandomAccessFile.
42+
const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5;
43+
leveldb::RandomAccessFile* files[kNumFiles] = {0};
44+
for (int i = 0; i < kNumFiles; i++) {
45+
ASSERT_OK(env_->NewRandomAccessFile(test_file, &files[i]));
46+
}
47+
char scratch;
48+
Slice read_result;
49+
for (int i = 0; i < kNumFiles; i++) {
50+
ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
51+
ASSERT_EQ(kFileData[i], read_result[0]);
52+
}
53+
for (int i = 0; i < kNumFiles; i++) {
54+
delete files[i];
55+
}
56+
ASSERT_OK(env_->DeleteFile(test_file));
57+
}
58+
59+
} // namespace leveldb
60+
61+
int main(int argc, char** argv) {
62+
// All tests currently run with the same read-only file limits.
63+
leveldb::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit,
64+
leveldb::kMMapLimit);
65+
return leveldb::test::RunAllTests();
66+
}

util/env_test.cc

Lines changed: 8 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,35 @@
66

77
#include "port/port.h"
88
#include "util/testharness.h"
9-
#include "util/env_posix_test_helper.h"
109

1110
namespace leveldb {
1211

1312
static const int kDelayMicros = 100000;
1413
static const int kReadOnlyFileLimit = 4;
1514
static const int kMMapLimit = 4;
1615

17-
class EnvPosixTest {
16+
class EnvTest {
1817
private:
1918
port::Mutex mu_;
2019
std::string events_;
2120

2221
public:
2322
Env* env_;
24-
EnvPosixTest() : env_(Env::Default()) { }
25-
26-
static void SetFileLimits(int read_only_file_limit, int mmap_limit) {
27-
EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit);
28-
EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit);
29-
}
23+
EnvTest() : env_(Env::Default()) { }
3024
};
3125

3226
static void SetBool(void* ptr) {
3327
reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
3428
}
3529

36-
TEST(EnvPosixTest, RunImmediately) {
30+
TEST(EnvTest, RunImmediately) {
3731
port::AtomicPointer called (NULL);
3832
env_->Schedule(&SetBool, &called);
39-
Env::Default()->SleepForMicroseconds(kDelayMicros);
33+
env_->SleepForMicroseconds(kDelayMicros);
4034
ASSERT_TRUE(called.NoBarrier_Load() != NULL);
4135
}
4236

43-
TEST(EnvPosixTest, RunMany) {
37+
TEST(EnvTest, RunMany) {
4438
port::AtomicPointer last_id (NULL);
4539

4640
struct CB {
@@ -67,7 +61,7 @@ TEST(EnvPosixTest, RunMany) {
6761
env_->Schedule(&CB::Run, &cb3);
6862
env_->Schedule(&CB::Run, &cb4);
6963

70-
Env::Default()->SleepForMicroseconds(kDelayMicros);
64+
env_->SleepForMicroseconds(kDelayMicros);
7165
void* cur = last_id.Acquire_Load();
7266
ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
7367
}
@@ -86,7 +80,7 @@ static void ThreadBody(void* arg) {
8680
s->mu.Unlock();
8781
}
8882

89-
TEST(EnvPosixTest, StartThread) {
83+
TEST(EnvTest, StartThread) {
9084
State state;
9185
state.val = 0;
9286
state.num_running = 3;
@@ -100,47 +94,13 @@ TEST(EnvPosixTest, StartThread) {
10094
if (num == 0) {
10195
break;
10296
}
103-
Env::Default()->SleepForMicroseconds(kDelayMicros);
97+
env_->SleepForMicroseconds(kDelayMicros);
10498
}
10599
ASSERT_EQ(state.val, 3);
106100
}
107101

108-
TEST(EnvPosixTest, TestOpenOnRead) {
109-
// Write some test data to a single file that will be opened |n| times.
110-
std::string test_dir;
111-
ASSERT_OK(Env::Default()->GetTestDirectory(&test_dir));
112-
std::string test_file = test_dir + "/open_on_read.txt";
113-
114-
FILE* f = fopen(test_file.c_str(), "w");
115-
ASSERT_TRUE(f != NULL);
116-
const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
117-
fputs(kFileData, f);
118-
fclose(f);
119-
120-
// Open test file some number above the sum of the two limits to force
121-
// open-on-read behavior of POSIX Env leveldb::RandomAccessFile.
122-
const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5;
123-
leveldb::RandomAccessFile* files[kNumFiles] = {0};
124-
for (int i = 0; i < kNumFiles; i++) {
125-
ASSERT_OK(Env::Default()->NewRandomAccessFile(test_file, &files[i]));
126-
}
127-
char scratch;
128-
Slice read_result;
129-
for (int i = 0; i < kNumFiles; i++) {
130-
ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
131-
ASSERT_EQ(kFileData[i], read_result[0]);
132-
}
133-
for (int i = 0; i < kNumFiles; i++) {
134-
delete files[i];
135-
}
136-
ASSERT_OK(Env::Default()->DeleteFile(test_file));
137-
}
138-
139102
} // namespace leveldb
140103

141104
int main(int argc, char** argv) {
142-
// All tests currently run with the same read-only file limits.
143-
leveldb::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit,
144-
leveldb::kMMapLimit);
145105
return leveldb::test::RunAllTests();
146106
}

0 commit comments

Comments
 (0)