Skip to content

Commit c4d8490

Browse files
committed
Improve the busy timeout concurrency tests
The zero timeout test only checked for the "database is locked" error, which also happens after the 10 second default. It passed even when the option was not forwarded to SQLite. Measure the time until the write fails instead, and test that a 200 ms timeout makes the write wait at least that long. The create_driver() helper built the driver on the connection's PDO without passing the busy timeout, so it reset to the default. Tests that needed a zero timeout had to set it again. Pass it on like the journal mode. Also keep the public test methods above the private helpers.
1 parent f73719a commit c4d8490

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Concurrency_Tests.php‎

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ public function testSetQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void
138138
)
139139
);
140140
$driver_b = $this->create_driver( $conn_b );
141-
$conn_b->get_pdo()->setAttribute( PDO::ATTR_TIMEOUT, 0 );
142141

143142
// SET writes nothing, so it must not contend for the write lock.
144143
$result = $driver_b->query( "SET SESSION sql_mode = 'NO_ENGINE_SUBSTITUTION'" );
@@ -149,6 +148,20 @@ public function testSetQuerySucceedsWhileAnotherConnectionHoldsWriteLock(): void
149148
}
150149
}
151150

151+
public function testWriteQueryFailsImmediatelyWithZeroBusyTimeoutWhileAnotherConnectionHoldsWriteLock(): void {
152+
$elapsed = $this->measure_write_failure_under_write_lock( 0 );
153+
154+
// Far below the 10 second default.
155+
$this->assertLessThan( 1, $elapsed );
156+
}
157+
158+
public function testWriteQueryWaitsForBusyTimeoutWhileAnotherConnectionHoldsWriteLock(): void {
159+
$elapsed = $this->measure_write_failure_under_write_lock( 200 );
160+
161+
$this->assertGreaterThanOrEqual( 0.2, $elapsed );
162+
$this->assertLessThan( 5, $elapsed );
163+
}
164+
152165
private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): void {
153166
// Connection A: set up the database.
154167
$conn_a = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
@@ -168,7 +181,6 @@ private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): voi
168181
)
169182
);
170183
$driver_b = $this->create_driver( $conn_b );
171-
$conn_b->get_pdo()->setAttribute( PDO::ATTR_TIMEOUT, 0 );
172184

173185
$result = $driver_b->query( $query );
174186

@@ -178,7 +190,13 @@ private function assertReadOnlyQuerySucceedsUnderWriteLock( string $query ): voi
178190
}
179191
}
180192

181-
public function testWriteQueryFailsImmediatelyWithZeroBusyTimeoutWhileAnotherConnectionHoldsWriteLock(): void {
193+
/**
194+
* Run a write query while another connection holds the write lock.
195+
*
196+
* @param int $busy_timeout Busy timeout of the writing connection in milliseconds.
197+
* @return float Time in seconds until the write query failed.
198+
*/
199+
private function measure_write_failure_under_write_lock( int $busy_timeout ): float {
182200
// Connection A: set up the database and hold a write transaction.
183201
$conn_a = new WP_SQLite_Connection( array( 'path' => $this->db_path ) );
184202
$driver_a = $this->create_driver( $conn_a );
@@ -189,19 +207,21 @@ public function testWriteQueryFailsImmediatelyWithZeroBusyTimeoutWhileAnotherCon
189207
$conn_a->get_pdo()->exec( 'BEGIN IMMEDIATE' );
190208

191209
try {
192-
// Connection B with a zero busy timeout — any lock conflict fails
193-
// immediately instead of waiting for the default timeout.
194210
$driver_b = new WP_MySQL_On_SQLite(
195211
sprintf( 'mysql-on-sqlite:path=%s;dbname=wp', $this->db_path ),
196212
null,
197213
null,
198-
array( 'sqlite_busy_timeout' => 0 )
214+
array( 'sqlite_busy_timeout' => $busy_timeout )
199215
);
200216

201-
$this->expectException( PDOException::class );
202-
$this->expectExceptionMessage( 'database is locked' );
203-
204-
$driver_b->query( "INSERT INTO t VALUES (2, 'Bob')" );
217+
$start = microtime( true );
218+
try {
219+
$driver_b->query( "INSERT INTO t VALUES (2, 'Bob')" );
220+
} catch ( PDOException $e ) {
221+
$this->assertStringContainsString( 'database is locked', $e->getMessage() );
222+
return microtime( true ) - $start;
223+
}
224+
$this->fail( 'The write query did not fail while another connection held the write lock.' );
205225
} finally {
206226
$conn_a->get_pdo()->exec( 'ROLLBACK' );
207227
}
@@ -221,6 +241,7 @@ private function create_driver( WP_SQLite_Connection $connection ): WP_MySQL_On_
221241
null,
222242
array(
223243
'sqlite_pdo' => $connection->get_pdo(),
244+
'sqlite_busy_timeout' => (int) $connection->query( 'PRAGMA busy_timeout' )->fetchColumn(),
224245
'sqlite_journal_mode' => $connection->query( 'PRAGMA journal_mode' )->fetchColumn(),
225246
)
226247
);

0 commit comments

Comments
 (0)