-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathRetryableTransactionExceptionTest.php
More file actions
150 lines (114 loc) · 5.8 KB
/
RetryableTransactionExceptionTest.php
File metadata and controls
150 lines (114 loc) · 5.8 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
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Database;
use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Database\Exceptions\UniqueConstraintViolationException;
use CodeIgniter\Database\MySQLi\Connection as MySQLiConnection;
use CodeIgniter\Database\OCI8\Connection as OCI8Connection;
use CodeIgniter\Database\Postgre\Connection as PostgreConnection;
use CodeIgniter\Database\SQLite3\Connection as SQLite3Connection;
use CodeIgniter\Database\SQLSRV\Connection as SQLSRVConnection;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockConnection;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use RuntimeException;
/**
* @internal
*/
#[Group('Others')]
final class RetryableTransactionExceptionTest extends CIUnitTestCase
{
#[DataProvider('provideRecognizesRetryableTransactionExceptions')]
public function testRecognizesRetryableTransactionExceptions(BaseConnection $db, int|string $code): void
{
$exception = new DatabaseException('Retryable transaction failure.', $code);
$this->assertTrue($db->isRetryableTransactionException($exception));
}
/**
* @return iterable<string, array{BaseConnection, int|string}>
*/
public static function provideRecognizesRetryableTransactionExceptions(): iterable
{
yield 'MySQLi deadlock' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1213];
yield 'Postgre serialization failure' => [self::connection(PostgreConnection::class, 'Postgre'), '40001'];
yield 'Postgre deadlock' => [self::connection(PostgreConnection::class, 'Postgre'), '40P01'];
yield 'SQLite busy' => [self::connection(SQLite3Connection::class, 'SQLite3'), 5];
yield 'SQLSRV deadlock' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '40001/1205'];
yield 'SQLSRV vendor deadlock' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 1205];
yield 'SQLSRV snapshot isolation conflict' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 'HY000/3960'];
if (defined('OCI_COMMIT_ON_SUCCESS')) {
yield 'OCI8 deadlock' => [self::connection(OCI8Connection::class, 'OCI8'), 60];
yield 'OCI8 serialization failure' => [self::connection(OCI8Connection::class, 'OCI8'), 8177];
}
}
#[DataProvider('provideRejectsNonRetryableTransactionExceptions')]
public function testRejectsNonRetryableTransactionExceptions(BaseConnection $db, int|string $code): void
{
$exception = new DatabaseException('Non-retryable transaction failure.', $code);
$this->assertFalse($db->isRetryableTransactionException($exception));
}
/**
* @return iterable<string, array{BaseConnection, int|string}>
*/
public static function provideRejectsNonRetryableTransactionExceptions(): iterable
{
yield 'Base connection default' => [self::connection(MockConnection::class, 'MockDriver'), 1213];
yield 'MySQLi lock wait timeout' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1205];
yield 'MySQLi duplicate key' => [self::connection(MySQLiConnection::class, 'MySQLi'), 1062];
yield 'Postgre unique violation' => [self::connection(PostgreConnection::class, 'Postgre'), '23505'];
yield 'Postgre exclusion violation' => [self::connection(PostgreConnection::class, 'Postgre'), '23P01'];
yield 'SQLite locked' => [self::connection(SQLite3Connection::class, 'SQLite3'), 6];
yield 'SQLite busy snapshot extended code' => [self::connection(SQLite3Connection::class, 'SQLite3'), 517];
yield 'SQLite constraint' => [self::connection(SQLite3Connection::class, 'SQLite3'), 19];
yield 'SQLSRV lock timeout' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), 'HYT00/1222'];
yield 'SQLSRV SQLSTATE without vendor code' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '40001'];
yield 'SQLSRV unique constraint' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '23000/2627'];
yield 'SQLSRV unique index' => [self::connection(SQLSRVConnection::class, 'SQLSRV'), '23000/2601'];
if (defined('OCI_COMMIT_ON_SUCCESS')) {
yield 'OCI8 resource busy' => [self::connection(OCI8Connection::class, 'OCI8'), 54];
yield 'OCI8 unique constraint' => [self::connection(OCI8Connection::class, 'OCI8'), 1];
}
}
public function testRejectsNonDatabaseExceptions(): void
{
$db = self::connection(MySQLiConnection::class, 'MySQLi');
$this->assertFalse($db->isRetryableTransactionException(new RuntimeException('Not a database exception.')));
}
public function testRejectsUniqueConstraintViolationExceptions(): void
{
$db = self::connection(MySQLiConnection::class, 'MySQLi');
$this->assertFalse($db->isRetryableTransactionException(
new UniqueConstraintViolationException('Duplicate key.', 1062),
));
}
/**
* @param class-string<BaseConnection> $connectionClass
*/
private static function connection(string $connectionClass, string $driver): BaseConnection
{
return new $connectionClass([
'DSN' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => 'test',
'DBDriver' => $driver,
'DBDebug' => true,
'charset' => 'utf8',
'DBCollat' => 'utf8_general_ci',
'swapPre' => '',
'encrypt' => false,
'compress' => false,
'failover' => [],
]);
}
}