2019-03-02 05:12:01 +08:00
|
|
|
// Copyright (c) 2018 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.
|
|
|
|
|
2019-11-26 01:29:06 +08:00
|
|
|
#include "gtest/gtest.h"
|
2019-03-02 05:12:01 +08:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/env_windows_test_helper.h"
|
2019-11-22 05:09:53 +08:00
|
|
|
#include "util/testutil.h"
|
2019-03-02 05:12:01 +08:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
static const int kMMapLimit = 4;
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
class EnvWindowsTest : public testing::Test {
|
2019-03-02 05:12:01 +08:00
|
|
|
public:
|
|
|
|
static void SetFileLimits(int mmap_limit) {
|
|
|
|
EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit);
|
|
|
|
}
|
2019-05-04 00:31:18 +08:00
|
|
|
|
|
|
|
EnvWindowsTest() : env_(Env::Default()) {}
|
|
|
|
|
|
|
|
Env* env_;
|
2019-03-02 05:12:01 +08:00
|
|
|
};
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
TEST_F(EnvWindowsTest, TestOpenOnRead) {
|
2019-03-02 05:12:01 +08:00
|
|
|
// Write some test data to a single file that will be opened |n| times.
|
|
|
|
std::string test_dir;
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir));
|
2019-03-02 05:12:01 +08:00
|
|
|
std::string test_file = test_dir + "/open_on_read.txt";
|
|
|
|
|
2020-04-30 06:31:41 +08:00
|
|
|
FILE* f = std::fopen(test_file.c_str(), "w");
|
2019-03-02 05:12:01 +08:00
|
|
|
ASSERT_TRUE(f != nullptr);
|
|
|
|
const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
fputs(kFileData, f);
|
2020-04-30 06:31:41 +08:00
|
|
|
std::fclose(f);
|
2019-03-02 05:12:01 +08:00
|
|
|
|
|
|
|
// Open test file some number above the sum of the two limits to force
|
|
|
|
// leveldb::WindowsEnv to switch from mapping the file into memory
|
|
|
|
// to basic file reading.
|
|
|
|
const int kNumFiles = kMMapLimit + 5;
|
|
|
|
leveldb::RandomAccessFile* files[kNumFiles] = {0};
|
|
|
|
for (int i = 0; i < kNumFiles; i++) {
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(test_file, &files[i]));
|
2019-03-02 05:12:01 +08:00
|
|
|
}
|
|
|
|
char scratch;
|
|
|
|
Slice read_result;
|
|
|
|
for (int i = 0; i < kNumFiles; i++) {
|
2019-11-22 05:09:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(files[i]->Read(i, 1, &read_result, &scratch));
|
2019-03-02 05:12:01 +08:00
|
|
|
ASSERT_EQ(kFileData[i], read_result[0]);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < kNumFiles; i++) {
|
|
|
|
delete files[i];
|
|
|
|
}
|
Add Env::Remove{File,Dir} which obsolete Env::Delete{File,Dir}.
The "DeleteFile" method name causes pain for Windows developers, because
<windows.h> #defines a DeleteFile macro to DeleteFileW or DeleteFileA.
Current code uses workarounds, like #undefining DeleteFile everywhere an
Env is declared, implemented, or used.
This CL removes the need for workarounds by renaming Env::DeleteFile to
Env::RemoveFile. For consistency, Env::DeleteDir is also renamed to
Env::RemoveDir. A few internal methods are also renamed for consistency.
Software that supports Windows is expected to migrate any Env
implementations and usage to Remove{File,Dir}, and never use the name
Env::Delete{File,Dir} in its code.
The renaming is done in a backwards-compatible way, at the risk of
making it slightly more difficult to build a new correct Env
implementation. The backwards compatibility is achieved using the
following hacks:
1) Env::Remove{File,Dir} methods are added, with a default
implementation that calls into Env::Delete{File,Dir}. This makes old
Env implementations compatible with code that calls into the updated
API.
2) The Env::Delete{File,Dir} methods are no longer pure virtuals.
Instead, they gain a default implementation that calls into
Env::Remove{File,Dir}. This makes updated Env implementations
compatible with code that calls into the old API.
The cost of this approach is that it's possible to write an Env without
overriding either Rename{File,Dir} or Delete{File,Dir}, without getting
a compiler warning. However, attempting to run the test suite will
immediately fail with an infinite call stack ending in
{Remove,Delete}{File,Dir}, making developers aware of the problem.
PiperOrigin-RevId: 288710907
2020-01-09 01:14:53 +08:00
|
|
|
ASSERT_LEVELDB_OK(env_->RemoveFile(test_file));
|
2019-03-02 05:12:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace leveldb
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
// All tests currently run with the same read-only file limits.
|
|
|
|
leveldb::EnvWindowsTest::SetFileLimits(leveldb::kMMapLimit);
|
2019-11-22 05:09:53 +08:00
|
|
|
testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2019-03-02 05:12:01 +08:00
|
|
|
}
|