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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
sql / transaction_unittest.cc [blame]
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sql/transaction.h"
#include <memory>
#include "base/files/scoped_temp_dir.h"
#include "sql/database.h"
#include "sql/statement.h"
#include "sql/test/test_helpers.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace sql {
namespace {
class SQLTransactionTest : public testing::Test {
public:
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
ASSERT_TRUE(
db_.Open(temp_dir_.GetPath().AppendASCII("transaction_test.sqlite")));
ASSERT_TRUE(db_.Execute("CREATE TABLE foo (a, b)"));
}
// Returns the number of rows in table "foo".
int CountFoo() {
Statement count(db_.GetUniqueStatement("SELECT count(*) FROM foo"));
count.Step();
return count.ColumnInt(0);
}
protected:
base::ScopedTempDir temp_dir_;
Database db_{test::kTestTag};
};
TEST_F(SQLTransactionTest, Commit) {
{
Transaction transaction(&db_);
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_FALSE(transaction.IsActiveForTesting());
ASSERT_TRUE(transaction.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_TRUE(transaction.IsActiveForTesting());
ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended";
transaction.Commit();
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_FALSE(transaction.IsActiveForTesting());
}
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_EQ(1, CountFoo()) << "Transaction changes not committed";
}
// Regression test for <https://crbug.com/326498384>.
TEST_F(SQLTransactionTest, CloseDatabase) {
EXPECT_FALSE(db_.HasActiveTransactions());
{
Transaction transaction(&db_);
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_FALSE(transaction.IsActiveForTesting());
ASSERT_TRUE(transaction.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_TRUE(transaction.IsActiveForTesting());
db_.Close();
EXPECT_FALSE(db_.HasActiveTransactions());
}
EXPECT_FALSE(db_.HasActiveTransactions());
}
TEST_F(SQLTransactionTest, RollbackOnDestruction) {
EXPECT_FALSE(db_.HasActiveTransactions());
{
Transaction transaction(&db_);
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_FALSE(transaction.IsActiveForTesting());
ASSERT_TRUE(transaction.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_TRUE(transaction.IsActiveForTesting());
ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended";
}
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_EQ(0, CountFoo()) << "Transaction changes not rolled back";
}
TEST_F(SQLTransactionTest, ExplicitRollback) {
EXPECT_FALSE(db_.HasActiveTransactions());
{
Transaction transaction(&db_);
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_FALSE(transaction.IsActiveForTesting());
ASSERT_TRUE(transaction.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_TRUE(transaction.IsActiveForTesting());
ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended";
transaction.Rollback();
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_FALSE(transaction.IsActiveForTesting());
EXPECT_EQ(0, CountFoo()) << "Transaction changes not rolled back";
}
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_EQ(0, CountFoo()) << "Transaction changes not rolled back";
}
// Rolling back any part of a transaction should roll back all of them.
TEST_F(SQLTransactionTest, NestedRollback) {
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_EQ(0, db_.transaction_nesting());
// Outermost transaction.
{
Transaction outer_txn(&db_);
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_EQ(0, db_.transaction_nesting());
ASSERT_TRUE(outer_txn.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
// First inner transaction is committed.
{
Transaction committed_inner_txn(&db_);
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
ASSERT_TRUE(committed_inner_txn.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(2, db_.transaction_nesting());
ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (1, 2)"));
ASSERT_EQ(1, CountFoo()) << "INSERT did not work as intended";
committed_inner_txn.Commit();
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
}
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
EXPECT_EQ(1, CountFoo()) << "First inner transaction did not commit";
// Second inner transaction is rolled back.
{
Transaction rolled_back_inner_txn(&db_);
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
ASSERT_TRUE(rolled_back_inner_txn.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(2, db_.transaction_nesting());
ASSERT_TRUE(db_.Execute("INSERT INTO foo (a, b) VALUES (2, 3)"));
ASSERT_EQ(2, CountFoo()) << "INSERT did not work as intended";
rolled_back_inner_txn.Rollback();
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
EXPECT_EQ(2, CountFoo())
<< "Nested transaction rollback deferred to top-level transaction";
}
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
EXPECT_EQ(2, CountFoo())
<< "Nested transaction rollback deferred to top-level transaction";
// Third inner transaction fails in Begin(), because a nested transaction
// has already been rolled back.
{
Transaction failed_inner_txn(&db_);
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
EXPECT_FALSE(failed_inner_txn.Begin());
EXPECT_TRUE(db_.HasActiveTransactions());
EXPECT_EQ(1, db_.transaction_nesting());
}
}
EXPECT_FALSE(db_.HasActiveTransactions());
EXPECT_EQ(0, db_.transaction_nesting());
EXPECT_EQ(0, CountFoo());
}
TEST(SQLTransactionDatabaseDestroyedTest, BeginIsNoOp) {
auto db = std::make_unique<Database>(test::kTestTag);
ASSERT_TRUE(db->OpenInMemory());
Transaction transaction(db.get());
db.reset();
ASSERT_FALSE(transaction.Begin());
}
TEST(SQLTransactionDatabaseDestroyedTest, RollbackIsNoOp) {
auto db = std::make_unique<Database>(test::kTestTag);
ASSERT_TRUE(db->OpenInMemory());
Transaction transaction(db.get());
ASSERT_TRUE(transaction.Begin());
EXPECT_TRUE(db->HasActiveTransactions());
db.reset();
// `Transaction::Rollback()` does not return a value, so we cannot verify
// externally whether it returned early.
transaction.Rollback();
}
TEST(SQLTransactionDatabaseDestroyedTest, CommitIsNoOp) {
auto db = std::make_unique<Database>(test::kTestTag);
ASSERT_TRUE(db->OpenInMemory());
Transaction transaction(db.get());
ASSERT_TRUE(transaction.Begin());
EXPECT_TRUE(db->HasActiveTransactions());
db.reset();
ASSERT_FALSE(transaction.Commit());
}
} // namespace
} // namespace sql