summaryrefslogtreecommitdiffstats
path: root/third_party/leveldatabase/env_chromium_unittest.cc
blob: eae2d6a1bb214db1830bea3acba71b48c43fca7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/test_suite.h"
#include "env_chromium.h"
#include "testing/gtest/include/gtest/gtest.h"

using namespace leveldb_env;
using namespace leveldb;

TEST(ErrorEncoding, OnlyAMethod) {
  const MethodID in_method = kSequentialFileRead;
  const Status s = MakeIOError("Somefile.txt", "message", in_method);
  int method = -50;
  int error = -75;
  EXPECT_TRUE(ParseMethodAndError(s.ToString().c_str(), &method, &error));
  EXPECT_EQ(in_method, method);
  EXPECT_EQ(-75, error);
}

TEST(ErrorEncoding, PlatformFileError) {
  const MethodID in_method = kWritableFileClose;
  const base::PlatformFileError pfe =
      base::PLATFORM_FILE_ERROR_INVALID_OPERATION;
  const Status s = MakeIOError("Somefile.txt", "message", in_method, pfe);
  int method;
  int error;
  EXPECT_TRUE(ParseMethodAndError(s.ToString().c_str(), &method, &error));
  EXPECT_EQ(in_method, method);
  EXPECT_EQ(pfe, error);
}

TEST(ErrorEncoding, Errno) {
  const MethodID in_method = kWritableFileFlush;
  const int some_errno = ENOENT;
  const Status s =
      MakeIOError("Somefile.txt", "message", in_method, some_errno);
  int method;
  int error;
  EXPECT_TRUE(ParseMethodAndError(s.ToString().c_str(), &method, &error));
  EXPECT_EQ(in_method, method);
  EXPECT_EQ(some_errno, error);
}

TEST(ErrorEncoding, NoEncodedMessage) {
  Status s = Status::IOError("Some message", "from leveldb itself");
  int method = 3;
  int error = 4;
  EXPECT_FALSE(ParseMethodAndError(s.ToString().c_str(), &method, &error));
  EXPECT_EQ(3, method);
  EXPECT_EQ(4, error);
}

class MyEnv : public ChromiumEnv {
 public:
  MyEnv() : directory_syncs_(0) {}
  int directory_syncs() { return directory_syncs_; }

 protected:
  virtual void DidSyncDir(const std::string& fname) {
    ++directory_syncs_;
    ChromiumEnv::DidSyncDir(fname);
  }

 private:
  int directory_syncs_;
};

TEST(ChromiumEnv, DirectorySyncing) {
  MyEnv env;
  base::ScopedTempDir dir;
  dir.CreateUniqueTempDir();
  base::FilePath dir_path = dir.path();
  std::string some_data = "some data";
  Slice data = some_data;

  std::string manifest_file_name =
      FilePathToString(dir_path.Append("MANIFEST-001"));
  WritableFile* manifest_file;
  Status s = env.NewWritableFile(manifest_file_name, &manifest_file);
  EXPECT_TRUE(s.ok());
  manifest_file->Append(data);
  EXPECT_EQ(0, env.directory_syncs());
  manifest_file->Append(data);
  EXPECT_EQ(0, env.directory_syncs());

  std::string sst_file_name = FilePathToString(dir_path.Append("000003.sst"));
  WritableFile* sst_file;
  s = env.NewWritableFile(sst_file_name, &sst_file);
  EXPECT_TRUE(s.ok());
  sst_file->Append(data);
  EXPECT_EQ(0, env.directory_syncs());

  manifest_file->Append(data);
  EXPECT_EQ(1, env.directory_syncs());
  manifest_file->Append(data);
  EXPECT_EQ(1, env.directory_syncs());
}

int main(int argc, char** argv) { return base::TestSuite(argc, argv).Run(); }