mock_env_test.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. //
  5. // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  6. #include "env/mock_env.h"
  7. #include <memory>
  8. #include <string>
  9. #include "rocksdb/env.h"
  10. #include "test_util/testharness.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class MockEnvTest : public testing::Test {
  13. public:
  14. MockEnv* env_;
  15. const EnvOptions soptions_;
  16. MockEnvTest()
  17. : env_(new MockEnv(Env::Default())) {
  18. }
  19. ~MockEnvTest() override { delete env_; }
  20. };
  21. TEST_F(MockEnvTest, Corrupt) {
  22. const std::string kGood = "this is a good string, synced to disk";
  23. const std::string kCorrupted = "this part may be corrupted";
  24. const std::string kFileName = "/dir/f";
  25. std::unique_ptr<WritableFile> writable_file;
  26. ASSERT_OK(env_->NewWritableFile(kFileName, &writable_file, soptions_));
  27. ASSERT_OK(writable_file->Append(kGood));
  28. ASSERT_TRUE(writable_file->GetFileSize() == kGood.size());
  29. std::string scratch;
  30. scratch.resize(kGood.size() + kCorrupted.size() + 16);
  31. Slice result;
  32. std::unique_ptr<RandomAccessFile> rand_file;
  33. ASSERT_OK(env_->NewRandomAccessFile(kFileName, &rand_file, soptions_));
  34. ASSERT_OK(rand_file->Read(0, kGood.size(), &result, &(scratch[0])));
  35. ASSERT_EQ(result.compare(kGood), 0);
  36. // Sync + corrupt => no change
  37. ASSERT_OK(writable_file->Fsync());
  38. ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
  39. result.clear();
  40. ASSERT_OK(rand_file->Read(0, kGood.size(), &result, &(scratch[0])));
  41. ASSERT_EQ(result.compare(kGood), 0);
  42. // Add new data and corrupt it
  43. ASSERT_OK(writable_file->Append(kCorrupted));
  44. ASSERT_TRUE(writable_file->GetFileSize() == kGood.size() + kCorrupted.size());
  45. result.clear();
  46. ASSERT_OK(rand_file->Read(kGood.size(), kCorrupted.size(),
  47. &result, &(scratch[0])));
  48. ASSERT_EQ(result.compare(kCorrupted), 0);
  49. // Corrupted
  50. ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
  51. result.clear();
  52. ASSERT_OK(rand_file->Read(kGood.size(), kCorrupted.size(),
  53. &result, &(scratch[0])));
  54. ASSERT_NE(result.compare(kCorrupted), 0);
  55. }
  56. TEST_F(MockEnvTest, FakeSleeping) {
  57. int64_t now = 0;
  58. auto s = env_->GetCurrentTime(&now);
  59. ASSERT_OK(s);
  60. env_->FakeSleepForMicroseconds(3 * 1000 * 1000);
  61. int64_t after_sleep = 0;
  62. s = env_->GetCurrentTime(&after_sleep);
  63. ASSERT_OK(s);
  64. auto delta = after_sleep - now;
  65. // this will be true unless test runs for 2 seconds
  66. ASSERT_TRUE(delta == 3 || delta == 4);
  67. }
  68. } // namespace ROCKSDB_NAMESPACE
  69. int main(int argc, char** argv) {
  70. ::testing::InitGoogleTest(&argc, argv);
  71. return RUN_ALL_TESTS();
  72. }