作者: 谢瑞阳 10225101483 徐翔宇 10225101535
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
2.0 KiB

  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. #include "leveldb/env.h"
  5. #include "port/port.h"
  6. #include "util/env_posix_test_helper.h"
  7. #include "util/testharness.h"
  8. namespace leveldb {
  9. static const int kReadOnlyFileLimit = 4;
  10. static const int kMMapLimit = 4;
  11. class EnvPosixTest {
  12. public:
  13. Env* env_;
  14. EnvPosixTest() : env_(Env::Default()) {}
  15. static void SetFileLimits(int read_only_file_limit, int mmap_limit) {
  16. EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit);
  17. EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit);
  18. }
  19. };
  20. TEST(EnvPosixTest, TestOpenOnRead) {
  21. // Write some test data to a single file that will be opened |n| times.
  22. std::string test_dir;
  23. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  24. std::string test_file = test_dir + "/open_on_read.txt";
  25. FILE* f = fopen(test_file.c_str(), "w");
  26. ASSERT_TRUE(f != nullptr);
  27. const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
  28. fputs(kFileData, f);
  29. fclose(f);
  30. // Open test file some number above the sum of the two limits to force
  31. // open-on-read behavior of POSIX Env leveldb::RandomAccessFile.
  32. const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5;
  33. leveldb::RandomAccessFile* files[kNumFiles] = {0};
  34. for (int i = 0; i < kNumFiles; i++) {
  35. ASSERT_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_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_OK(env_->DeleteFile(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::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit,
  52. leveldb::kMMapLimit);
  53. return leveldb::test::RunAllTests();
  54. }