env_mirror_test.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  2. // Copyright (c) 2015, Red Hat, Inc. All rights reserved.
  3. // This source code is licensed under both the GPLv2 (found in the
  4. // COPYING file in the root directory) and Apache 2.0 License
  5. // (found in the LICENSE.Apache file in the root directory).
  6. #ifndef ROCKSDB_LITE
  7. #include "rocksdb/utilities/env_mirror.h"
  8. #include "env/mock_env.h"
  9. #include "test_util/testharness.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. class EnvMirrorTest : public testing::Test {
  12. public:
  13. Env* default_;
  14. MockEnv* a_, *b_;
  15. EnvMirror* env_;
  16. const EnvOptions soptions_;
  17. EnvMirrorTest()
  18. : default_(Env::Default()),
  19. a_(new MockEnv(default_)),
  20. b_(new MockEnv(default_)),
  21. env_(new EnvMirror(a_, b_)) {}
  22. ~EnvMirrorTest() {
  23. delete env_;
  24. delete a_;
  25. delete b_;
  26. }
  27. };
  28. TEST_F(EnvMirrorTest, Basics) {
  29. uint64_t file_size;
  30. std::unique_ptr<WritableFile> writable_file;
  31. std::vector<std::string> children;
  32. ASSERT_OK(env_->CreateDir("/dir"));
  33. // Check that the directory is empty.
  34. ASSERT_EQ(Status::NotFound(), env_->FileExists("/dir/non_existent"));
  35. ASSERT_TRUE(!env_->GetFileSize("/dir/non_existent", &file_size).ok());
  36. ASSERT_OK(env_->GetChildren("/dir", &children));
  37. ASSERT_EQ(0U, children.size());
  38. // Create a file.
  39. ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
  40. writable_file.reset();
  41. // Check that the file exists.
  42. ASSERT_OK(env_->FileExists("/dir/f"));
  43. ASSERT_OK(a_->FileExists("/dir/f"));
  44. ASSERT_OK(b_->FileExists("/dir/f"));
  45. ASSERT_OK(env_->GetFileSize("/dir/f", &file_size));
  46. ASSERT_EQ(0U, file_size);
  47. ASSERT_OK(env_->GetChildren("/dir", &children));
  48. ASSERT_EQ(1U, children.size());
  49. ASSERT_EQ("f", children[0]);
  50. ASSERT_OK(a_->GetChildren("/dir", &children));
  51. ASSERT_EQ(1U, children.size());
  52. ASSERT_EQ("f", children[0]);
  53. ASSERT_OK(b_->GetChildren("/dir", &children));
  54. ASSERT_EQ(1U, children.size());
  55. ASSERT_EQ("f", children[0]);
  56. // Write to the file.
  57. ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
  58. ASSERT_OK(writable_file->Append("abc"));
  59. writable_file.reset();
  60. // Check for expected size.
  61. ASSERT_OK(env_->GetFileSize("/dir/f", &file_size));
  62. ASSERT_EQ(3U, file_size);
  63. ASSERT_OK(a_->GetFileSize("/dir/f", &file_size));
  64. ASSERT_EQ(3U, file_size);
  65. ASSERT_OK(b_->GetFileSize("/dir/f", &file_size));
  66. ASSERT_EQ(3U, file_size);
  67. // Check that renaming works.
  68. ASSERT_TRUE(!env_->RenameFile("/dir/non_existent", "/dir/g").ok());
  69. ASSERT_OK(env_->RenameFile("/dir/f", "/dir/g"));
  70. ASSERT_EQ(Status::NotFound(), env_->FileExists("/dir/f"));
  71. ASSERT_OK(env_->FileExists("/dir/g"));
  72. ASSERT_OK(env_->GetFileSize("/dir/g", &file_size));
  73. ASSERT_EQ(3U, file_size);
  74. ASSERT_OK(a_->FileExists("/dir/g"));
  75. ASSERT_OK(a_->GetFileSize("/dir/g", &file_size));
  76. ASSERT_EQ(3U, file_size);
  77. ASSERT_OK(b_->FileExists("/dir/g"));
  78. ASSERT_OK(b_->GetFileSize("/dir/g", &file_size));
  79. ASSERT_EQ(3U, file_size);
  80. // Check that opening non-existent file fails.
  81. std::unique_ptr<SequentialFile> seq_file;
  82. std::unique_ptr<RandomAccessFile> rand_file;
  83. ASSERT_TRUE(
  84. !env_->NewSequentialFile("/dir/non_existent", &seq_file, soptions_).ok());
  85. ASSERT_TRUE(!seq_file);
  86. ASSERT_TRUE(!env_->NewRandomAccessFile("/dir/non_existent", &rand_file,
  87. soptions_).ok());
  88. ASSERT_TRUE(!rand_file);
  89. // Check that deleting works.
  90. ASSERT_TRUE(!env_->DeleteFile("/dir/non_existent").ok());
  91. ASSERT_OK(env_->DeleteFile("/dir/g"));
  92. ASSERT_EQ(Status::NotFound(), env_->FileExists("/dir/g"));
  93. ASSERT_OK(env_->GetChildren("/dir", &children));
  94. ASSERT_EQ(0U, children.size());
  95. ASSERT_OK(env_->DeleteDir("/dir"));
  96. }
  97. TEST_F(EnvMirrorTest, ReadWrite) {
  98. std::unique_ptr<WritableFile> writable_file;
  99. std::unique_ptr<SequentialFile> seq_file;
  100. std::unique_ptr<RandomAccessFile> rand_file;
  101. Slice result;
  102. char scratch[100];
  103. ASSERT_OK(env_->CreateDir("/dir"));
  104. ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
  105. ASSERT_OK(writable_file->Append("hello "));
  106. ASSERT_OK(writable_file->Append("world"));
  107. writable_file.reset();
  108. // Read sequentially.
  109. ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file, soptions_));
  110. ASSERT_OK(seq_file->Read(5, &result, scratch)); // Read "hello".
  111. ASSERT_EQ(0, result.compare("hello"));
  112. ASSERT_OK(seq_file->Skip(1));
  113. ASSERT_OK(seq_file->Read(1000, &result, scratch)); // Read "world".
  114. ASSERT_EQ(0, result.compare("world"));
  115. ASSERT_OK(seq_file->Read(1000, &result, scratch)); // Try reading past EOF.
  116. ASSERT_EQ(0U, result.size());
  117. ASSERT_OK(seq_file->Skip(100)); // Try to skip past end of file.
  118. ASSERT_OK(seq_file->Read(1000, &result, scratch));
  119. ASSERT_EQ(0U, result.size());
  120. // Random reads.
  121. ASSERT_OK(env_->NewRandomAccessFile("/dir/f", &rand_file, soptions_));
  122. ASSERT_OK(rand_file->Read(6, 5, &result, scratch)); // Read "world".
  123. ASSERT_EQ(0, result.compare("world"));
  124. ASSERT_OK(rand_file->Read(0, 5, &result, scratch)); // Read "hello".
  125. ASSERT_EQ(0, result.compare("hello"));
  126. ASSERT_OK(rand_file->Read(10, 100, &result, scratch)); // Read "d".
  127. ASSERT_EQ(0, result.compare("d"));
  128. // Too high offset.
  129. ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok());
  130. }
  131. TEST_F(EnvMirrorTest, Locks) {
  132. FileLock* lock;
  133. // These are no-ops, but we test they return success.
  134. ASSERT_OK(env_->LockFile("some file", &lock));
  135. ASSERT_OK(env_->UnlockFile(lock));
  136. }
  137. TEST_F(EnvMirrorTest, Misc) {
  138. std::string test_dir;
  139. ASSERT_OK(env_->GetTestDirectory(&test_dir));
  140. ASSERT_TRUE(!test_dir.empty());
  141. std::unique_ptr<WritableFile> writable_file;
  142. ASSERT_OK(env_->NewWritableFile("/a/b", &writable_file, soptions_));
  143. // These are no-ops, but we test they return success.
  144. ASSERT_OK(writable_file->Sync());
  145. ASSERT_OK(writable_file->Flush());
  146. ASSERT_OK(writable_file->Close());
  147. writable_file.reset();
  148. }
  149. TEST_F(EnvMirrorTest, LargeWrite) {
  150. const size_t kWriteSize = 300 * 1024;
  151. char* scratch = new char[kWriteSize * 2];
  152. std::string write_data;
  153. for (size_t i = 0; i < kWriteSize; ++i) {
  154. write_data.append(1, static_cast<char>(i));
  155. }
  156. std::unique_ptr<WritableFile> writable_file;
  157. ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
  158. ASSERT_OK(writable_file->Append("foo"));
  159. ASSERT_OK(writable_file->Append(write_data));
  160. writable_file.reset();
  161. std::unique_ptr<SequentialFile> seq_file;
  162. Slice result;
  163. ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file, soptions_));
  164. ASSERT_OK(seq_file->Read(3, &result, scratch)); // Read "foo".
  165. ASSERT_EQ(0, result.compare("foo"));
  166. size_t read = 0;
  167. std::string read_data;
  168. while (read < kWriteSize) {
  169. ASSERT_OK(seq_file->Read(kWriteSize - read, &result, scratch));
  170. read_data.append(result.data(), result.size());
  171. read += result.size();
  172. }
  173. ASSERT_TRUE(write_data == read_data);
  174. delete[] scratch;
  175. }
  176. } // namespace ROCKSDB_NAMESPACE
  177. int main(int argc, char** argv) {
  178. ::testing::InitGoogleTest(&argc, argv);
  179. return RUN_ALL_TESTS();
  180. }
  181. #else
  182. #include <stdio.h>
  183. int main(int argc, char** argv) {
  184. fprintf(stderr, "SKIPPED as EnvMirror is not supported in ROCKSDB_LITE\n");
  185. return 0;
  186. }
  187. #endif // !ROCKSDB_LITE