Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

223 Zeilen
6.4 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 <algorithm>
  6. #include "port/port.h"
  7. #include "port/thread_annotations.h"
  8. #include "util/mutexlock.h"
  9. #include "util/testharness.h"
  10. #include "util/testutil.h"
  11. namespace leveldb {
  12. static const int kDelayMicros = 100000;
  13. static const int kReadOnlyFileLimit = 4;
  14. static const int kMMapLimit = 4;
  15. class EnvTest {
  16. public:
  17. Env* env_;
  18. EnvTest() : env_(Env::Default()) { }
  19. };
  20. static void SetBool(void* ptr) {
  21. reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
  22. }
  23. TEST(EnvTest, ReadWrite) {
  24. Random rnd(test::RandomSeed());
  25. // Get file to use for testing.
  26. std::string test_dir;
  27. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  28. std::string test_file_name = test_dir + "/open_on_read.txt";
  29. WritableFile* writable_file;
  30. ASSERT_OK(env_->NewWritableFile(test_file_name, &writable_file));
  31. // Fill a file with data generated via a sequence of randomly sized writes.
  32. static const size_t kDataSize = 10 * 1048576;
  33. std::string data;
  34. while (data.size() < kDataSize) {
  35. int len = rnd.Skewed(18); // Up to 2^18 - 1, but typically much smaller
  36. std::string r;
  37. test::RandomString(&rnd, len, &r);
  38. ASSERT_OK(writable_file->Append(r));
  39. data += r;
  40. if (rnd.OneIn(10)) {
  41. ASSERT_OK(writable_file->Flush());
  42. }
  43. }
  44. ASSERT_OK(writable_file->Sync());
  45. ASSERT_OK(writable_file->Close());
  46. delete writable_file;
  47. // Read all data using a sequence of randomly sized reads.
  48. SequentialFile* sequential_file;
  49. ASSERT_OK(env_->NewSequentialFile(test_file_name, &sequential_file));
  50. std::string read_result;
  51. std::string scratch;
  52. while (read_result.size() < data.size()) {
  53. int len = std::min<int>(rnd.Skewed(18), data.size() - read_result.size());
  54. scratch.resize(std::max(len, 1)); // at least 1 so &scratch[0] is legal
  55. Slice read;
  56. ASSERT_OK(sequential_file->Read(len, &read, &scratch[0]));
  57. if (len > 0) {
  58. ASSERT_GT(read.size(), 0);
  59. }
  60. ASSERT_LE(read.size(), len);
  61. read_result.append(read.data(), read.size());
  62. }
  63. ASSERT_EQ(read_result, data);
  64. delete sequential_file;
  65. }
  66. TEST(EnvTest, RunImmediately) {
  67. port::AtomicPointer called(nullptr);
  68. env_->Schedule(&SetBool, &called);
  69. env_->SleepForMicroseconds(kDelayMicros);
  70. ASSERT_TRUE(called.NoBarrier_Load() != nullptr);
  71. }
  72. TEST(EnvTest, RunMany) {
  73. port::AtomicPointer last_id(nullptr);
  74. struct CB {
  75. port::AtomicPointer* last_id_ptr; // Pointer to shared slot
  76. uintptr_t id; // Order# for the execution of this callback
  77. CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
  78. static void Run(void* v) {
  79. CB* cb = reinterpret_cast<CB*>(v);
  80. void* cur = cb->last_id_ptr->NoBarrier_Load();
  81. ASSERT_EQ(cb->id-1, reinterpret_cast<uintptr_t>(cur));
  82. cb->last_id_ptr->Release_Store(reinterpret_cast<void*>(cb->id));
  83. }
  84. };
  85. // Schedule in different order than start time
  86. CB cb1(&last_id, 1);
  87. CB cb2(&last_id, 2);
  88. CB cb3(&last_id, 3);
  89. CB cb4(&last_id, 4);
  90. env_->Schedule(&CB::Run, &cb1);
  91. env_->Schedule(&CB::Run, &cb2);
  92. env_->Schedule(&CB::Run, &cb3);
  93. env_->Schedule(&CB::Run, &cb4);
  94. env_->SleepForMicroseconds(kDelayMicros);
  95. void* cur = last_id.Acquire_Load();
  96. ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
  97. }
  98. struct State {
  99. port::Mutex mu;
  100. int val GUARDED_BY(mu);
  101. int num_running GUARDED_BY(mu);
  102. State(int val, int num_running) : val(val), num_running(num_running) { }
  103. };
  104. static void ThreadBody(void* arg) {
  105. State* s = reinterpret_cast<State*>(arg);
  106. s->mu.Lock();
  107. s->val += 1;
  108. s->num_running -= 1;
  109. s->mu.Unlock();
  110. }
  111. TEST(EnvTest, StartThread) {
  112. State state(0, 3);
  113. for (int i = 0; i < 3; i++) {
  114. env_->StartThread(&ThreadBody, &state);
  115. }
  116. while (true) {
  117. state.mu.Lock();
  118. int num = state.num_running;
  119. state.mu.Unlock();
  120. if (num == 0) {
  121. break;
  122. }
  123. env_->SleepForMicroseconds(kDelayMicros);
  124. }
  125. MutexLock l(&state.mu);
  126. ASSERT_EQ(state.val, 3);
  127. }
  128. TEST(EnvTest, TestOpenNonExistentFile) {
  129. // Write some test data to a single file that will be opened |n| times.
  130. std::string test_dir;
  131. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  132. std::string non_existent_file = test_dir + "/non_existent_file";
  133. ASSERT_TRUE(!env_->FileExists(non_existent_file));
  134. RandomAccessFile* random_access_file;
  135. Status status = env_->NewRandomAccessFile(
  136. non_existent_file, &random_access_file);
  137. ASSERT_TRUE(status.IsNotFound());
  138. SequentialFile* sequential_file;
  139. status = env_->NewSequentialFile(non_existent_file, &sequential_file);
  140. ASSERT_TRUE(status.IsNotFound());
  141. }
  142. TEST(EnvTest, ReopenWritableFile) {
  143. std::string test_dir;
  144. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  145. std::string test_file_name = test_dir + "/reopen_writable_file.txt";
  146. env_->DeleteFile(test_file_name);
  147. WritableFile* writable_file;
  148. ASSERT_OK(env_->NewWritableFile(test_file_name, &writable_file));
  149. std::string data("hello world!");
  150. ASSERT_OK(writable_file->Append(data));
  151. ASSERT_OK(writable_file->Close());
  152. delete writable_file;
  153. ASSERT_OK(env_->NewWritableFile(test_file_name, &writable_file));
  154. data = "42";
  155. ASSERT_OK(writable_file->Append(data));
  156. ASSERT_OK(writable_file->Close());
  157. delete writable_file;
  158. ASSERT_OK(ReadFileToString(env_, test_file_name, &data));
  159. ASSERT_EQ(std::string("42"), data);
  160. env_->DeleteFile(test_file_name);
  161. }
  162. TEST(EnvTest, ReopenAppendableFile) {
  163. std::string test_dir;
  164. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  165. std::string test_file_name = test_dir + "/reopen_appendable_file.txt";
  166. env_->DeleteFile(test_file_name);
  167. WritableFile* appendable_file;
  168. ASSERT_OK(env_->NewAppendableFile(test_file_name, &appendable_file));
  169. std::string data("hello world!");
  170. ASSERT_OK(appendable_file->Append(data));
  171. ASSERT_OK(appendable_file->Close());
  172. delete appendable_file;
  173. ASSERT_OK(env_->NewAppendableFile(test_file_name, &appendable_file));
  174. data = "42";
  175. ASSERT_OK(appendable_file->Append(data));
  176. ASSERT_OK(appendable_file->Close());
  177. delete appendable_file;
  178. ASSERT_OK(ReadFileToString(env_, test_file_name, &data));
  179. ASSERT_EQ(std::string("hello world!42"), data);
  180. env_->DeleteFile(test_file_name);
  181. }
  182. } // namespace leveldb
  183. int main(int argc, char** argv) {
  184. return leveldb::test::RunAllTests();
  185. }