public function testUpdatesRequestForRetry()
 {
     $request = new Request('GET', 'http://www.example.com');
     $request->setState('transfer');
     $response = new Response(500);
     $handle = $this->getMockBuilder('Guzzle\\Http\\Curl\\CurlHandle')->disableOriginalConstructor()->getMock();
     $e = new CurlException();
     $e->setCurlHandle($handle);
     $plugin = new BackoffPlugin(new ConstantBackoffStrategy(10));
     $plugin->addSubscriber($this);
     $event = new Event(array('request' => $request, 'response' => $response, 'exception' => $e));
     $plugin->onRequestSent($event);
     $this->assertEquals(array('request' => $request, 'response' => $response, 'handle' => $handle, 'retries' => 1, 'delay' => 10), $this->readAttribute($this->retried, 'context'));
     $plugin->onRequestSent($event);
     $this->assertEquals(array('request' => $request, 'response' => $response, 'handle' => $handle, 'retries' => 2, 'delay' => 10), $this->readAttribute($this->retried, 'context'));
 }
Example #2
0
 /**
  * Add backoff logging to the backoff plugin if needed
  *
  * @param BackoffPlugin $plugin Backoff plugin
  * @param Collection    $config Configuration settings
  *
  * @throws InvalidArgumentException
  */
 protected function addBackoffLogger(BackoffPlugin $plugin, Collection $config)
 {
     // The log option can be set to `debug` or an instance of a LogAdapterInterface
     if ($logger = $config->get(Options::BACKOFF_LOGGER)) {
         $format = $config->get(Options::BACKOFF_LOGGER_TEMPLATE);
         if ($logger === 'debug') {
             $logger = new ClosureLogAdapter(function ($message) {
                 trigger_error($message . "\n");
             });
         } elseif (!$logger instanceof LogAdapterInterface) {
             throw new InvalidArgumentException(Options::BACKOFF_LOGGER . ' must be set to `debug` or an instance of ' . 'Guzzle\\Common\\Log\\LogAdapterInterface');
         }
         // Create the plugin responsible for logging exponential backoff retries
         $logPlugin = new BackoffLogger($logger);
         // You can specify a custom format or use the default
         if ($format) {
             $logPlugin->setTemplate($format);
         }
         $plugin->addSubscriber($logPlugin);
     }
 }
Example #3
0
 /**
  * Add the exponential backoff logger to the backoff plugin
  *
  * @param BackoffPlugin $plugin Plugin to attach a logger to
  * @param mixed         $logger Logger to use with the plugin
  * @param string        $format Logger format option
  *
  * @throws InvalidArgumentException if the logger is not valid
  */
 private function addLogger(BackoffPlugin $plugin, $logger, $format = null)
 {
     if ($logger === 'debug') {
         $logger = new ClosureLogAdapter(function ($message) {
             trigger_error($message . "\n");
         });
     } elseif (!$logger instanceof LogAdapterInterface) {
         throw new InvalidArgumentException(Options::BACKOFF_LOGGER . ' must be set to `debug` or an instance of ' . 'Guzzle\\Common\\Log\\LogAdapterInterface');
     }
     // Create the plugin responsible for logging exponential backoff retries
     $logPlugin = new BackoffLogger($logger);
     // You can specify a custom format or use the default
     if ($format) {
         $logPlugin->setTemplate($format);
     }
     $plugin->addSubscriber($logPlugin);
 }