Skip to content

Commit

Permalink
Added explicit file unlock. Fix in deleting locked files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivopetkov committed Nov 11, 2017
1 parent d4ddf81 commit 0e91967
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 10 deletions.
6 changes: 5 additions & 1 deletion src/ObjectStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ObjectStorage
/**
* The current library version
*/
const VERSION = '0.3.0';
const VERSION = '0.3.1';

/**
* The directory where the objects will be stored
Expand Down Expand Up @@ -437,6 +437,7 @@ public function execute(array $commands): array
$filePointer = fopen($filename, "r");
flock($filePointer, LOCK_SH);
$content = fread($filePointer, filesize($filename));
flock($filePointer, LOCK_UN);
fclose($filePointer);
return $content;
}
Expand Down Expand Up @@ -689,6 +690,8 @@ public function execute(array $commands): array
};
} elseif ($command === 'delete') {
$key = $getProperty('key', true);
$prepareFileForWriting($this->objectsDir . $key);
$prepareFileForWriting($this->metadataDir . $key);
$functions[$index] = function() use ($key, $deleteFile) {
$deleteFile($this->objectsDir . $key);
$deleteFile($this->metadataDir . $key);
Expand Down Expand Up @@ -923,6 +926,7 @@ public function execute(array $commands): array
}

foreach ($filePointers as $filename => $filePointer) {
flock($filePointer, LOCK_UN);
fclose($filePointer);
}
unset($filePointers);
Expand Down
45 changes: 36 additions & 9 deletions tests/KeyCollisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ public function testFileWhenThereIsDirWithTheSameKey()
//echo "\n\n" . var_export($result) . "\n\n" . getState() . "\n\n";
}

/**
*
*/
public function testFileWhenThereIsDirWithTheSameKeyCleanUp()
{
$this->removeDataDir();
}

/**
*
*/
Expand Down Expand Up @@ -84,9 +76,44 @@ public function testDirWhenThereIsFileWithTheSameKey()
/**
*
*/
public function testDirWhenThereIsFileWithTheSameKeyCleanUp()
public function testGetKeyWhenItsDir()
{
$this->removeDataDir();

$objectStorage = $this->getInstance();

$this->createFile($this->getDataDir() . '/objects/data1/key1', 'content');
$result = $objectStorage->get([
'key' => 'data1',
'result' => ['key', 'body']
]);

$this->assertTrue($result === []);

$this->assertTrue($this->checkState('3fb020a8835e135fb296ad20b43ba773
objects/data1/key1: content
'));
}

/**
*
*/
public function testDeleteKeyWhenItsDir()
{
$this->removeDataDir();

$objectStorage = $this->getInstance();

$this->createFile($this->getDataDir() . '/objects/data1/key1', 'content');

$this->assertTrue($this->checkState('3fb020a8835e135fb296ad20b43ba773
objects/data1/key1: content
'));

$this->setExpectedException('\IvoPetkov\ObjectStorage\ErrorException');
$objectStorage->delete([
'key' => 'data1'
]);
}

}
22 changes: 22 additions & 0 deletions tests/LocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,26 @@ public function testLockedKeys2()
);
}

/**
*
*/
public function testLockedKeys3()
{
$this->removeDataDir();
$objectStorage = new \IvoPetkov\ObjectStorage($this->getDataDir());
$objectStorage->set(
[
'key' => 'lockeddata3',
'body' => 'data'
]
);
$this->lockFile('lockeddata3');
$this->setExpectedException('\IvoPetkov\ObjectStorage\ObjectLockedException');
$objectStorage->delete(
[
'key' => 'lockeddata3',
]
);
}

}

0 comments on commit 0e91967

Please sign in to comment.