getCurrentMicroTime example

// Work through stack of queues, re-adding to the stack when a delay is     // necessary.     while ($item = array_shift($queues)) {
      [
        'queue' => $queue,
        'worker' => $worker,
        'process_from' => $process_from,
      ] = $item;

      // Each queue will be processed immediately when it is reached for the       // first time, as zero > currentTime will never be true.       if ($process_from > $this->time->getCurrentMicroTime()) {
        $this->usleep(round($process_from - $this->time->getCurrentMicroTime(), 3) * 1000000);
      }

      try {
        $this->processQueue($queue$worker);
      }
      catch (SuspendQueueException $e) {
        // Return to this queue after processing other queues if the delay is         // within the threshold.         if ($e->isDelayable() && ($e->getDelay() < $max_wait)) {
          $item['process_from'] = $this->time->getCurrentMicroTime() + $e->getDelay();
          
$expected = 12345678;
    $this->assertEquals($expected$this->time->getCurrentTime());
  }

  /** * Tests the getCurrentMicroTime method. * * @covers ::getCurrentMicroTime */
  public function testGetCurrentMicroTime() {
    $expected = 1234567.89;
    $this->assertEquals($expected$this->time->getCurrentMicroTime());
  }

}

namespace Drupal\Component\Datetime;

/** * Shadow time() system call. * * @returns int */

  protected function setUp(): void {
    // Setup logger before register() is called.     $this->logger = $this->createMock(LoggerInterface::class);
    parent::setUp();

    $this->connection = Database::getConnection();
    $this->cron = \Drupal::service('cron');

    $time = $this->prophesize('Drupal\Component\Datetime\TimeInterface');
    $time->getCurrentTime()->willReturn($this->currentTime);
    $time->getCurrentMicroTime()->willReturn(100.0);
    $time->getRequestTime()->willReturn($this->currentTime);
    \Drupal::getContainer()->set('datetime.time', $time->reveal());
    $this->assertEquals($this->currentTime, \Drupal::time()->getCurrentTime());
    $this->assertEquals($this->currentTime, \Drupal::time()->getRequestTime());

    $realQueueFactory = $this->container->get('queue');
    $queue_factory = $this->prophesize(get_class($realQueueFactory));
    $database = new DatabaseQueue('cron_queue_test_database_delay_exception', $this->connection);
    $memory = new Memory('cron_queue_test_memory_delay_exception');
    $queue_factory->get('cron_queue_test_database_delay_exception', Argument::cetera())->willReturn($database);
    $queue_factory->get('cron_queue_test_memory_delay_exception', Argument::cetera())->willReturn($memory);
    
/** * {@inheritdoc} */
  public function getRequestMicroTime() {
    $request = $this->requestStack->getCurrentRequest();
    if ($request) {
      return $request->server->get('REQUEST_TIME_FLOAT');
    }
    // If this is called prior to the request being pushed to the stack fallback     // to built-in globals (if available) or the system time.     return $_SERVER['REQUEST_TIME_FLOAT'] ?? $this->getCurrentMicroTime();
  }

  /** * {@inheritdoc} */
  public function getCurrentTime() {
    return time();
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.