mock_env_test.cc 2.8 KB

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