Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/storage/__tests__/AphrontIsolatedDatabaseConnectionTestCase.php
12241 views
1
<?php
2
3
final class AphrontIsolatedDatabaseConnectionTestCase
4
extends PhabricatorTestCase {
5
6
protected function getPhabricatorTestCaseConfiguration() {
7
return array(
8
// We disable this here because this test is unique (it is testing that
9
// isolation actually occurs) and must establish a live connection to the
10
// database to verify that.
11
self::PHABRICATOR_TESTCONFIG_ISOLATE_LISK => false,
12
);
13
}
14
15
public function testIsolation() {
16
// This will fail if the connection isn't isolated.
17
queryfx(
18
$this->newIsolatedConnection(),
19
'INSERT INVALID SYNTAX');
20
21
$this->assertTrue(true);
22
}
23
24
public function testInsertGeneratesID() {
25
$conn = $this->newIsolatedConnection();
26
27
queryfx($conn, 'INSERT');
28
$id1 = $conn->getInsertID();
29
30
queryfx($conn, 'INSERT');
31
$id2 = $conn->getInsertID();
32
33
$this->assertTrue((bool)$id1, pht('ID1 exists.'));
34
$this->assertTrue((bool)$id2, pht('ID2 exists.'));
35
$this->assertTrue(
36
$id1 != $id2,
37
pht("IDs '%s' and '%s' are distinct.", $id1, $id2));
38
}
39
40
public function testDeletePermitted() {
41
$conn = $this->newIsolatedConnection();
42
queryfx($conn, 'DELETE');
43
44
$this->assertTrue(true);
45
}
46
47
public function testTransactionStack() {
48
$conn = $this->newIsolatedConnection();
49
$conn->openTransaction();
50
queryfx($conn, 'INSERT');
51
$conn->saveTransaction();
52
$this->assertEqual(
53
array(
54
'START TRANSACTION',
55
'INSERT',
56
'COMMIT',
57
),
58
$conn->getQueryTranscript());
59
60
$conn = $this->newIsolatedConnection();
61
$conn->openTransaction();
62
queryfx($conn, 'INSERT 1');
63
$conn->openTransaction();
64
queryfx($conn, 'INSERT 2');
65
$conn->killTransaction();
66
$conn->openTransaction();
67
queryfx($conn, 'INSERT 3');
68
$conn->openTransaction();
69
queryfx($conn, 'INSERT 4');
70
$conn->saveTransaction();
71
$conn->saveTransaction();
72
$conn->openTransaction();
73
queryfx($conn, 'INSERT 5');
74
$conn->killTransaction();
75
queryfx($conn, 'INSERT 6');
76
$conn->saveTransaction();
77
78
$this->assertEqual(
79
array(
80
'START TRANSACTION',
81
'INSERT 1',
82
'SAVEPOINT Aphront_Savepoint_1',
83
'INSERT 2',
84
'ROLLBACK TO SAVEPOINT Aphront_Savepoint_1',
85
'SAVEPOINT Aphront_Savepoint_1',
86
'INSERT 3',
87
'SAVEPOINT Aphront_Savepoint_2',
88
'INSERT 4',
89
'SAVEPOINT Aphront_Savepoint_1',
90
'INSERT 5',
91
'ROLLBACK TO SAVEPOINT Aphront_Savepoint_1',
92
'INSERT 6',
93
'COMMIT',
94
),
95
$conn->getQueryTranscript());
96
}
97
98
public function testTransactionRollback() {
99
$check = array();
100
101
$phid = new HarbormasterScratchTable();
102
$phid->openTransaction();
103
for ($ii = 0; $ii < 3; $ii++) {
104
$key = $this->generateTestData();
105
106
$obj = new HarbormasterScratchTable();
107
$obj->setData($key);
108
$obj->save();
109
110
$check[] = $key;
111
}
112
$phid->killTransaction();
113
114
foreach ($check as $key) {
115
$this->assertNoSuchRow($key);
116
}
117
}
118
119
private function newIsolatedConnection() {
120
$config = array();
121
return new AphrontIsolatedDatabaseConnection($config);
122
}
123
124
private function generateTestData() {
125
return Filesystem::readRandomCharacters(20);
126
}
127
128
private function assertNoSuchRow($data) {
129
try {
130
$row = id(new HarbormasterScratchTable())->loadOneWhere(
131
'data = %s',
132
$data);
133
$this->assertEqual(
134
null,
135
$row,
136
pht('Expect fake row to exist only in isolation.'));
137
} catch (AphrontConnectionQueryException $ex) {
138
// If we can't connect to the database, conclude that the isolated
139
// connection actually is isolated. Philosophically, this perhaps allows
140
// us to claim this test does not depend on the database?
141
}
142
}
143
144
}
145
146