LevelDB project 1 10225501460 林子骥 10211900416 郭夏辉
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 regels
2.0 KiB

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