作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

66 rindas
2.1 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/testharness.h"
  7. #include "util/env_posix_test_helper.h"
  8. namespace leveldb {
  9. static const int kDelayMicros = 100000;
  10. static const int kReadOnlyFileLimit = 4;
  11. static const int kMMapLimit = 4;
  12. class EnvPosixTest {
  13. public:
  14. Env* env_;
  15. EnvPosixTest() : env_(Env::Default()) { }
  16. static void SetFileLimits(int read_only_file_limit, int mmap_limit) {
  17. EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit);
  18. EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit);
  19. }
  20. };
  21. TEST(EnvPosixTest, TestOpenOnRead) {
  22. // Write some test data to a single file that will be opened |n| times.
  23. std::string test_dir;
  24. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  25. std::string test_file = test_dir + "/open_on_read.txt";
  26. FILE* f = fopen(test_file.c_str(), "w");
  27. ASSERT_TRUE(f != NULL);
  28. const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
  29. fputs(kFileData, f);
  30. fclose(f);
  31. // Open test file some number above the sum of the two limits to force
  32. // open-on-read behavior of POSIX Env leveldb::RandomAccessFile.
  33. const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5;
  34. leveldb::RandomAccessFile* files[kNumFiles] = {0};
  35. for (int i = 0; i < kNumFiles; i++) {
  36. ASSERT_OK(env_->NewRandomAccessFile(test_file, &files[i]));
  37. }
  38. char scratch;
  39. Slice read_result;
  40. for (int i = 0; i < kNumFiles; i++) {
  41. ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
  42. ASSERT_EQ(kFileData[i], read_result[0]);
  43. }
  44. for (int i = 0; i < kNumFiles; i++) {
  45. delete files[i];
  46. }
  47. ASSERT_OK(env_->DeleteFile(test_file));
  48. }
  49. } // namespace leveldb
  50. int main(int argc, char** argv) {
  51. // All tests currently run with the same read-only file limits.
  52. leveldb::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit,
  53. leveldb::kMMapLimit);
  54. return leveldb::test::RunAllTests();
  55. }