作者: 韩晨旭 10225101440 李畅 10225102463

124 line
3.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. namespace leveldb {
  8. static const int kDelayMicros = 100000;
  9. static const int kReadOnlyFileLimit = 4;
  10. static const int kMMapLimit = 4;
  11. class EnvTest {
  12. private:
  13. port::Mutex mu_;
  14. std::string events_;
  15. public:
  16. Env* env_;
  17. EnvTest() : env_(Env::Default()) { }
  18. };
  19. static void SetBool(void* ptr) {
  20. reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
  21. }
  22. TEST(EnvTest, RunImmediately) {
  23. port::AtomicPointer called (NULL);
  24. env_->Schedule(&SetBool, &called);
  25. env_->SleepForMicroseconds(kDelayMicros);
  26. ASSERT_TRUE(called.NoBarrier_Load() != NULL);
  27. }
  28. TEST(EnvTest, RunMany) {
  29. port::AtomicPointer last_id (NULL);
  30. struct CB {
  31. port::AtomicPointer* last_id_ptr; // Pointer to shared slot
  32. uintptr_t id; // Order# for the execution of this callback
  33. CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
  34. static void Run(void* v) {
  35. CB* cb = reinterpret_cast<CB*>(v);
  36. void* cur = cb->last_id_ptr->NoBarrier_Load();
  37. ASSERT_EQ(cb->id-1, reinterpret_cast<uintptr_t>(cur));
  38. cb->last_id_ptr->Release_Store(reinterpret_cast<void*>(cb->id));
  39. }
  40. };
  41. // Schedule in different order than start time
  42. CB cb1(&last_id, 1);
  43. CB cb2(&last_id, 2);
  44. CB cb3(&last_id, 3);
  45. CB cb4(&last_id, 4);
  46. env_->Schedule(&CB::Run, &cb1);
  47. env_->Schedule(&CB::Run, &cb2);
  48. env_->Schedule(&CB::Run, &cb3);
  49. env_->Schedule(&CB::Run, &cb4);
  50. env_->SleepForMicroseconds(kDelayMicros);
  51. void* cur = last_id.Acquire_Load();
  52. ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
  53. }
  54. struct State {
  55. port::Mutex mu;
  56. int val;
  57. int num_running;
  58. };
  59. static void ThreadBody(void* arg) {
  60. State* s = reinterpret_cast<State*>(arg);
  61. s->mu.Lock();
  62. s->val += 1;
  63. s->num_running -= 1;
  64. s->mu.Unlock();
  65. }
  66. TEST(EnvTest, StartThread) {
  67. State state;
  68. state.val = 0;
  69. state.num_running = 3;
  70. for (int i = 0; i < 3; i++) {
  71. env_->StartThread(&ThreadBody, &state);
  72. }
  73. while (true) {
  74. state.mu.Lock();
  75. int num = state.num_running;
  76. state.mu.Unlock();
  77. if (num == 0) {
  78. break;
  79. }
  80. env_->SleepForMicroseconds(kDelayMicros);
  81. }
  82. ASSERT_EQ(state.val, 3);
  83. }
  84. TEST(EnvTest, TestOpenNonExistentFile) {
  85. // Write some test data to a single file that will be opened |n| times.
  86. std::string test_dir;
  87. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  88. std::string non_existent_file = test_dir + "/non_existent_file";
  89. ASSERT_TRUE(!env_->FileExists(non_existent_file));
  90. RandomAccessFile* random_access_file;
  91. Status status = env_->NewRandomAccessFile(
  92. non_existent_file, &random_access_file);
  93. ASSERT_TRUE(status.IsNotFound());
  94. SequentialFile* sequential_file;
  95. status = env_->NewSequentialFile(non_existent_file, &sequential_file);
  96. ASSERT_TRUE(status.IsNotFound());
  97. }
  98. } // namespace leveldb
  99. int main(int argc, char** argv) {
  100. return leveldb::test::RunAllTests();
  101. }