@@ -30,6 +30,20 @@ class MutexConcurrencyTest extends \PHPUnit_Framework_TestCase
3030 */
3131 private $ pdo ;
3232
33+ /**
34+ * @var string
35+ */
36+ private $ path ;
37+
38+ protected function tearDown ()
39+ {
40+ if ($ this ->path ) {
41+ unlink ($ this ->path );
42+ }
43+
44+ parent ::tearDown ();
45+ }
46+
3347 /**
3448 * Gets a PDO instance.
3549 *
@@ -102,13 +116,16 @@ public function provideTestHighContention()
102116 {
103117 $ cases = array_map (function (array $ mutexFactory ) {
104118 $ file = tmpfile ();
105- fwrite ($ file , pack ("i " , 0 ));
119+ $ this -> assertEquals ( 4 , fwrite ($ file , pack ("i " , 0 )), " Expected 4 bytes to be written to temporary file. " );
106120
107121 return [
108122 function ($ increment ) use ($ file ) {
109123 rewind ($ file );
110124 flock ($ file , LOCK_EX );
111125 $ data = fread ($ file , 4 );
126+
127+ $ this ->assertEquals (4 , strlen ($ data ), "Expected four bytes to be present in temporary file. " );
128+
112129 $ counter = unpack ("i " , $ data )[1 ];
113130
114131 $ counter += $ increment ;
@@ -209,16 +226,16 @@ public function testSerialisation(callable $mutexFactory)
209226 */
210227 public function provideMutexFactories ()
211228 {
212- $ path = stream_get_meta_data ( tmpfile ())[ " uri " ] ;
213-
229+ $ this -> path = tempnam ( sys_get_temp_dir (), " mutex-concurrency-test " ) ;
230+
214231 $ cases = [
215- "flock " => [function ($ timeout = 3 ) use ( $ path ) {
216- $ file = fopen ($ path , "w " );
232+ "flock " => [function ($ timeout = 3 ) {
233+ $ file = fopen ($ this -> path , "w " );
217234 return new FlockMutex ($ file );
218235 }],
219236
220- "semaphore " => [function ($ timeout = 3 ) use ( $ path ) {
221- $ semaphore = sem_get (ftok ($ path , "b " ));
237+ "semaphore " => [function ($ timeout = 3 ) {
238+ $ semaphore = sem_get (ftok ($ this -> path , "b " ));
222239 $ this ->assertTrue (is_resource ($ semaphore ));
223240 return new SemaphoreMutex ($ semaphore );
224241 }],
@@ -273,6 +290,15 @@ function ($uri) {
273290 return new MySQLMutex ($ pdo , "test " , $ timeout );
274291 }];
275292 }
293+
294+ if (getenv ("PGSQL_DSN " )) {
295+ $ cases ["PgAdvisoryLockMutex " ] = [function () {
296+ $ pdo = new \PDO (getenv ("PGSQL_DSN " ), getenv ("PGSQL_USER " ));
297+ $ pdo ->setAttribute (\PDO ::ATTR_ERRMODE , \PDO ::ERRMODE_EXCEPTION );
298+
299+ return new PgAdvisoryLockMutex ($ pdo , "test " );
300+ }];
301+ }
276302
277303 return $ cases ;
278304 }
0 commit comments