66use PHPUnit \Framework \TestCase ;
77use Predis \Client ;
88use Redis ;
9- use Spork \ ProcessManager ;
9+ use Spatie \ Async \ Pool ;
1010
1111/**
1212 * Concurrency Tests for Mutex.
@@ -69,16 +69,15 @@ private function getPDO(string $dsn, string $user): \PDO
6969 * @param int $concurrency The amount of forks.
7070 * @param callable $code The code for the fork.
7171 */
72- private function fork ($ concurrency , callable $ code )
72+ private function fork (int $ concurrency , callable $ code )
7373 {
74- $ manager = new ProcessManager ();
75- $ manager ->setDebug (true );
74+ $ pool = Pool::create ();
7675
7776 for ($ i = 0 ; $ i < $ concurrency ; $ i ++) {
78- $ manager -> fork ($ code );
77+ $ pool [] = async ($ code );
7978 }
8079
81- $ manager -> check ( );
80+ await ( $ pool );
8281 }
8382
8483 /**
@@ -92,8 +91,8 @@ private function fork($concurrency, callable $code)
9291 */
9392 public function testHighContention (callable $ code , callable $ mutexFactory )
9493 {
95- $ concurrency = 2 ;
96- $ iterations = 20000 / $ concurrency ;
94+ $ concurrency = 10 ;
95+ $ iterations = 1000 / $ concurrency ;
9796 $ timeout = $ concurrency * 20 ;
9897
9998 $ this ->fork ($ concurrency , function () use ($ mutexFactory , $ timeout , $ iterations , $ code ): void {
@@ -112,31 +111,25 @@ public function testHighContention(callable $code, callable $mutexFactory)
112111
113112 /**
114113 * Returns test cases for testHighContention().
115- *
116- * @return array The test cases.
117114 */
118- public function provideTestHighContention ()
115+ public function provideTestHighContention (): array
119116 {
120117 $ cases = array_map (function (array $ mutexFactory ) {
121- $ file = tmpfile ();
122- $ this ->assertEquals (4 , fwrite ($ file , pack ('i ' , 0 )), 'Expected 4 bytes to be written to temporary file. ' );
118+ $ filename = tempnam (sys_get_temp_dir (), 'php-lock ' );
119+
120+ file_put_contents ($ filename , pack ('i ' , 0 ));
123121
124122 return [
125- function (int $ increment ) use ($ file ): int {
126- rewind ($ file );
127- flock ($ file , LOCK_EX );
128- $ data = fread ($ file , 4 );
123+ function (int $ increment ) use ($ filename ): int {
124+ $ data = file_get_contents ($ filename );
129125
130126 $ this ->assertEquals (4 , strlen ($ data ), 'Expected four bytes to be present in temporary file. ' );
131127
132128 $ counter = unpack ('i ' , $ data )[1 ];
133129
134130 $ counter += $ increment ;
135131
136- rewind ($ file );
137- fwrite ($ file , pack ('i ' , $ counter ));
138-
139- flock ($ file , LOCK_UN );
132+ file_put_contents ($ filename , pack ('i ' , $ counter ));
140133
141134 return $ counter ;
142135 },
0 commit comments