File: 0002-Patch-out-tests-that-use-Spatie-Async.patch

package info (click to toggle)
php-malkusch-lock 2.3.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 440 kB
  • sloc: php: 2,124; makefile: 19
file content (334 lines) | stat: -rw-r--r-- 11,457 bytes parent folder | download
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
From: James Valleroy <jvalleroy@mailbox.org>
Date: Thu, 30 Dec 2021 10:29:20 -0500
Subject: Drop tests that use Spatie\Async

Not currently packaged.

Forwarded: not-needed
---
 tests/mutex/MutexConcurrencyTest.php | 316 -----------------------------------
 1 file changed, 316 deletions(-)
 delete mode 100644 tests/mutex/MutexConcurrencyTest.php

diff --git a/tests/mutex/MutexConcurrencyTest.php b/tests/mutex/MutexConcurrencyTest.php
deleted file mode 100644
index cd874f2..0000000
--- a/tests/mutex/MutexConcurrencyTest.php
+++ /dev/null
@@ -1,316 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace malkusch\lock\Tests\mutex;
-
-use malkusch\lock\mutex\FlockMutex;
-use malkusch\lock\mutex\MemcachedMutex;
-use malkusch\lock\mutex\Mutex;
-use malkusch\lock\mutex\MySQLMutex;
-use malkusch\lock\mutex\PgAdvisoryLockMutex;
-use malkusch\lock\mutex\PHPRedisMutex;
-use malkusch\lock\mutex\PredisMutex;
-use malkusch\lock\mutex\SemaphoreMutex;
-use malkusch\lock\mutex\TransactionalMutex;
-use PHPUnit\Framework\Attributes\DataProvider;
-use PHPUnit\Framework\Constraint\IsType;
-use PHPUnit\Framework\TestCase;
-use Predis\Client;
-use Spatie\Async\Pool;
-
-/**
- * If you want to run integration tests you should provide these environment variables:
- *
- * - MEMCACHE_HOST
- * - REDIS_URIS - a comma separated list of redis:// URIs.
- * - MYSQL_DSN, MYSQL_USER, MYSQL_PASSWORD
- * - PGSQL_DSN, PGSQL_USER, PGSQL_PASSWORD
- */
-class MutexConcurrencyTest extends TestCase
-{
-    /** @var list<string> */
-    protected static $temporaryFiles = [];
-    /** @var \PDO|null */
-    private static $pdo;
-
-    #[\Override]
-    public static function tearDownAfterClass(): void
-    {
-        foreach (self::$temporaryFiles as $temporaryFile) {
-            unlink($temporaryFile);
-        }
-        self::$temporaryFiles = [];
-
-        self::$pdo = null;
-
-        parent::tearDownAfterClass();
-    }
-
-    /**
-     * Gets a PDO instance.
-     */
-    private static function getPDO(string $dsn, string $user, string $password): \PDO
-    {
-        if (self::$pdo === null) {
-            self::$pdo = new \PDO($dsn, $user, $password);
-            self::$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
-        }
-
-        return self::$pdo;
-    }
-
-    /**
-     * Forks, runs code in the children and wait until all finished.
-     *
-     * @param \Closure(): void $code The code for the fork
-     */
-    private function fork(int $concurrency, \Closure $code): void
-    {
-        $pool = Pool::create();
-
-        for ($i = 0; $i < $concurrency; ++$i) {
-            $pool[] = async($code);
-        }
-
-        await($pool);
-    }
-
-    /**
-     * Tests high contention empirically.
-     *
-     * @param \Closure(0|1): int     $code         The counter code
-     * @param \Closure(float): Mutex $mutexFactory
-     * @param \Closure(): void       $setUp
-     *
-     * @dataProvider provideHighContentionCases
-     */
-    #[DataProvider('provideHighContentionCases')]
-    public function testHighContention(\Closure $code, \Closure $mutexFactory, ?\Closure $setUp = null): void
-    {
-        if ($setUp !== null) {
-            $setUp();
-        }
-
-        $concurrency = 10;
-        $iterations = 1000 / $concurrency;
-        $timeout = $concurrency * 20;
-
-        $this->fork($concurrency, static function () use ($mutexFactory, $timeout, $iterations, $code): void {
-            $mutex = $mutexFactory($timeout);
-            for ($i = 0; $i < $iterations; ++$i) {
-                $mutex->synchronized(static function () use ($code): void {
-                    $code(1);
-                });
-            }
-        });
-
-        $counter = $code(0);
-        self::assertSame($concurrency * $iterations, $counter);
-    }
-
-    /**
-     * @return iterable<list<mixed>>
-     */
-    public static function provideHighContentionCases(): iterable
-    {
-        foreach (static::provideExecutionIsSerializedWhenLockedCases() as $name => [$mutexFactory]) {
-            $filename = tempnam(sys_get_temp_dir(), 'php-lock-high-contention');
-
-            static::$temporaryFiles[] = $filename;
-
-            yield $name => [
-                static function (int $increment) use ($filename): int {
-                    $counter = file_get_contents($filename);
-                    $counter += $increment;
-
-                    file_put_contents($filename, $counter);
-
-                    return $counter;
-                },
-                $mutexFactory,
-                static function () use ($filename): void {
-                    file_put_contents($filename, '0');
-                },
-            ];
-        }
-
-        $makePDOCase = static function (string $dsn, string $user, string $password, string $vendor) {
-            $pdo = self::getPDO($dsn, $user, $password);
-
-            $options = ['mysql' => 'engine=InnoDB'];
-            $option = $options[$vendor] ?? '';
-            $pdo->exec('CREATE TABLE IF NOT EXISTS counter(id INT PRIMARY KEY, counter INT) ' . $option);
-
-            self::$pdo = null;
-
-            return [
-                static function (int $increment) use ($dsn, $user, $password) {
-                    // This prevents using a closed connection from a child.
-                    if ($increment === 0) {
-                        self::$pdo = null;
-                    }
-                    $pdo = self::getPDO($dsn, $user, $password);
-                    $id = 1;
-                    $select = $pdo->prepare('SELECT counter FROM counter WHERE id = ? FOR UPDATE');
-                    $select->execute([$id]);
-                    $counter = $select->fetchColumn();
-
-                    $counter += $increment;
-
-                    $pdo->prepare('UPDATE counter SET counter = ? WHERE id = ?')
-                        ->execute([$counter, $id]);
-
-                    return $counter;
-                },
-                static function ($timeout) use ($dsn, $user, $password) {
-                    self::$pdo = null;
-                    $pdo = self::getPDO($dsn, $user, $password);
-
-                    return new TransactionalMutex($pdo, $timeout);
-                },
-                static function () use ($pdo): void {
-                    $pdo->beginTransaction();
-                    $pdo->exec('DELETE FROM counter');
-                    $pdo->exec('INSERT INTO counter VALUES (1, 0)');
-                    $pdo->commit();
-                },
-            ];
-        };
-
-        if (getenv('MYSQL_DSN')) {
-            $dsn = getenv('MYSQL_DSN');
-            $user = getenv('MYSQL_USER');
-            $password = getenv('MYSQL_PASSWORD');
-            yield 'mysql' => $makePDOCase($dsn, $user, $password, 'mysql');
-        }
-
-        if (getenv('PGSQL_DSN')) {
-            $dsn = getenv('PGSQL_DSN');
-            $user = getenv('PGSQL_USER');
-            $password = getenv('PGSQL_PASSWORD');
-            yield 'postgres' => $makePDOCase($dsn, $user, $password, 'postgres');
-        }
-    }
-
-    /**
-     * Tests that five processes run sequentially.
-     *
-     * @param \Closure(float): Mutex $mutexFactory
-     *
-     * @dataProvider provideExecutionIsSerializedWhenLockedCases
-     */
-    #[DataProvider('provideExecutionIsSerializedWhenLockedCases')]
-    public function testExecutionIsSerializedWhenLocked(\Closure $mutexFactory): void
-    {
-        $time = \microtime(true);
-
-        $this->fork(6, static function () use ($mutexFactory): void {
-            $mutex = $mutexFactory(3);
-            $mutex->synchronized(static function (): void {
-                \usleep(200 * 1000);
-            });
-        });
-
-        $delta = \microtime(true) - $time;
-        self::assertGreaterThan(1.201, $delta);
-    }
-
-    /**
-     * Provides Mutex factories.
-     *
-     * @return iterable<list<mixed>>
-     */
-    public static function provideExecutionIsSerializedWhenLockedCases(): iterable
-    {
-        $filename = tempnam(sys_get_temp_dir(), 'mutex-concurrency-test');
-
-        self::$temporaryFiles[] = $filename;
-
-        yield 'flock' => [static function ($timeout) use ($filename): Mutex {
-            $file = fopen($filename, 'w');
-
-            return new FlockMutex($file, $timeout);
-        }];
-
-        yield 'semaphore' => [static function () use ($filename): Mutex {
-            $semaphore = sem_get(ftok($filename, 'b'));
-            self::assertThat(
-                $semaphore,
-                self::logicalOr(
-                    self::isInstanceOf(\SysvSemaphore::class),
-                    new IsType(IsType::TYPE_RESOURCE)
-                )
-            );
-
-            return new SemaphoreMutex($semaphore);
-        }];
-
-        if (getenv('MEMCACHE_HOST')) {
-            yield 'memcached' => [static function ($timeout): Mutex {
-                $memcached = new \Memcached();
-                $memcached->addServer(getenv('MEMCACHE_HOST'), 11211);
-
-                return new MemcachedMutex('test', $memcached, $timeout);
-            }];
-        }
-
-        if (getenv('REDIS_URIS')) {
-            $uris = explode(',', getenv('REDIS_URIS'));
-
-            yield 'PredisMutex' => [static function ($timeout) use ($uris): Mutex {
-                $clients = array_map(
-                    static fn ($uri) => new Client($uri),
-                    $uris
-                );
-
-                return new PredisMutex($clients, 'test', $timeout);
-            }];
-
-            if (class_exists(\Redis::class)) {
-                yield 'PHPRedisMutex' => [
-                    static function ($timeout) use ($uris): Mutex {
-                        $apis = array_map(
-                            static function (string $uri): \Redis {
-                                $redis = new \Redis();
-
-                                $uri = parse_url($uri);
-                                $redis->connect($uri['host'], $uri['port'] ?? 6379);
-                                if (!empty($uri['pass'])) {
-                                    $redis->auth(
-                                        empty($uri['user'])
-                                        ? $uri['pass']
-                                        : [$uri['user'], $uri['pass']]
-                                    );
-                                }
-
-                                return $redis;
-                            },
-                            $uris
-                        );
-
-                        return new PHPRedisMutex($apis, 'test', $timeout);
-                    },
-                ];
-            }
-        }
-
-        if (getenv('MYSQL_DSN')) {
-            yield 'MySQLMutex' => [static function ($timeout): Mutex {
-                $pdo = new \PDO(getenv('MYSQL_DSN'), getenv('MYSQL_USER'), getenv('MYSQL_PASSWORD'));
-                $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
-
-                return new MySQLMutex($pdo, 'test', $timeout);
-            }];
-        }
-
-        if (getenv('PGSQL_DSN')) {
-            yield 'PgAdvisoryLockMutex' => [static function (): Mutex {
-                $pdo = new \PDO(getenv('PGSQL_DSN'), getenv('PGSQL_USER'), getenv('PGSQL_PASSWORD'));
-                $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
-
-                return new PgAdvisoryLockMutex($pdo, 'test');
-            }];
-        }
-    }
-}