mock_env.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. //
  6. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  7. // Use of this source code is governed by a BSD-style license that can be
  8. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  9. #pragma once
  10. #include <atomic>
  11. #include <map>
  12. #include <string>
  13. #include <vector>
  14. #include "rocksdb/env.h"
  15. #include "rocksdb/status.h"
  16. #include "port/port.h"
  17. #include "util/mutexlock.h"
  18. namespace ROCKSDB_NAMESPACE {
  19. class MemFile;
  20. class MockEnv : public EnvWrapper {
  21. public:
  22. explicit MockEnv(Env* base_env);
  23. virtual ~MockEnv();
  24. // Partial implementation of the Env interface.
  25. virtual Status NewSequentialFile(const std::string& fname,
  26. std::unique_ptr<SequentialFile>* result,
  27. const EnvOptions& soptions) override;
  28. virtual Status NewRandomAccessFile(const std::string& fname,
  29. std::unique_ptr<RandomAccessFile>* result,
  30. const EnvOptions& soptions) override;
  31. virtual Status NewRandomRWFile(const std::string& fname,
  32. std::unique_ptr<RandomRWFile>* result,
  33. const EnvOptions& options) override;
  34. virtual Status ReuseWritableFile(const std::string& fname,
  35. const std::string& old_fname,
  36. std::unique_ptr<WritableFile>* result,
  37. const EnvOptions& options) override;
  38. virtual Status NewWritableFile(const std::string& fname,
  39. std::unique_ptr<WritableFile>* result,
  40. const EnvOptions& env_options) override;
  41. virtual Status NewDirectory(const std::string& name,
  42. std::unique_ptr<Directory>* result) override;
  43. virtual Status FileExists(const std::string& fname) override;
  44. virtual Status GetChildren(const std::string& dir,
  45. std::vector<std::string>* result) override;
  46. void DeleteFileInternal(const std::string& fname);
  47. virtual Status DeleteFile(const std::string& fname) override;
  48. virtual Status Truncate(const std::string& fname, size_t size) override;
  49. virtual Status CreateDir(const std::string& dirname) override;
  50. virtual Status CreateDirIfMissing(const std::string& dirname) override;
  51. virtual Status DeleteDir(const std::string& dirname) override;
  52. virtual Status GetFileSize(const std::string& fname,
  53. uint64_t* file_size) override;
  54. virtual Status GetFileModificationTime(const std::string& fname,
  55. uint64_t* time) override;
  56. virtual Status RenameFile(const std::string& src,
  57. const std::string& target) override;
  58. virtual Status LinkFile(const std::string& src,
  59. const std::string& target) override;
  60. virtual Status NewLogger(const std::string& fname,
  61. std::shared_ptr<Logger>* result) override;
  62. virtual Status LockFile(const std::string& fname, FileLock** flock) override;
  63. virtual Status UnlockFile(FileLock* flock) override;
  64. virtual Status GetTestDirectory(std::string* path) override;
  65. // Results of these can be affected by FakeSleepForMicroseconds()
  66. virtual Status GetCurrentTime(int64_t* unix_time) override;
  67. virtual uint64_t NowMicros() override;
  68. virtual uint64_t NowNanos() override;
  69. Status CorruptBuffer(const std::string& fname);
  70. // Doesn't really sleep, just affects output of GetCurrentTime(), NowMicros()
  71. // and NowNanos()
  72. void FakeSleepForMicroseconds(int64_t micros);
  73. private:
  74. std::string NormalizePath(const std::string path);
  75. // Map from filenames to MemFile objects, representing a simple file system.
  76. typedef std::map<std::string, MemFile*> FileSystem;
  77. port::Mutex mutex_;
  78. FileSystem file_map_; // Protected by mutex_.
  79. std::atomic<int64_t> fake_sleep_micros_;
  80. };
  81. } // namespace ROCKSDB_NAMESPACE