Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue when using flock #126

Open
joshuaspence opened this issue Dec 29, 2015 · 2 comments
Open

Issue when using flock #126

joshuaspence opened this issue Dec 29, 2015 · 2 comments

Comments

@joshuaspence
Copy link

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)
@mikey179
Copy link
Member

mikey179 commented Jan 6, 2016

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.

@yurii-github
Copy link

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
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants