小组成员:10215300402-朱维清 & 10222140408 谷杰
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

146 rader
4.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/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. private:
  14. port::Mutex mu_;
  15. std::string events_;
  16. public:
  17. Env* env_;
  18. EnvPosixTest() : env_(Env::Default()) { }
  19. static void SetFileLimits(int read_only_file_limit, int mmap_limit) {
  20. EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit);
  21. EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit);
  22. }
  23. };
  24. static void SetBool(void* ptr) {
  25. reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
  26. }
  27. TEST(EnvPosixTest, RunImmediately) {
  28. port::AtomicPointer called (NULL);
  29. env_->Schedule(&SetBool, &called);
  30. Env::Default()->SleepForMicroseconds(kDelayMicros);
  31. ASSERT_TRUE(called.NoBarrier_Load() != NULL);
  32. }
  33. TEST(EnvPosixTest, RunMany) {
  34. port::AtomicPointer last_id (NULL);
  35. struct CB {
  36. port::AtomicPointer* last_id_ptr; // Pointer to shared slot
  37. uintptr_t id; // Order# for the execution of this callback
  38. CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
  39. static void Run(void* v) {
  40. CB* cb = reinterpret_cast<CB*>(v);
  41. void* cur = cb->last_id_ptr->NoBarrier_Load();
  42. ASSERT_EQ(cb->id-1, reinterpret_cast<uintptr_t>(cur));
  43. cb->last_id_ptr->Release_Store(reinterpret_cast<void*>(cb->id));
  44. }
  45. };
  46. // Schedule in different order than start time
  47. CB cb1(&last_id, 1);
  48. CB cb2(&last_id, 2);
  49. CB cb3(&last_id, 3);
  50. CB cb4(&last_id, 4);
  51. env_->Schedule(&CB::Run, &cb1);
  52. env_->Schedule(&CB::Run, &cb2);
  53. env_->Schedule(&CB::Run, &cb3);
  54. env_->Schedule(&CB::Run, &cb4);
  55. Env::Default()->SleepForMicroseconds(kDelayMicros);
  56. void* cur = last_id.Acquire_Load();
  57. ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
  58. }
  59. struct State {
  60. port::Mutex mu;
  61. int val;
  62. int num_running;
  63. };
  64. static void ThreadBody(void* arg) {
  65. State* s = reinterpret_cast<State*>(arg);
  66. s->mu.Lock();
  67. s->val += 1;
  68. s->num_running -= 1;
  69. s->mu.Unlock();
  70. }
  71. TEST(EnvPosixTest, StartThread) {
  72. State state;
  73. state.val = 0;
  74. state.num_running = 3;
  75. for (int i = 0; i < 3; i++) {
  76. env_->StartThread(&ThreadBody, &state);
  77. }
  78. while (true) {
  79. state.mu.Lock();
  80. int num = state.num_running;
  81. state.mu.Unlock();
  82. if (num == 0) {
  83. break;
  84. }
  85. Env::Default()->SleepForMicroseconds(kDelayMicros);
  86. }
  87. ASSERT_EQ(state.val, 3);
  88. }
  89. TEST(EnvPosixTest, TestOpenOnRead) {
  90. // Write some test data to a single file that will be opened |n| times.
  91. std::string test_dir;
  92. ASSERT_OK(Env::Default()->GetTestDirectory(&test_dir));
  93. std::string test_file = test_dir + "/open_on_read.txt";
  94. FILE* f = fopen(test_file.c_str(), "w");
  95. ASSERT_TRUE(f != NULL);
  96. const char kFileData[] = "abcdefghijklmnopqrstuvwxyz";
  97. fputs(kFileData, f);
  98. fclose(f);
  99. // Open test file some number above the sum of the two limits to force
  100. // open-on-read behavior of POSIX Env leveldb::RandomAccessFile.
  101. const int kNumFiles = kReadOnlyFileLimit + kMMapLimit + 5;
  102. leveldb::RandomAccessFile* files[kNumFiles] = {0};
  103. for (int i = 0; i < kNumFiles; i++) {
  104. ASSERT_OK(Env::Default()->NewRandomAccessFile(test_file, &files[i]));
  105. }
  106. char scratch;
  107. Slice read_result;
  108. for (int i = 0; i < kNumFiles; i++) {
  109. ASSERT_OK(files[i]->Read(i, 1, &read_result, &scratch));
  110. ASSERT_EQ(kFileData[i], read_result[0]);
  111. }
  112. for (int i = 0; i < kNumFiles; i++) {
  113. delete files[i];
  114. }
  115. ASSERT_OK(Env::Default()->DeleteFile(test_file));
  116. }
  117. } // namespace leveldb
  118. int main(int argc, char** argv) {
  119. // All tests currently run with the same read-only file limits.
  120. leveldb::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit,
  121. leveldb::kMMapLimit);
  122. return leveldb::test::RunAllTests();
  123. }