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