Ejemplo n.º 1
0
 function it_always_wait_the_same_amount_of_time(Waiter $waiter)
 {
     $this->wait();
     $waiter->wait(3)->shouldHaveBeenCalled();
     $this->wait();
     $waiter->wait(3)->shouldHaveBeenCalled();
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function wait($identifier = '')
 {
     if ($this->rateLimit->hasReachedLimit($identifier)) {
         $this->waiter->wait($this->rateLimit->getTicksBeforeUnderLimit($identifier));
     }
     $this->rateLimit->tick($identifier);
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function wait($seconds = 0)
 {
     if ($this->limit-- <= 0) {
         throw new CountLimitReached();
     }
     $this->waiter->wait($seconds);
 }
Ejemplo n.º 4
0
 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);
 }
Ejemplo n.º 5
0
 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');
 }
 /**
  * {@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_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);
 }
Ejemplo n.º 8
0
 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();
 }
Ejemplo n.º 9
0
 /**
  * {@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);
 }
Ejemplo n.º 10
0
 /**
  * {@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);
     }
 }
Ejemplo n.º 11
0
 /**
  * @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);
 }
Ejemplo n.º 12
0
 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();
 }
Ejemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function wait()
 {
     $time = exp($this->exponent++);
     $this->waiter->wait($time);
 }