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 
11 #include <atomic>
12 #include <map>
13 #include <string>
14 #include <vector>
15 #include "rocksdb/env.h"
16 #include "rocksdb/status.h"
17 #include "port/port.h"
18 #include "util/mutexlock.h"
19 
20 namespace ROCKSDB_NAMESPACE {
21 
22 class MemFile;
23 class MockEnv : public EnvWrapper {
24  public:
25   explicit MockEnv(Env* base_env);
26 
27   ~MockEnv() override;
28 
29   // Partial implementation of the Env interface.
RegisterDbPaths(const std::vector<std::string> &)30   Status RegisterDbPaths(const std::vector<std::string>& /*paths*/) override {
31     return Status::OK();
32   }
33 
UnregisterDbPaths(const std::vector<std::string> &)34   Status UnregisterDbPaths(const std::vector<std::string>& /*paths*/) override {
35     return Status::OK();
36   }
37 
38   Status NewSequentialFile(const std::string& fname,
39                            std::unique_ptr<SequentialFile>* result,
40                            const EnvOptions& soptions) override;
41 
42   Status NewRandomAccessFile(const std::string& fname,
43                              std::unique_ptr<RandomAccessFile>* result,
44                              const EnvOptions& soptions) override;
45 
46   Status NewRandomRWFile(const std::string& fname,
47                          std::unique_ptr<RandomRWFile>* result,
48                          const EnvOptions& options) override;
49 
50   Status ReuseWritableFile(const std::string& fname,
51                            const std::string& old_fname,
52                            std::unique_ptr<WritableFile>* result,
53                            const EnvOptions& options) override;
54 
55   Status NewWritableFile(const std::string& fname,
56                          std::unique_ptr<WritableFile>* result,
57                          const EnvOptions& env_options) override;
58 
59   Status NewDirectory(const std::string& name,
60                       std::unique_ptr<Directory>* result) override;
61 
62   Status FileExists(const std::string& fname) override;
63 
64   Status GetChildren(const std::string& dir,
65                      std::vector<std::string>* result) override;
66 
67   void DeleteFileInternal(const std::string& fname);
68 
69   Status DeleteFile(const std::string& fname) override;
70 
71   Status Truncate(const std::string& fname, size_t size) override;
72 
73   Status CreateDir(const std::string& dirname) override;
74 
75   Status CreateDirIfMissing(const std::string& dirname) override;
76 
77   Status DeleteDir(const std::string& dirname) override;
78 
79   Status GetFileSize(const std::string& fname, uint64_t* file_size) override;
80 
81   Status GetFileModificationTime(const std::string& fname,
82                                  uint64_t* time) override;
83 
84   Status RenameFile(const std::string& src, const std::string& target) override;
85 
86   Status LinkFile(const std::string& src, const std::string& target) override;
87 
88   Status NewLogger(const std::string& fname,
89                    std::shared_ptr<Logger>* result) override;
90 
91   Status LockFile(const std::string& fname, FileLock** flock) override;
92 
93   Status UnlockFile(FileLock* flock) override;
94 
95   Status GetTestDirectory(std::string* path) override;
96 
97   // Results of these can be affected by FakeSleepForMicroseconds()
98   Status GetCurrentTime(int64_t* unix_time) override;
99   uint64_t NowMicros() override;
100   uint64_t NowNanos() override;
101 
102   Status CorruptBuffer(const std::string& fname);
103 
104   // Doesn't really sleep, just affects output of GetCurrentTime(), NowMicros()
105   // and NowNanos()
106   void FakeSleepForMicroseconds(int64_t micros);
107 
108  private:
109   // Map from filenames to MemFile objects, representing a simple file system.
110   typedef std::map<std::string, MemFile*> FileSystem;
111   port::Mutex mutex_;
112   FileSystem file_map_;  // Protected by mutex_.
113 
114   std::atomic<int64_t> fake_sleep_micros_;
115 };
116 
117 }  // namespace ROCKSDB_NAMESPACE
118