Skip to content

Commit 14cca19

Browse files
committed
Fixed durability not working for first iteration of generator.
1 parent c13eb95 commit 14cca19

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Porter.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ private function fetch(ProviderResource $resource, CacheAdvice $cacheAdvice, $fe
9696
if (($records = \ScriptFUSION\Retry\retry(
9797
$fetchAttempts,
9898
function () use ($provider, $resource) {
99-
return $provider->fetch($resource);
99+
if (($records = $provider->fetch($resource)) instanceof \Iterator) {
100+
// Force generator to run until first yield to provoke an exception.
101+
$records->valid();
102+
}
103+
104+
return $records;
100105
},
101106
function (\Exception $exception) use ($fetchExceptionHandler) {
102107
// Throw exception if unrecoverable.

test/Integration/Porter/PorterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,23 @@ public function testCustomFetchExceptionHandler()
300300
$this->porter->import($this->specification);
301301
}
302302

303+
/**
304+
* Tests that when a generator throws a recoverable exception before the first yield, the fetch is retried.
305+
*
306+
* Note this does not support cases where exceptions may be thrown in subsequent iterations.
307+
*/
308+
public function testGeneratorException()
309+
{
310+
$this->provider->shouldReceive('fetch')->once()->andReturnUsing(function () {
311+
throw new RecoverableConnectorException;
312+
313+
yield;
314+
});
315+
316+
$this->setExpectedException(FailingTooHardException::class, '1');
317+
$this->porter->import($this->specification->setMaxFetchAttempts(1));
318+
}
319+
303320
#endregion
304321

305322
public function testFilter()

0 commit comments

Comments
 (0)