2014-10-31 22:08:10 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
2019-04-18 17:51:19 +00:00
|
|
|
//
|
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
2014-10-31 22:08:10 +00:00
|
|
|
|
2017-04-06 02:02:00 +00:00
|
|
|
#include "env/mock_env.h"
|
2016-06-03 22:13:03 +00:00
|
|
|
|
2014-10-31 22:08:10 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2016-06-03 22:13:03 +00:00
|
|
|
|
2014-10-31 22:08:10 +00:00
|
|
|
#include "rocksdb/env.h"
|
2019-05-30 18:21:38 +00:00
|
|
|
#include "test_util/testharness.h"
|
2014-10-31 22:08:10 +00:00
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2014-10-31 22:08:10 +00:00
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
class MockEnvTest : public testing::Test {
|
2014-10-31 22:08:10 +00:00
|
|
|
public:
|
2015-04-13 23:15:05 +00:00
|
|
|
MockEnv* env_;
|
2014-10-31 22:08:10 +00:00
|
|
|
const EnvOptions soptions_;
|
|
|
|
|
2021-09-21 15:53:03 +00:00
|
|
|
MockEnvTest() : env_(MockEnv::Create(Env::Default())) {}
|
2019-02-19 21:36:04 +00:00
|
|
|
~MockEnvTest() override { delete env_; }
|
2014-10-31 22:08:10 +00:00
|
|
|
};
|
|
|
|
|
2015-03-17 21:08:00 +00:00
|
|
|
TEST_F(MockEnvTest, Corrupt) {
|
2014-10-31 22:16:31 +00:00
|
|
|
const std::string kGood = "this is a good string, synced to disk";
|
|
|
|
const std::string kCorrupted = "this part may be corrupted";
|
|
|
|
const std::string kFileName = "/dir/f";
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<WritableFile> writable_file;
|
2014-10-31 22:16:31 +00:00
|
|
|
ASSERT_OK(env_->NewWritableFile(kFileName, &writable_file, soptions_));
|
|
|
|
ASSERT_OK(writable_file->Append(kGood));
|
|
|
|
ASSERT_TRUE(writable_file->GetFileSize() == kGood.size());
|
|
|
|
|
|
|
|
std::string scratch;
|
|
|
|
scratch.resize(kGood.size() + kCorrupted.size() + 16);
|
|
|
|
Slice result;
|
2018-11-09 19:17:34 +00:00
|
|
|
std::unique_ptr<RandomAccessFile> rand_file;
|
2014-10-31 22:16:31 +00:00
|
|
|
ASSERT_OK(env_->NewRandomAccessFile(kFileName, &rand_file, soptions_));
|
|
|
|
ASSERT_OK(rand_file->Read(0, kGood.size(), &result, &(scratch[0])));
|
|
|
|
ASSERT_EQ(result.compare(kGood), 0);
|
|
|
|
|
|
|
|
// Sync + corrupt => no change
|
|
|
|
ASSERT_OK(writable_file->Fsync());
|
|
|
|
ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
|
|
|
|
result.clear();
|
|
|
|
ASSERT_OK(rand_file->Read(0, kGood.size(), &result, &(scratch[0])));
|
|
|
|
ASSERT_EQ(result.compare(kGood), 0);
|
|
|
|
|
|
|
|
// Add new data and corrupt it
|
|
|
|
ASSERT_OK(writable_file->Append(kCorrupted));
|
|
|
|
ASSERT_TRUE(writable_file->GetFileSize() == kGood.size() + kCorrupted.size());
|
|
|
|
result.clear();
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
rand_file->Read(kGood.size(), kCorrupted.size(), &result, &(scratch[0])));
|
2014-10-31 22:16:31 +00:00
|
|
|
ASSERT_EQ(result.compare(kCorrupted), 0);
|
|
|
|
// Corrupted
|
|
|
|
ASSERT_OK(dynamic_cast<MockEnv*>(env_)->CorruptBuffer(kFileName));
|
|
|
|
result.clear();
|
2022-10-25 00:54:14 +00:00
|
|
|
ASSERT_OK(
|
|
|
|
rand_file->Read(kGood.size(), kCorrupted.size(), &result, &(scratch[0])));
|
2014-10-31 22:16:31 +00:00
|
|
|
ASSERT_NE(result.compare(kCorrupted), 0);
|
|
|
|
}
|
|
|
|
|
2015-04-13 23:15:05 +00:00
|
|
|
TEST_F(MockEnvTest, FakeSleeping) {
|
|
|
|
int64_t now = 0;
|
|
|
|
auto s = env_->GetCurrentTime(&now);
|
|
|
|
ASSERT_OK(s);
|
2021-09-21 15:53:03 +00:00
|
|
|
env_->SleepForMicroseconds(3 * 1000 * 1000);
|
2015-04-13 23:15:05 +00:00
|
|
|
int64_t after_sleep = 0;
|
|
|
|
s = env_->GetCurrentTime(&after_sleep);
|
|
|
|
ASSERT_OK(s);
|
|
|
|
auto delta = after_sleep - now;
|
|
|
|
// this will be true unless test runs for 2 seconds
|
|
|
|
ASSERT_TRUE(delta == 3 || delta == 4);
|
|
|
|
}
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|
2014-10-31 22:08:10 +00:00
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2022-10-18 07:35:35 +00:00
|
|
|
ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
|
2015-03-17 21:08:00 +00:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
|
|
|
return RUN_ALL_TESTS();
|
2014-10-31 22:08:10 +00:00
|
|
|
}
|