]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/env/mock_env_test.cc
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / env / mock_env_test.cc
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 }
23 ~MockEnvTest() {
24 delete env_;
25 }
26 };
27
28 TEST_F(MockEnvTest, Corrupt) {
29 const std::string kGood = "this is a good string, synced to disk";
30 const std::string kCorrupted = "this part may be corrupted";
31 const std::string kFileName = "/dir/f";
32 unique_ptr<WritableFile> writable_file;
33 ASSERT_OK(env_->NewWritableFile(kFileName, &writable_file, soptions_));
34 ASSERT_OK(writable_file->Append(kGood));
35 ASSERT_TRUE(writable_file->GetFileSize() == kGood.size());
36
37 std::string scratch;
38 scratch.resize(kGood.size() + kCorrupted.size() + 16);
39 Slice result;
40 unique_ptr<RandomAccessFile> rand_file;
41 ASSERT_OK(env_->NewRandomAccessFile(kFileName, &rand_file, soptions_));
42 ASSERT_OK(rand_file->Read(0, kGood.size(), &result, &(scratch[0])));
43 ASSERT_EQ(result.compare(kGood), 0);
44
45 // Sync + corrupt => no change
46 ASSERT_OK(writable_file->Fsync());
47 ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
48 result.clear();
49 ASSERT_OK(rand_file->Read(0, kGood.size(), &result, &(scratch[0])));
50 ASSERT_EQ(result.compare(kGood), 0);
51
52 // Add new data and corrupt it
53 ASSERT_OK(writable_file->Append(kCorrupted));
54 ASSERT_TRUE(writable_file->GetFileSize() == kGood.size() + kCorrupted.size());
55 result.clear();
56 ASSERT_OK(rand_file->Read(kGood.size(), kCorrupted.size(),
57 &result, &(scratch[0])));
58 ASSERT_EQ(result.compare(kCorrupted), 0);
59 // Corrupted
60 ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
61 result.clear();
62 ASSERT_OK(rand_file->Read(kGood.size(), kCorrupted.size(),
63 &result, &(scratch[0])));
64 ASSERT_NE(result.compare(kCorrupted), 0);
65 }
66
67 TEST_F(MockEnvTest, FakeSleeping) {
68 int64_t now = 0;
69 auto s = env_->GetCurrentTime(&now);
70 ASSERT_OK(s);
71 env_->FakeSleepForMicroseconds(3 * 1000 * 1000);
72 int64_t after_sleep = 0;
73 s = env_->GetCurrentTime(&after_sleep);
74 ASSERT_OK(s);
75 auto delta = after_sleep - now;
76 // this will be true unless test runs for 2 seconds
77 ASSERT_TRUE(delta == 3 || delta == 4);
78 }
79
80 } // namespace rocksdb
81
82 int main(int argc, char** argv) {
83 ::testing::InitGoogleTest(&argc, argv);
84 return RUN_ALL_TESTS();
85 }