@@ -30,6 +30,20 @@ class MutexConcurrencyTest extends \PHPUnit_Framework_TestCase
30
30
*/
31
31
private $ pdo ;
32
32
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
+
33
47
/**
34
48
* Gets a PDO instance.
35
49
*
@@ -102,13 +116,16 @@ public function provideTestHighContention()
102
116
{
103
117
$ cases = array_map (function (array $ mutexFactory ) {
104
118
$ 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. " );
106
120
107
121
return [
108
122
function ($ increment ) use ($ file ) {
109
123
rewind ($ file );
110
124
flock ($ file , LOCK_EX );
111
125
$ data = fread ($ file , 4 );
126
+
127
+ $ this ->assertEquals (4 , strlen ($ data ), "Expected four bytes to be present in temporary file. " );
128
+
112
129
$ counter = unpack ("i " , $ data )[1 ];
113
130
114
131
$ counter += $ increment ;
@@ -209,16 +226,16 @@ public function testSerialisation(callable $mutexFactory)
209
226
*/
210
227
public function provideMutexFactories ()
211
228
{
212
- $ path = stream_get_meta_data ( tmpfile ())[ " uri " ] ;
213
-
229
+ $ this -> path = tempnam ( sys_get_temp_dir (), " mutex-concurrency-test " ) ;
230
+
214
231
$ cases = [
215
- "flock " => [function ($ timeout = 3 ) use ( $ path ) {
216
- $ file = fopen ($ path , "w " );
232
+ "flock " => [function ($ timeout = 3 ) {
233
+ $ file = fopen ($ this -> path , "w " );
217
234
return new FlockMutex ($ file );
218
235
}],
219
236
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 " ));
222
239
$ this ->assertTrue (is_resource ($ semaphore ));
223
240
return new SemaphoreMutex ($ semaphore );
224
241
}],
@@ -273,6 +290,15 @@ function ($uri) {
273
290
return new MySQLMutex ($ pdo , "test " , $ timeout );
274
291
}];
275
292
}
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
+ }
276
302
277
303
return $ cases ;
278
304
}
0 commit comments