@@ -106,20 +106,18 @@ public function testHighContention(\Closure $code, \Closure $mutexFactory): void
106
106
}
107
107
108
108
/**
109
- * Returns test cases for testHighContention().
110
- *
111
109
* @return iterable<list<mixed>>
112
110
*/
113
111
public static function provideHighContentionCases (): iterable
114
112
{
115
- $ cases = array_map (static function ( array $ mutexFactory): array {
113
+ foreach (static :: provideExecutionIsSerializedWhenLockedCases () as [ $ mutexFactory]) {
116
114
$ filename = tempnam (sys_get_temp_dir (), 'php-lock-high-contention ' );
117
115
118
116
static ::$ temporaryFiles [] = $ filename ;
119
117
120
118
file_put_contents ($ filename , '0 ' );
121
119
122
- return [
120
+ yield [
123
121
static function (int $ increment ) use ($ filename ): int {
124
122
$ counter = file_get_contents ($ filename );
125
123
$ counter += $ increment ;
@@ -128,11 +126,11 @@ static function (int $increment) use ($filename): int {
128
126
129
127
return $ counter ;
130
128
},
131
- $ mutexFactory[ 0 ] ,
129
+ $ mutexFactory ,
132
130
];
133
- }, static :: provideExecutionIsSerializedWhenLockedCases ());
131
+ }
134
132
135
- $ addPDO = static function ($ dsn , $ user , $ password , $ vendor ) use (& $ cases ) {
133
+ $ addPDO = static function ($ dsn , $ user , $ password , $ vendor ) {
136
134
$ pdo = self ::getPDO ($ dsn , $ user , $ password );
137
135
138
136
$ options = ['mysql ' => 'engine=InnoDB ' ];
@@ -146,7 +144,7 @@ static function (int $increment) use ($filename): int {
146
144
147
145
self ::$ pdo = null ;
148
146
149
- $ cases [ $ vendor ] = [
147
+ return [
150
148
static function (int $ increment ) use ($ dsn , $ user , $ password ) {
151
149
// This prevents using a closed connection from a child.
152
150
if ($ increment === 0 ) {
@@ -178,17 +176,15 @@ static function ($timeout = 3) use ($dsn, $user, $password) {
178
176
$ dsn = getenv ('MYSQL_DSN ' );
179
177
$ user = getenv ('MYSQL_USER ' );
180
178
$ password = getenv ('MYSQL_PASSWORD ' );
181
- $ addPDO ($ dsn , $ user , $ password , 'mysql ' );
179
+ yield ' mysql ' => $ addPDO ($ dsn , $ user , $ password , 'mysql ' );
182
180
}
183
181
184
182
if (getenv ('PGSQL_DSN ' )) {
185
183
$ dsn = getenv ('PGSQL_DSN ' );
186
184
$ user = getenv ('PGSQL_USER ' );
187
185
$ password = getenv ('PGSQL_PASSWORD ' );
188
- $ addPDO ($ dsn , $ user , $ password , 'postgres ' );
186
+ yield ' postgres ' => $ addPDO ($ dsn , $ user , $ password , 'postgres ' );
189
187
}
190
-
191
- return $ cases ;
192
188
}
193
189
194
190
/**
@@ -225,45 +221,43 @@ public static function provideExecutionIsSerializedWhenLockedCases(): iterable
225
221
226
222
self ::$ temporaryFiles [] = $ filename ;
227
223
228
- $ cases = [
229
- 'flock ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
230
- $ file = fopen ($ filename , 'w ' );
231
-
232
- return new FlockMutex ($ file );
233
- }],
234
-
235
- 'flockWithTimoutPcntl ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
236
- $ file = fopen ($ filename , 'w ' );
237
- $ lock = Liberator::liberate (new FlockMutex ($ file , $ timeout ));
238
- $ lock ->strategy = FlockMutex::STRATEGY_PCNTL ; // @phpstan-ignore property.notFound
239
-
240
- return $ lock ->popsValue ();
241
- }],
242
-
243
- 'flockWithTimoutBusy ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
244
- $ file = fopen ($ filename , 'w ' );
245
- $ lock = Liberator::liberate (new FlockMutex ($ file , $ timeout ));
246
- $ lock ->strategy = FlockMutex::STRATEGY_BUSY ; // @phpstan-ignore property.notFound
247
-
248
- return $ lock ->popsValue ();
249
- }],
250
-
251
- 'semaphore ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
252
- $ semaphore = sem_get (ftok ($ filename , 'b ' ));
253
- self ::assertThat (
254
- $ semaphore ,
255
- self ::logicalOr (
256
- self ::isInstanceOf (\SysvSemaphore::class),
257
- new IsType (IsType::TYPE_RESOURCE )
258
- )
259
- );
224
+ yield 'flock ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
225
+ $ file = fopen ($ filename , 'w ' );
226
+
227
+ return new FlockMutex ($ file );
228
+ }];
260
229
261
- return new SemaphoreMutex ($ semaphore );
262
- }],
263
- ];
230
+ yield 'flockWithTimoutPcntl ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
231
+ $ file = fopen ($ filename , 'w ' );
232
+ $ lock = Liberator::liberate (new FlockMutex ($ file , $ timeout ));
233
+ $ lock ->strategy = FlockMutex::STRATEGY_PCNTL ; // @phpstan-ignore property.notFound
234
+
235
+ return $ lock ->popsValue ();
236
+ }];
237
+
238
+ yield 'flockWithTimoutBusy ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
239
+ $ file = fopen ($ filename , 'w ' );
240
+ $ lock = Liberator::liberate (new FlockMutex ($ file , $ timeout ));
241
+ $ lock ->strategy = FlockMutex::STRATEGY_BUSY ; // @phpstan-ignore property.notFound
242
+
243
+ return $ lock ->popsValue ();
244
+ }];
245
+
246
+ yield 'semaphore ' => [static function ($ timeout = 3 ) use ($ filename ): Mutex {
247
+ $ semaphore = sem_get (ftok ($ filename , 'b ' ));
248
+ self ::assertThat (
249
+ $ semaphore ,
250
+ self ::logicalOr (
251
+ self ::isInstanceOf (\SysvSemaphore::class),
252
+ new IsType (IsType::TYPE_RESOURCE )
253
+ )
254
+ );
255
+
256
+ return new SemaphoreMutex ($ semaphore );
257
+ }];
264
258
265
259
if (getenv ('MEMCACHE_HOST ' )) {
266
- $ cases [ 'memcached ' ] = [static function ($ timeout = 3 ): Mutex {
260
+ yield 'memcached ' => [static function ($ timeout = 3 ): Mutex {
267
261
$ memcached = new \Memcached ();
268
262
$ memcached ->addServer (getenv ('MEMCACHE_HOST ' ), 11211 );
269
263
@@ -274,7 +268,7 @@ public static function provideExecutionIsSerializedWhenLockedCases(): iterable
274
268
if (getenv ('REDIS_URIS ' )) {
275
269
$ uris = explode (', ' , getenv ('REDIS_URIS ' ));
276
270
277
- $ cases [ 'PredisMutex ' ] = [static function ($ timeout = 3 ) use ($ uris ): Mutex {
271
+ yield 'PredisMutex ' => [static function ($ timeout = 3 ) use ($ uris ): Mutex {
278
272
$ clients = array_map (
279
273
static fn ($ uri ) => new Client ($ uri ),
280
274
$ uris
@@ -284,7 +278,7 @@ public static function provideExecutionIsSerializedWhenLockedCases(): iterable
284
278
}];
285
279
286
280
if (class_exists (\Redis::class)) {
287
- $ cases [ 'PHPRedisMutex ' ] = [
281
+ yield 'PHPRedisMutex ' => [
288
282
static function ($ timeout = 3 ) use ($ uris ): Mutex {
289
283
$ apis = array_map (
290
284
static function (string $ uri ): \Redis {
@@ -312,7 +306,7 @@ static function (string $uri): \Redis {
312
306
}
313
307
314
308
if (getenv ('MYSQL_DSN ' )) {
315
- $ cases [ 'MySQLMutex ' ] = [static function ($ timeout = 3 ): Mutex {
309
+ yield 'MySQLMutex ' => [static function ($ timeout = 3 ): Mutex {
316
310
$ pdo = new \PDO (getenv ('MYSQL_DSN ' ), getenv ('MYSQL_USER ' ), getenv ('MYSQL_PASSWORD ' ));
317
311
$ pdo ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
318
312
@@ -321,14 +315,12 @@ static function (string $uri): \Redis {
321
315
}
322
316
323
317
if (getenv ('PGSQL_DSN ' )) {
324
- $ cases [ 'PgAdvisoryLockMutex ' ] = [static function (): Mutex {
318
+ yield 'PgAdvisoryLockMutex ' => [static function (): Mutex {
325
319
$ pdo = new \PDO (getenv ('PGSQL_DSN ' ), getenv ('PGSQL_USER ' ), getenv ('PGSQL_PASSWORD ' ));
326
320
$ pdo ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
327
321
328
322
return new PgAdvisoryLockMutex ($ pdo , 'test ' );
329
323
}];
330
324
}
331
-
332
- return $ cases ;
333
325
}
334
326
}
0 commit comments