1 // Copyright (c) 2018 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/env_windows_test_helper.h"
9 #include "util/testharness.h"
10 11 namespace leveldb {
12 13 static const int kMMapLimit = 4;
14 15 class EnvWindowsTest {
16 public:
17 static void SetFileLimits(int mmap_limit) {
18 EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit);
19 }
20 21 EnvWindowsTest() : env_(Env::Default()) {}
22 23 Env* env_;
24 };
25 26 TEST(EnvWindowsTest, TestOpenOnRead) {
27 // Write some test data to a single file that will be opened |n| times.
28 std::string test_dir;
29 ASSERT_OK(env_->GetTestDirectory(&test_dir));
30 std::string test_file = test_dir + "/open_on_read.txt";
31 32 FILE* f = fopen(test_file.c_str(), "w");
33 ASSERT_TRUE(f != nullptr);
34 const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
35 fputs(kFileData, f);
36 fclose(f);
37 38 // Open test file some number above the sum of the two limits to force
39 // leveldb::WindowsEnv to switch from mapping the file into memory
40 // to basic file reading.
41 const int kNumFiles = kMMapLimit + 5;
42 leveldb::RandomAccessFile* files[kNumFiles] = {0};
43 for (int i = 0; i < kNumFiles; i++) {
44 ASSERT_OK(env_->NewRandomAccessFile(test_file, &files[i]));
45 }
46 char scratch;
47 Slice read_result;
48 for (int i = 0; i < kNumFiles; i++) {
49 ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
50 ASSERT_EQ(kFileData[i], read_result[0]);
51 }
52 for (int i = 0; i < kNumFiles; i++) {
53 delete files[i];
54 }
55 ASSERT_OK(env_->DeleteFile(test_file));
56 }
57 58 } // namespace leveldb
59 60 int main(int argc, char** argv) {
61 // All tests currently run with the same read-only file limits.
62 leveldb::EnvWindowsTest::SetFileLimits(leveldb::kMMapLimit);
63 return leveldb::test::RunAllTests();
64 }
65