Skip to content

Commit

Permalink
Fixed durability not working for first iteration of generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Mar 16, 2017
1 parent c13eb95 commit 14cca19
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Porter.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ private function fetch(ProviderResource $resource, CacheAdvice $cacheAdvice, $fe
if (($records = \ScriptFUSION\Retry\retry(
$fetchAttempts,
function () use ($provider, $resource) {
return $provider->fetch($resource);
if (($records = $provider->fetch($resource)) instanceof \Iterator) {
// Force generator to run until first yield to provoke an exception.
$records->valid();
}

return $records;
},
function (\Exception $exception) use ($fetchExceptionHandler) {
// Throw exception if unrecoverable.
Expand Down
17 changes: 17 additions & 0 deletions test/Integration/Porter/PorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,23 @@ public function testCustomFetchExceptionHandler()
$this->porter->import($this->specification);
}

/**
* Tests that when a generator throws a recoverable exception before the first yield, the fetch is retried.
*
* Note this does not support cases where exceptions may be thrown in subsequent iterations.
*/
public function testGeneratorException()
{
$this->provider->shouldReceive('fetch')->once()->andReturnUsing(function () {
throw new RecoverableConnectorException;

yield;
});

$this->setExpectedException(FailingTooHardException::class, '1');
$this->porter->import($this->specification->setMaxFetchAttempts(1));
}

#endregion

public function testFilter()
Expand Down

0 comments on commit 14cca19

Please sign in to comment.