YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/mock_env_test.cc
Line
Count
Source
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
// The following only applies to changes made to this file as part of YugaByte development.
6
//
7
// Portions Copyright (c) YugaByte, Inc.
8
//
9
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
10
// in compliance with the License.  You may obtain a copy of the License at
11
//
12
// http://www.apache.org/licenses/LICENSE-2.0
13
//
14
// Unless required by applicable law or agreed to in writing, software distributed under the License
15
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
16
// or implied.  See the License for the specific language governing permissions and limitations
17
// under the License.
18
//
19
20
#include <memory>
21
#include <string>
22
#include <vector>
23
24
#include <gtest/gtest.h>
25
26
#include "yb/rocksdb/util/mock_env.h"
27
28
#include "yb/util/status.h"
29
#include "yb/util/test_macros.h"
30
#include "yb/rocksdb/util/testutil.h"
31
32
namespace rocksdb {
33
34
class MockEnvTest : public RocksDBTest {
35
 public:
36
  MockEnv* env_;
37
  const EnvOptions soptions_;
38
39
  MockEnvTest()
40
7
      : env_(new MockEnv(Env::Default())) {
41
7
  }
42
7
  ~MockEnvTest() {
43
7
    delete env_;
44
7
  }
45
};
46
47
1
TEST_F(MockEnvTest, Basics) {
48
1
  uint64_t file_size;
49
1
  unique_ptr<WritableFile> writable_file;
50
1
  std::vector<std::string> children;
51
52
1
  ASSERT_OK(env_->CreateDir("/dir"));
53
54
  // Check that the directory is empty.
55
1
  ASSERT_TRUE(env_->FileExists("/dir/non_existent").IsNotFound());
56
1
  ASSERT_TRUE(!env_->GetFileSize("/dir/non_existent", &file_size).ok());
57
1
  ASSERT_OK(env_->GetChildren("/dir", &children));
58
1
  ASSERT_EQ(0U, children.size());
59
60
  // Create a file.
61
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
62
1
  writable_file.reset();
63
64
  // Check that the file exists.
65
1
  ASSERT_OK(env_->FileExists("/dir/f"));
66
1
  ASSERT_OK(env_->GetFileSize("/dir/f", &file_size));
67
1
  ASSERT_EQ(0U, file_size);
68
1
  ASSERT_OK(env_->GetChildren("/dir", &children));
69
1
  ASSERT_EQ(1U, children.size());
70
1
  ASSERT_EQ("f", children[0]);
71
72
  // Write to the file.
73
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
74
1
  ASSERT_OK(writable_file->Append("abc"));
75
1
  writable_file.reset();
76
77
  // Check for expected size.
78
1
  ASSERT_OK(env_->GetFileSize("/dir/f", &file_size));
79
1
  ASSERT_EQ(3U, file_size);
80
81
  // Check that renaming works.
82
1
  ASSERT_TRUE(!env_->RenameFile("/dir/non_existent", "/dir/g").ok());
83
1
  ASSERT_OK(env_->RenameFile("/dir/f", "/dir/g"));
84
1
  ASSERT_TRUE(env_->FileExists("/dir/f").IsNotFound());
85
1
  ASSERT_OK(env_->FileExists("/dir/g"));
86
1
  ASSERT_OK(env_->GetFileSize("/dir/g", &file_size));
87
1
  ASSERT_EQ(3U, file_size);
88
89
  // Check that opening non-existent file fails.
90
1
  unique_ptr<SequentialFile> seq_file;
91
1
  unique_ptr<RandomAccessFile> rand_file;
92
1
  ASSERT_TRUE(!env_->NewSequentialFile("/dir/non_existent", &seq_file,
93
1
                                       soptions_).ok());
94
1
  ASSERT_TRUE(!seq_file);
95
1
  ASSERT_TRUE(!env_->NewRandomAccessFile("/dir/non_existent", &rand_file,
96
1
                                         soptions_).ok());
97
1
  ASSERT_TRUE(!rand_file);
98
99
  // Check that deleting works.
100
1
  ASSERT_TRUE(!env_->DeleteFile("/dir/non_existent").ok());
101
1
  ASSERT_OK(env_->DeleteFile("/dir/g"));
102
1
  ASSERT_TRUE(env_->FileExists("/dir/g").IsNotFound());
103
1
  ASSERT_OK(env_->GetChildren("/dir", &children));
104
1
  ASSERT_EQ(0U, children.size());
105
1
  ASSERT_OK(env_->DeleteDir("/dir"));
106
1
}
107
108
1
TEST_F(MockEnvTest, ReadWrite) {
109
1
  unique_ptr<WritableFile> writable_file;
110
1
  unique_ptr<SequentialFile> seq_file;
111
1
  unique_ptr<RandomAccessFile> rand_file;
112
1
  Slice result;
113
1
  uint8_t scratch[100];
114
115
1
  ASSERT_OK(env_->CreateDir("/dir"));
116
117
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
118
1
  ASSERT_OK(writable_file->Append("hello "));
119
1
  ASSERT_OK(writable_file->Append("world"));
120
1
  writable_file.reset();
121
122
  // Read sequentially.
123
1
  ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file, soptions_));
