小组成员:姚凯文(kevinyao0901),姜嘉琪
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.

66 line
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, size_t len,
  22. 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()
  29. : EnvWrapper(NewMemEnv(Env::Default())),
  30. writable_file_error_(false),
  31. num_writable_file_errors_(0) {}
  32. ~ErrorEnv() override { delete target(); }
  33. Status NewWritableFile(const std::string& fname,
  34. WritableFile** result) override {
  35. if (writable_file_error_) {
  36. ++num_writable_file_errors_;
  37. *result = nullptr;
  38. return Status::IOError(fname, "fake error");
  39. }
  40. return target()->NewWritableFile(fname, result);
  41. }
  42. Status NewAppendableFile(const std::string& fname,
  43. WritableFile** result) override {
  44. if (writable_file_error_) {
  45. ++num_writable_file_errors_;
  46. *result = nullptr;
  47. return Status::IOError(fname, "fake error");
  48. }
  49. return target()->NewAppendableFile(fname, result);
  50. }
  51. };
  52. } // namespace test
  53. } // namespace leveldb
  54. #endif // STORAGE_LEVELDB_UTIL_TESTUTIL_H_