Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: content/browser/indexed_db/indexed_db_browsertest_mock_factory.cc

Issue 334303002: Using a mock LevelDBTransaction for corruption tests. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Combined VLOG and NOTREACHED Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium 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.
4
5 #include <string>
6
7 #include "base/logging.h"
8 #include "content/browser/indexed_db/indexed_db_browsertest_mock_factory.h"
9 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h"
10 #include "third_party/leveldatabase/src/include/leveldb/status.h"
11
12 namespace {
13
14 class FunctionTracer {
15 public:
16 FunctionTracer(const std::string& class_name,
17 const std::string& method_name,
18 int instance_num)
19 : class_name_(class_name),
20 method_name_(method_name),
21 instance_count_(instance_num),
22 current_call_num_(0) {}
23
24 void log_call() {
25 current_call_num_++;
26 VLOG(0) << class_name_ << '[' << instance_count_ << "]::" << method_name_
27 << "()[" << current_call_num_ << ']';
28 }
29
30 private:
31 std::string class_name_;
32 std::string method_name_;
33 int instance_count_;
34 int current_call_num_;
35 };
36
37 } // namespace
38
39 namespace content {
40
41 class LevelDBTestTansaction : public LevelDBTransaction {
42 public:
43 LevelDBTestTansaction(LevelDBDatabase* db,
44 FailMethod fail_method,
45 int fail_on_call_num)
46 : LevelDBTransaction(db),
47 fail_method_(fail_method),
48 fail_on_call_num_(fail_on_call_num),
49 current_call_num_(0) {
50 DCHECK(fail_method != FAIL_METHOD_NOTHING);
51 DCHECK_GT(fail_on_call_num, 0);
52 }
53
54 virtual leveldb::Status Get(const base::StringPiece& key,
55 std::string* value,
56 bool* found) OVERRIDE {
57 if (fail_method_ != FAIL_METHOD_GET ||
58 ++current_call_num_ != fail_on_call_num_)
59 return LevelDBTransaction::Get(key, value, found);
60
61 *found = false;
62 return leveldb::Status::Corruption("Corrupted for the test");
63 }
64
65 virtual leveldb::Status Commit() OVERRIDE {
66 if (fail_method_ != FAIL_METHOD_COMMIT ||
67 ++current_call_num_ != fail_on_call_num_)
68 return LevelDBTransaction::Commit();
69
70 return leveldb::Status::Corruption("Corrupted for the test");
71 }
72
73 private:
74 virtual ~LevelDBTestTansaction() {}
75
76 FailMethod fail_method_;
77 int fail_on_call_num_;
78 int current_call_num_;
79 };
80
81 class LevelDBTraceTansaction : public LevelDBTransaction {
82 public:
83 LevelDBTraceTansaction(LevelDBDatabase* db, int tx_num)
84 : LevelDBTransaction(db),
85 commit_tracer_(s_class_name, "Commit", tx_num),
86 get_tracer_(s_class_name, "Get", tx_num) {}
87
88 virtual leveldb::Status Get(const base::StringPiece& key,
89 std::string* value,
90 bool* found) OVERRIDE {
91 get_tracer_.log_call();
92 return LevelDBTransaction::Get(key, value, found);
93 }
94
95 virtual leveldb::Status Commit() OVERRIDE {
96 commit_tracer_.log_call();
97 return LevelDBTransaction::Commit();
98 }
99
100 private:
101 virtual ~LevelDBTraceTansaction() {}
102
103 const std::string s_class_name = "LevelDBTransaction";
104
105 FunctionTracer commit_tracer_;
106 FunctionTracer get_tracer_;
107 };
108
109 IndexedDBBrowserTestClassFactory::IndexedDBBrowserTestClassFactory()
110 : failure_class_(FAIL_CLASS_NOTHING),
111 failure_method_(FAIL_METHOD_NOTHING),
112 only_trace_calls_(false) {
113 }
114
115 IndexedDBBrowserTestClassFactory::~IndexedDBBrowserTestClassFactory() {
116 }
117
118 LevelDBTransaction* IndexedDBBrowserTestClassFactory::CreateLevelDBTransaction(
119 LevelDBDatabase* db) {
120 instance_count_[FAIL_CLASS_LEVELDB_TRANSACTION] =
121 instance_count_[FAIL_CLASS_LEVELDB_TRANSACTION] + 1;
122 if (only_trace_calls_) {
123 return new LevelDBTraceTansaction(
124 db, instance_count_[FAIL_CLASS_LEVELDB_TRANSACTION]);
125 } else {
126 if (failure_class_ == FAIL_CLASS_LEVELDB_TRANSACTION &&
jsbell 2014/06/19 21:08:47 Need braces for this if block (and therefore for t
cmumford 2014/06/19 22:17:11 Done.
127 instance_count_[FAIL_CLASS_LEVELDB_TRANSACTION] ==
128 fail_on_instance_num_[FAIL_CLASS_LEVELDB_TRANSACTION])
129 return new LevelDBTestTansaction(
130 db,
131 failure_method_,
132 fail_on_call_num_[FAIL_CLASS_LEVELDB_TRANSACTION]);
133 else
134 return IndexedDBClassFactory::CreateLevelDBTransaction(db);
135 }
136 }
137
138 void IndexedDBBrowserTestClassFactory::FailOperation(FailClass failure_class,
139 FailMethod failure_method,
140 int fail_on_instance_num,
141 int fail_on_call_num) {
142 VLOG(0) << "FailOperation: class=" << failure_class
143 << ", method=" << failure_method
144 << ", instanceNum=" << fail_on_instance_num
145 << ", callNum=" << fail_on_call_num;
146 DCHECK(failure_class != FAIL_CLASS_NOTHING);
147 DCHECK(failure_method != FAIL_METHOD_NOTHING);
148 failure_class_ = failure_class;
149 failure_method_ = failure_method;
150 fail_on_instance_num_[failure_class_] = fail_on_instance_num;
151 fail_on_call_num_[failure_class_] = fail_on_call_num;
152 instance_count_.clear();
153 }
154
155 void IndexedDBBrowserTestClassFactory::Reset() {
156 failure_class_ = FAIL_CLASS_NOTHING;
157 failure_method_ = FAIL_METHOD_NOTHING;
158 instance_count_.clear();
159 fail_on_instance_num_.clear();
160 fail_on_call_num_.clear();
161 }
162
163 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698