1*9507f98cSAndroid Build Coastguard Worker // Copyright (c) 2018 The LevelDB Authors. All rights reserved. 2*9507f98cSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*9507f98cSAndroid Build Coastguard Worker // found in the LICENSE file. See the AUTHORS file for names of contributors. 4*9507f98cSAndroid Build Coastguard Worker 5*9507f98cSAndroid Build Coastguard Worker #include "gtest/gtest.h" 6*9507f98cSAndroid Build Coastguard Worker #include "leveldb/env.h" 7*9507f98cSAndroid Build Coastguard Worker #include "port/port.h" 8*9507f98cSAndroid Build Coastguard Worker #include "util/env_windows_test_helper.h" 9*9507f98cSAndroid Build Coastguard Worker #include "util/testutil.h" 10*9507f98cSAndroid Build Coastguard Worker 11*9507f98cSAndroid Build Coastguard Worker namespace leveldb { 12*9507f98cSAndroid Build Coastguard Worker 13*9507f98cSAndroid Build Coastguard Worker static const int kMMapLimit = 4; 14*9507f98cSAndroid Build Coastguard Worker 15*9507f98cSAndroid Build Coastguard Worker class EnvWindowsTest : public testing::Test { 16*9507f98cSAndroid Build Coastguard Worker public: SetFileLimits(int mmap_limit)17*9507f98cSAndroid Build Coastguard Worker static void SetFileLimits(int mmap_limit) { 18*9507f98cSAndroid Build Coastguard Worker EnvWindowsTestHelper::SetReadOnlyMMapLimit(mmap_limit); 19*9507f98cSAndroid Build Coastguard Worker } 20*9507f98cSAndroid Build Coastguard Worker EnvWindowsTest()21*9507f98cSAndroid Build Coastguard Worker EnvWindowsTest() : env_(Env::Default()) {} 22*9507f98cSAndroid Build Coastguard Worker 23*9507f98cSAndroid Build Coastguard Worker Env* env_; 24*9507f98cSAndroid Build Coastguard Worker }; 25*9507f98cSAndroid Build Coastguard Worker TEST_F(EnvWindowsTest,TestOpenOnRead)26*9507f98cSAndroid Build Coastguard WorkerTEST_F(EnvWindowsTest, TestOpenOnRead) { 27*9507f98cSAndroid Build Coastguard Worker // Write some test data to a single file that will be opened |n| times. 28*9507f98cSAndroid Build Coastguard Worker std::string test_dir; 29*9507f98cSAndroid Build Coastguard Worker ASSERT_LEVELDB_OK(env_->GetTestDirectory(&test_dir)); 30*9507f98cSAndroid Build Coastguard Worker std::string test_file = test_dir + "/open_on_read.txt"; 31*9507f98cSAndroid Build Coastguard Worker 32*9507f98cSAndroid Build Coastguard Worker FILE* f = std::fopen(test_file.c_str(), "w"); 33*9507f98cSAndroid Build Coastguard Worker ASSERT_TRUE(f != nullptr); 34*9507f98cSAndroid Build Coastguard Worker const char kFileData[] = "abcdefghijklmnopqrstuvwxyz"; 35*9507f98cSAndroid Build Coastguard Worker fputs(kFileData, f); 36*9507f98cSAndroid Build Coastguard Worker std::fclose(f); 37*9507f98cSAndroid Build Coastguard Worker 38*9507f98cSAndroid Build Coastguard Worker // Open test file some number above the sum of the two limits to force 39*9507f98cSAndroid Build Coastguard Worker // leveldb::WindowsEnv to switch from mapping the file into memory 40*9507f98cSAndroid Build Coastguard Worker // to basic file reading. 41*9507f98cSAndroid Build Coastguard Worker const int kNumFiles = kMMapLimit + 5; 42*9507f98cSAndroid Build Coastguard Worker leveldb::RandomAccessFile* files[kNumFiles] = {0}; 43*9507f98cSAndroid Build Coastguard Worker for (int i = 0; i < kNumFiles; i++) { 44*9507f98cSAndroid Build Coastguard Worker ASSERT_LEVELDB_OK(env_->NewRandomAccessFile(test_file, &files[i])); 45*9507f98cSAndroid Build Coastguard Worker } 46*9507f98cSAndroid Build Coastguard Worker char scratch; 47*9507f98cSAndroid Build Coastguard Worker Slice read_result; 48*9507f98cSAndroid Build Coastguard Worker for (int i = 0; i < kNumFiles; i++) { 49*9507f98cSAndroid Build Coastguard Worker ASSERT_LEVELDB_OK(files[i]->Read(i, 1, &read_result, &scratch)); 50*9507f98cSAndroid Build Coastguard Worker ASSERT_EQ(kFileData[i], read_result[0]); 51*9507f98cSAndroid Build Coastguard Worker } 52*9507f98cSAndroid Build Coastguard Worker for (int i = 0; i < kNumFiles; i++) { 53*9507f98cSAndroid Build Coastguard Worker delete files[i]; 54*9507f98cSAndroid Build Coastguard Worker } 55*9507f98cSAndroid Build Coastguard Worker ASSERT_LEVELDB_OK(env_->RemoveFile(test_file)); 56*9507f98cSAndroid Build Coastguard Worker } 57*9507f98cSAndroid Build Coastguard Worker 58*9507f98cSAndroid Build Coastguard Worker } // namespace leveldb 59*9507f98cSAndroid Build Coastguard Worker 60