Repository navigation
Expand file tree
/
Copy pathWP_MySQL_On_SQLite_Concurrency_Tests.php
More file actions
186 lines (151 loc) · 6.04 KB
/
Copy pathWP_MySQL_On_SQLite_Concurrency_Tests.php
File metadata and controls
186 lines (151 loc) · 6.04 KB
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
<?php
use PHPUnit\Framework\TestCase;
/**
* Tests for concurrent access to the same SQLite database file.
*/
class WP_MySQL_On_SQLite_Concurrency_Tests extends TestCase {
/**
* Path to the temporary SQLite database file used in file-based tests.
*
* @var string|null
*/
private $db_path;
public function setUp(): void {
$this->db_path = tempnam( sys_get_temp_dir(), 'wp_sqlite_' );
unlink( $this->db_path ); // Remove so SQLite creates a fresh database.
}
public function tearDown(): void {
foreach ( array(
$this->db_path,
$this->db_path . '-wal',
$this->db_path . '-shm',
$this->db_path . '-journal',
) as $path ) {
if ( is_string( $path ) && file_exists( $path ) ) {
unlink( $path );
}
}
$this->db_path = null;
}
public function testSelectQueryIsNotWrappedInTransaction(): void {
$driver = $this->create_in_memory_driver();
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$driver->query( 'SELECT * FROM t' );
$this->assertStringStartsNotWith( 'BEGIN', $driver->get_last_sqlite_queries()[0]['sql'] );
}
public function testShowQueryOpensReadOnlyTransaction(): void {
$driver = $this->create_in_memory_driver();
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$driver->query( 'SHOW TABLES' );
$this->assertSame( 'BEGIN', $driver->get_last_sqlite_queries()[0]['sql'] );
}
public function testDescribeQueryOpensReadOnlyTransaction(): void {
$driver = $this->create_in_memory_driver();
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$driver->query( 'DESCRIBE t' );
$this->assertSame( 'BEGIN', $driver->get_last_sqlite_queries()[0]['sql'] );
}
/**
* @dataProvider provideWriteStatements
*/
public function testWriteQueryOpensWriteTransaction( string $query ): void {
$driver = $this->create_in_memory_driver();
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$driver->query( "INSERT INTO t VALUES (1, 'Alice')" );
$driver->query( $query );
$this->assertSame( 'BEGIN IMMEDIATE', $driver->get_last_sqlite_queries()[0]['sql'] );
}
public function provideWriteStatements(): array {
return array(
'INSERT' => array( "INSERT INTO t VALUES (2, 'Bob')" ),
'UPDATE' => array( "UPDATE t SET name = 'Carol' WHERE id = 1" ),
'DELETE' => array( 'DELETE FROM t WHERE id = 1' ),
'REPLACE' => array( "REPLACE INTO t VALUES (1, 'Dan')" ),
'CREATE TABLE' => array( 'CREATE TABLE u (id INT)' ),
'ALTER TABLE' => array( 'ALTER TABLE t ADD COLUMN x INT' ),
'DROP TABLE' => array( 'DROP TABLE t' ),
'TRUNCATE TABLE' => array( 'TRUNCATE TABLE t' ),
);
}
public function testSelectQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void {
$this->assertReadOnlyQuerySucceedsUnderWriteLock( 'SELECT * FROM t' );
}
public function testShowQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void {
$this->assertReadOnlyQuerySucceedsUnderWriteLock( 'SHOW TABLES' );
}
public function testDescribeQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void {
$this->assertReadOnlyQuerySucceedsUnderWriteLock( 'DESCRIBE t' );
}
public function testSetQueryOpensReadOnlyTransaction(): void {
$driver = $this->create_in_memory_driver();
$driver->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$driver->query( "SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION'" );
$this->assertSame( 'BEGIN', $driver->get_last_sqlite_queries()[0]['sql'] );
}
public function testSetQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void {
// Connection A: set up the database and hold a write transaction.
$conn_a = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
$driver_a = $this->create_driver( $conn_a );
$driver_a->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$driver_a->query( "INSERT INTO t VALUES (1, 'Alice')" );
// Simulate another PHP process holding a write transaction.
$conn_a->get_pdo()->exec( 'BEGIN IMMEDIATE' );
try {
// Connection B with zero timeout — any lock conflict fails immediately.
$conn_b = new WP_SQLite_Connection(
array(
'path' => $this->db_path,
'timeout' => 0,
)
);
$driver_b = $this->create_driver( $conn_b );
$conn_b->get_pdo()->setAttribute( PDO::ATTR_TIMEOUT, 0 );
// SET writes nothing, so it must not contend for the write lock.
$result = $driver_b->query( "SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION'" );
$this->assertSame( 0, $result->rowCount() );
} finally {
$conn_a->get_pdo()->exec( 'ROLLBACK' );
}
}
private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): void {
// Connection A: set up the database.
$conn_a = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
$driver_a = $this->create_driver( $conn_a );
$driver_a->query( 'CREATE TABLE t (id INT, name VARCHAR(255))' );
$driver_a->query( "INSERT INTO t VALUES (1, 'Alice')" );
// Simulate another PHP process holding a write transaction.
$conn_a->get_pdo()->exec( 'BEGIN IMMEDIATE' );
try {
// Connection B with zero timeout — any lock conflict fails immediately.
$conn_b = new WP_SQLite_Connection(
array(
'path' => $this->db_path,
'timeout' => 0,
)
);
$driver_b = $this->create_driver( $conn_b );
$conn_b->get_pdo()->setAttribute( PDO::ATTR_TIMEOUT, 0 );
$result = $driver_b->query( $query );
$this->assertNotEmpty( $result->fetchAll() );
} finally {
$conn_a->get_pdo()->exec( 'ROLLBACK' );
}
}
private function create_in_memory_driver(): WP_MySQL_On_SQLite {
$pdo_class = PHP_VERSION_ID >= 80400 ? PDO\SQLite::class : PDO::class;
$pdo = new $pdo_class( 'sqlite::memory:' );
$connection = new WP_SQLite_Connection( array( 'pdo' => $pdo ) );
return $this->create_driver( $connection );
}
private function create_driver( WP_SQLite_Connection $connection ): WP_MySQL_On_SQLite {
return new WP_MySQL_On_SQLite(
'mysql-on-sqlite:dbname=wp',
null,
null,
array(
'pdo' => $connection->get_pdo(),
'journal_mode' => $connection->query( 'PRAGMA journal_mode' )->fetchColumn(),
)
);
}
}