Skip to content

Commit

Permalink
Merge pull request #39 from kevin-thackorie/concurrentCacheFile
Browse files Browse the repository at this point in the history
Add graceful handling of cache write failure due to already existent cache file caused by race condition.
  • Loading branch information
reinink committed Feb 26, 2015
2 parents 9856010 + 522d2b8 commit abf2864
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace League\Glide;

use InvalidArgumentException;
use League\Flysystem\FileExistsException;
use League\Flysystem\FilesystemInterface;
use League\Glide\Api\ApiInterface;
use League\Glide\Filesystem\FilesystemException;
Expand Down Expand Up @@ -299,10 +300,15 @@ public function makeImage()
);
}

$write = $this->cache->write(
$this->getCachePath($request),
$this->api->run($request, $source)
);
try {
$write = $this->cache->write(
$this->getCachePath($request),
$this->api->run($request, $source)
);
} catch (FileExistsException $exception) {
// Cache file failed to write. Fail silently.
return $request;
}

if ($write === false) {
throw new FilesystemException(
Expand Down
19 changes: 19 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ public function testMakeImageWithUnwritableCache()
$this->server->makeImage('image.jpg');
}

public function testMakeImageWithExistingCacheFile()
{
$this->server->setSource(Mockery::mock('League\Flysystem\FilesystemInterface', function ($mock) {
$mock->shouldReceive('has')->andReturn(true)->once();
$mock->shouldReceive('read')->andReturn('content')->once();
}));

$this->server->setCache(Mockery::mock('League\Flysystem\FilesystemInterface', function ($mock) {
$mock->shouldReceive('has')->andReturn(false)->once();
$mock->shouldReceive('write')->andThrow(new \League\Flysystem\FileExistsException('75094881e9fd2b93063d6a5cb083091c'));
}));

$this->server->setApi(Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) {
$mock->shouldReceive('run')->andReturn('content')->once();
}));

$this->assertInstanceOf('Symfony\Component\HttpFoundation\Request', $this->server->makeImage('image.jpg'));
}

public function testMakeImageFromSource()
{
$this->server->setSource(Mockery::mock('League\Flysystem\FilesystemInterface', function ($mock) {
Expand Down

0 comments on commit abf2864

Please sign in to comment.