124
1
  ASSERT_OK(seq_file->Read(5, &result, scratch));  // Read "hello".
125
1
  ASSERT_EQ(0, result.compare("hello"));
126
1
  ASSERT_OK(seq_file->Skip(1));
127
1
  ASSERT_OK(seq_file->Read(1000, &result, scratch));  // Read "world".
128
1
  ASSERT_EQ(0, result.compare("world"));
129
1
  ASSERT_OK(seq_file->Read(1000, &result, scratch));  // Try reading past EOF.
130
1
  ASSERT_EQ(0U, result.size());
131
1
  ASSERT_OK(seq_file->Skip(100));  // Try to skip past end of file.
132
1
  ASSERT_OK(seq_file->Read(1000, &result, scratch));
133
1
  ASSERT_EQ(0U, result.size());
134
135
  // Random reads.
136
1
  ASSERT_OK(env_->NewRandomAccessFile("/dir/f", &rand_file, soptions_));
137
1
  ASSERT_OK(rand_file->Read(6, 5, &result, scratch));  // Read "world".
138
1
  ASSERT_EQ(0, result.compare("world"));
139
1
  ASSERT_OK(rand_file->Read(0, 5, &result, scratch));  // Read "hello".
140
1
  ASSERT_EQ(0, result.compare("hello"));
141
1
  ASSERT_OK(rand_file->Read(10, 100, &result, scratch));  // Read "d".
142
1
  ASSERT_EQ(0, result.compare("d"));
143
144
  // Too high offset.
145
1
  ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok());
146
1
}
147
148
1
TEST_F(MockEnvTest, Locks) {
149
1
  FileLock* lock;
150
151
  // These are no-ops, but we test they return success.
152
1
  ASSERT_OK(env_->LockFile("some file", &lock));
153
1
  ASSERT_OK(env_->UnlockFile(lock));
154
1
}
155
156
1
TEST_F(MockEnvTest, Misc) {
157
1
  std::string test_dir;
158
1
  ASSERT_OK(env_->GetTestDirectory(&test_dir));
159
1
  ASSERT_TRUE(!test_dir.empty());
160
161
1
  unique_ptr<WritableFile> writable_file;
162
1
  ASSERT_OK(env_->NewWritableFile("/a/b", &writable_file, soptions_));
163
164
  // These are no-ops, but we test they return success.
165
1
  ASSERT_OK(writable_file->Sync());
166
1
  ASSERT_OK(writable_file->Flush());
167
1
  ASSERT_OK(writable_file->Close());
168
1
  writable_file.reset();
169
1
}
170
171
1
TEST_F(MockEnvTest, LargeWrite) {
172
1
  const size_t kWriteSize = 300 * 1024;
173
1
  uint8_t* scratch = new uint8_t[kWriteSize * 2];
174
175
1
  std::string write_data;
176
307k
  for (size_t i = 0; i < kWriteSize; ++i) {
177
307k
    write_data.append(1, static_cast<char>(i));
178
307k
  }
179
180
1
  unique_ptr<WritableFile> writable_file;
181
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
182
1
  ASSERT_OK(writable_file->Append("foo"));
183
1
  ASSERT_OK(writable_file->Append(write_data));
184
1
  writable_file.reset();
185
186
1
  unique_ptr<SequentialFile> seq_file;
187
1
  Slice result;
188
1
  ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file, soptions_));
