vfsStream
vfsStream copied to clipboard
Issue when using flock
I have an issue using vfsStream with code that calls flock
. The issue is best explained by the following code example:
<?php
require __DIR__.'/vendor/autoload.php';
use org\bovigo\vfs\vfsStream;
$root = vfsStream::setUp();
$path = $root->url().'/x';
//$path = '/tmp/x';
$handle_one = fopen($path, 'a+');
$handle_two = fopen($path, 'a+');
$would_block = null;
flock($handle_one, LOCK_EX | LOCK_NB, $would_block);
var_dump($would_block);
flock($handle_two, LOCK_EX | LOCK_NB, $would_block);
var_dump($would_block);
Using vfsStream (i.e. $path = $root->url().'/x'
), the output from this script is:
int(0)
int(0)
Using $path = '/tmp/x'
, the output from this script is:
int(0)
int(1)
I guess it is due to the fact the vfsStream doesn't block. It could be done, but currently it is not implemented. Also see #15.
today I came across this stuff myself. Any solution/workaround to this? I have checked vfsStream code and some lock logic is in place but I am not sure where to start digging for solution.
regards
"name": "mikey179/vfsstream",
"version": "v1.6.8",
"source": {
"type": "git",
"url": "https://github.com/bovigo/vfsStream.git",
"reference": "231c73783ebb7dd9ec77916c10037eff5a2b6efe"
},
<?php
namespace Tests;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
class VirtualFileSystemTest extends TestCase
{
public function testRealFS()
{
$filename = '/tmp/a.txt';
$stream1 = fopen($filename, 'a+');
$result1 = flock($stream1, LOCK_EX | LOCK_NB, $lock1);
$stream2 = fopen($filename, 'a+');
$result2 = flock($stream2, LOCK_EX | LOCK_NB, $lock2);
$this->assertFileExists($filename);
$this->assertSame(0, $lock1);
$this->assertTrue($result1);
$this->assertSame(1, $lock2);
$this->assertFalse($result2);
}
public function testVirtualFS()
{
$root = vfsStream::setup();
$filename = vfsStream::newFile('a.txt')->at($root)->url();
$stream1 = fopen($filename, 'a+');
$result1 = flock($stream1, LOCK_EX | LOCK_NB, $lock1);
$stream2 = fopen($filename, 'a+');
$result2 = flock($stream2, LOCK_EX | LOCK_NB, $lock2);
$this->assertFileExists($filename);
$this->assertSame(0, $lock1);
$this->assertTrue($result1);
$this->assertSame(1, $lock2); // this FAILS, because it is 0
$this->assertFalse($result2); // this passes
}
}