作者: 韩晨旭 10225101440 李畅 10225102463
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.1 KiB

  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. #ifndef STORAGE_LEVELDB_UTIL_TESTUTIL_H_
  5. #define STORAGE_LEVELDB_UTIL_TESTUTIL_H_
  6. #include "helpers/memenv/memenv.h"
  7. #include "leveldb/env.h"
  8. #include "leveldb/slice.h"
  9. #include "util/random.h"
  10. namespace leveldb {
  11. namespace test {
  12. // Store in *dst a random string of length "len" and return a Slice that
  13. // references the generated data.
  14. Slice RandomString(Random* rnd, int len, std::string* dst);
  15. // Return a random key with the specified length that may contain interesting
  16. // characters (e.g. \x00, \xff, etc.).
  17. std::string RandomKey(Random* rnd, int len);
  18. // Store in *dst a string of length "len" that will compress to
  19. // "N*compressed_fraction" bytes and return a Slice that references
  20. // the generated data.
  21. Slice CompressibleString(Random* rnd, double compressed_fraction,
  22. size_t len, std::string* dst);
  23. // A wrapper that allows injection of errors.
  24. class ErrorEnv : public EnvWrapper {
  25. public:
  26. bool writable_file_error_;
  27. int num_writable_file_errors_;
  28. ErrorEnv() : EnvWrapper(NewMemEnv(Env::Default())),
  29. writable_file_error_(false),
  30. num_writable_file_errors_(0) { }
  31. ~ErrorEnv() override {
  32. delete target();
  33. }
  34. Status NewWritableFile(const std::string& fname,
  35. WritableFile** result) override {
  36. if (writable_file_error_) {
  37. ++num_writable_file_errors_;
  38. *result = nullptr;
  39. return Status::IOError(fname, "fake error");
  40. }
  41. return target()->NewWritableFile(fname, result);
  42. }
  43. Status NewAppendableFile(const std::string& fname,
  44. WritableFile** result) override {
  45. if (writable_file_error_) {
  46. ++num_writable_file_errors_;
  47. *result = nullptr;
  48. return Status::IOError(fname, "fake error");
  49. }
  50. return target()->NewAppendableFile(fname, result);
  51. }
  52. };
  53. } // namespace test
  54. } // namespace leveldb
  55. #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_