189
1
  ASSERT_OK(seq_file->Read(3, &result, scratch));  // Read "foo".
190
1
  ASSERT_EQ(0, result.compare("foo"));
191
192
1
  size_t read = 0;
193
1
  std::string read_data;
194
2
  while (read < kWriteSize) {
195
1
    ASSERT_OK(seq_file->Read(kWriteSize - read, &result, scratch));
196
1
    read_data.append(result.cdata(), result.size());
197
1
    read += result.size();
198
1
  }
199
1
  ASSERT_TRUE(write_data == read_data);
200
1
  delete [] scratch;
201
1
}
202
203
1
TEST_F(MockEnvTest, Corrupt) {
204
1
  const std::string kGood = "this is a good string, synced to disk";
205
1
  const std::string kCorrupted = "this part may be corrupted";
206
1
  const std::string kFileName = "/dir/f";
207
1
  unique_ptr<WritableFile> writable_file;
208
1
  ASSERT_OK(env_->NewWritableFile(kFileName, &writable_file, soptions_));
209
1
  ASSERT_OK(writable_file->Append(kGood));
210
1
  ASSERT_TRUE(writable_file->GetFileSize() == kGood.size());
211
212
1
  std::string scratch;
213
1
  scratch.resize(kGood.size() + kCorrupted.size() + 16);
214
1
  uint8_t* read_buf = reinterpret_cast<uint8_t*>(&(scratch[0]));
215
1
  Slice result;
216
1
  unique_ptr<RandomAccessFile> rand_file;
217
1
  ASSERT_OK(env_->NewRandomAccessFile(kFileName, &rand_file, soptions_));
218
1
  ASSERT_OK(rand_file->Read(0, kGood.size(), &result, read_buf));
219
1
  ASSERT_EQ(result.compare(kGood), 0);
220
221
  // Sync + corrupt => no change
222
1
  ASSERT_OK(writable_file->Fsync());
223
1
  ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
224
1
  result.clear();
225
1
  ASSERT_OK(rand_file->Read(0, kGood.size(), &result, read_buf));
226
1
  ASSERT_EQ(result.compare(kGood), 0);
227
228
  // Add new data and corrupt it
229
1
  ASSERT_OK(writable_file->Append(kCorrupted));
230
1
  ASSERT_TRUE(writable_file->GetFileSize() == kGood.size() + kCorrupted.size());
231
1
  result.clear();
232
1
  ASSERT_OK(rand_file->Read(kGood.size(), kCorrupted.size(), &result, read_buf));
233
1
  ASSERT_EQ(result.compare(kCorrupted), 0);
234
  // Corrupted
235
1
  ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
236
1
  result.clear();
237
1
  ASSERT_OK(rand_file->Read(kGood.size(), kCorrupted.size(), &result, read_buf));
238
1
  ASSERT_NE(result.compare(kCorrupted), 0);
239
1
}
240
241
1
TEST_F(MockEnvTest, FakeSleeping) {
242
1
  int64_t now = 0;
243
1
  auto s = env_->GetCurrentTime(&now);
244
1
  ASSERT_OK(s);
245
1
  env_->FakeSleepForMicroseconds(3 * 1000 * 1000);
246
1
  int64_t after_sleep = 0;
247
1
  s = env_->GetCurrentTime(&after_sleep);
248
1
  ASSERT_OK(s);
249
1
  auto delta = after_sleep - now;
250
  // this will be true unless test runs for 2 seconds
251
1
  ASSERT_TRUE(delta == 3 || delta == 4);
252
1
}
253
254
}  // namespace rocksdb
255
256
13.2k
int main(int argc, char** argv) {
257
13.2k
  ::testing::InitGoogleTest(&argc, argv);
258
13.2k
  return RUN_ALL_TESTS();
259
13.2k
}