2011-03-19 06:37:00 +08: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.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_
|
|
|
|
#define STORAGE_LEVELDB_UTIL_TESTUTIL_H_
|
|
|
|
|
2019-11-26 01:29:06 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2019-03-20 08:30:42 +08:00
|
|
|
#include "helpers/memenv/memenv.h"
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/slice.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
#include "util/random.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
namespace test {
|
|
|
|
|
2019-11-22 05:09:53 +08:00
|
|
|
MATCHER(IsOK, "") { return arg.ok(); }
|
|
|
|
|
|
|
|
// Macros for testing the results of functions that return leveldb::Status or
|
|
|
|
// util::StatusOr<T> (for any type T).
|
|
|
|
#define EXPECT_LEVELDB_OK(expression) \
|
|
|
|
EXPECT_THAT(expression, leveldb::test::IsOK())
|
|
|
|
#define ASSERT_LEVELDB_OK(expression) \
|
|
|
|
ASSERT_THAT(expression, leveldb::test::IsOK())
|
|
|
|
|
|
|
|
// Returns the random seed used at the start of the current test run.
|
|
|
|
inline int RandomSeed() {
|
|
|
|
return testing::UnitTest::GetInstance()->random_seed();
|
|
|
|
}
|
|
|
|
|
2011-03-19 06:37:00 +08:00
|
|
|
// Store in *dst a random string of length "len" and return a Slice that
|
|
|
|
// references the generated data.
|
2018-03-13 00:14:44 +08:00
|
|
|
Slice RandomString(Random* rnd, int len, std::string* dst);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Return a random key with the specified length that may contain interesting
|
|
|
|
// characters (e.g. \x00, \xff, etc.).
|
2018-03-13 00:14:44 +08:00
|
|
|
std::string RandomKey(Random* rnd, int len);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// Store in *dst a string of length "len" that will compress to
|
|
|
|
// "N*compressed_fraction" bytes and return a Slice that references
|
|
|
|
// the generated data.
|
2019-05-03 02:01:00 +08:00
|
|
|
Slice CompressibleString(Random* rnd, double compressed_fraction, size_t len,
|
|
|
|
std::string* dst);
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
// A wrapper that allows injection of errors.
|
|
|
|
class ErrorEnv : public EnvWrapper {
|
|
|
|
public:
|
|
|
|
bool writable_file_error_;
|
|
|
|
int num_writable_file_errors_;
|
|
|
|
|
2019-05-03 02:01:00 +08:00
|
|
|
ErrorEnv()
|
|
|
|
: EnvWrapper(NewMemEnv(Env::Default())),
|
|
|
|
writable_file_error_(false),
|
|
|
|
num_writable_file_errors_(0) {}
|
|
|
|
~ErrorEnv() override { delete target(); }
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2019-03-21 03:41:49 +08:00
|
|
|
Status NewWritableFile(const std::string& fname,
|
|
|
|
WritableFile** result) override {
|
2011-03-19 06:37:00 +08:00
|
|
|
if (writable_file_error_) {
|
|
|
|
++num_writable_file_errors_;
|
2018-04-11 07:18:06 +08:00
|
|
|
*result = nullptr;
|
2011-03-19 06:37:00 +08:00
|
|
|
return Status::IOError(fname, "fake error");
|
|
|
|
}
|
|
|
|
return target()->NewWritableFile(fname, result);
|
|
|
|
}
|
2014-12-12 00:13:18 +08:00
|
|
|
|
2019-03-21 03:41:49 +08:00
|
|
|
Status NewAppendableFile(const std::string& fname,
|
|
|
|
WritableFile** result) override {
|
2014-12-12 00:13:18 +08:00
|
|
|
if (writable_file_error_) {
|
|
|
|
++num_writable_file_errors_;
|
2018-04-11 07:18:06 +08:00
|
|
|
*result = nullptr;
|
2014-12-12 00:13:18 +08:00
|
|
|
return Status::IOError(fname, "fake error");
|
|
|
|
}
|
|
|
|
return target()->NewAppendableFile(fname, result);
|
|
|
|
}
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace test
|
|
|
|
} // namespace leveldb
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_
|