function it_always_wait_the_same_amount_of_time(Waiter $waiter) { $this->wait(); $waiter->wait(3)->shouldHaveBeenCalled(); $this->wait(); $waiter->wait(3)->shouldHaveBeenCalled(); }
/** * {@inheritdoc} */ public function wait($identifier = '') { if ($this->rateLimit->hasReachedLimit($identifier)) { $this->waiter->wait($this->rateLimit->getTicksBeforeUnderLimit($identifier)); } $this->rateLimit->tick($identifier); }
/** * {@inheritdoc} */ public function wait($seconds = 0) { if ($this->limit-- <= 0) { throw new CountLimitReached(); } $this->waiter->wait($seconds); }
/** * {@inheritdoc} */ public function wait($seconds = 1) { $this->timeEllapsed += $seconds; if ($this->maxExecutionTime < $this->timeEllapsed) { throw MaxExecutionTimeReached::withValue($this->maxExecutionTime); } $this->waiter->wait($seconds); }
function it_waits_an_exponential_amount_of_time_each_time_its_called(Waiter $waiter) { $waiter->wait(exp(1))->shouldBeCalled(); $this->wait(); $waiter->wait(exp(2))->shouldBeCalled(); $this->wait(); $waiter->wait(exp(3))->shouldBeCalled(); $this->wait(); }
/** * {@inheritdoc} */ public function run(Operation $operation) { $identifier = $this->identifierStrategy->getOperationIdentifier($operation); if ($this->rateLimit->hasReachedLimit($identifier)) { $this->waiter->wait($this->rateLimit->getTicksBeforeUnderLimit($identifier)); } $this->rateLimit->tick($identifier); return $this->runner->run($operation); }
/** * {@inheritdoc} */ public function run(Operation $operation) { try { return $this->runner->run($operation); } catch (\Exception $e) { try { $this->waitStrategy->wait(); } catch (WaiterException $waiterException) { throw $e; } return $this->run($operation); } }
function it_should_throw_the_original_exception_if_the_wait_fails(OperationRunner $runner, Waiter $waiter, Operation $operation) { $runner->run($operation)->will(function () use($operation) { throw new \RuntimeException('Operation failed'); }); $waiter->wait()->willThrow(new WaiterException('Retried to many times')); $this->shouldThrow(\RuntimeException::class)->duringRun($operation); }
function it_waits_if_the_limit_is_reached(RateLimit $rateLimit, Waiter $waiter) { $rateLimit->hasReachedLimit('id')->willReturn(true); $rateLimit->getTicksBeforeUnderLimit('id')->willReturn(1.234); $waiter->wait(1.234)->shouldBeCalled(); $rateLimit->tick('id')->shouldBeCalled(); $this->wait('id'); }
function it_waits_the_number_of_required_tick_if_the_rate_limit_is_reached(OperationRunner $runner, RateLimit $rateLimit, Waiter $waiter, Operation $operation) { $rateLimit->hasReachedLimit(Argument::any())->willReturn(true); $rateLimit->getTicksBeforeUnderLimit(Argument::any())->willReturn(10); $waiter->wait(10)->shouldBeCalled(); $runner->run($operation)->shouldBeCalled(); $rateLimit->tick(Argument::any())->shouldBeCalled(); $this->run($operation); }
/** * @param Operation $operation * * @return mixed */ private function runOperation(Operation $operation) { try { return $this->runner->run($operation); } catch (\Throwable $e) { // treated below } catch (\Exception $e) { // treated below } // @todo keep only inner if once ExceptionCatcher is gone if ($this->exceptionCatcherVoter instanceof ThrowableCatcherVoter) { if (!$this->exceptionCatcherVoter->shouldCatchThrowable($e)) { throw $e; } } elseif (!$this->exceptionCatcherVoter->shouldCatch($e)) { throw $e; } try { $this->waitStrategy->wait(); } catch (WaiterException $waiterException) { throw $e; } return $this->runOperation($operation); }
function it_stops_execution_if_time_out_value_is_exceeded(Waiter $waitStrategy) { $this->wait(5); $this->shouldThrow(TimedOutExceeded::class)->duringWait(5); $waitStrategy->wait(5)->shouldHaveBeenCalled(); }
/** * {@inheritdoc} */ public function wait() { $time = exp($this->exponent++); $this->waiter->wait($time); }