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