You are here

class TimerTest in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Component/Utility/TimerTest.php \Drupal\Tests\Component\Utility\TimerTest

Tests the Timer system.

@group Utility

@coversDefaultClass \Drupal\Component\Utility\Timer

Hierarchy

  • class \Drupal\Tests\UnitTestCase extends \Drupal\Tests\PHPUnit_Framework_TestCase
    • class \Drupal\Tests\Component\Utility\TimerTest

Expanded class hierarchy of TimerTest

File

core/tests/Drupal/Tests/Component/Utility/TimerTest.php, line 20
Contains \Drupal\Tests\Component\Utility\TimerTest.

Namespace

Drupal\Tests\Component\Utility
View source
class TimerTest extends UnitTestCase {

  /**
   * Tests Timer::read() time accumulation accuracy across multiple restarts.
   *
   * @covers ::start
   * @covers ::stop
   * @covers ::read
   */
  public function testTimer() {
    Timer::start('test');
    usleep(5000);
    $value = Timer::read('test');
    usleep(5000);
    $value2 = Timer::read('test');
    usleep(5000);
    $value3 = Timer::read('test');
    usleep(5000);
    $value4 = Timer::read('test');

    // Although we sleep for 5 milliseconds, we should test that at least 4 ms
    // have past because usleep() is not reliable on Windows. See
    // http://php.net/manual/en/function.usleep.php for more information. The
    // purpose of the test to validate that the Timer class can measure elapsed
    // time not the granularity of usleep() on a particular OS.
    $this
      ->assertGreaterThanOrEqual(4, $value, 'Timer failed to measure at least 4 milliseconds of sleeping while running.');
    $this
      ->assertGreaterThanOrEqual($value + 4, $value2, 'Timer failed to measure at least 8 milliseconds of sleeping while running.');
    $this
      ->assertGreaterThanOrEqual($value2 + 4, $value3, 'Timer failed to measure at least 12 milliseconds of sleeping while running.');
    $this
      ->assertGreaterThanOrEqual($value3 + 4, $value4, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');

    // Stop the timer.
    $value5 = Timer::stop('test');
    $this
      ->assertGreaterThanOrEqual($value4, $value5['time'], 'Timer measured after stopping was not greater than last measurement.');

    // Read again.
    $value6 = Timer::read('test');
    $this
      ->assertEquals($value5['time'], $value6, 'Timer measured after stopping was not equal to the stopped time.');

    // Restart.
    Timer::start('test');
    usleep(5000);
    $value7 = Timer::read('test');
    $this
      ->assertGreaterThanOrEqual($value6 + 4, $value7, 'Timer failed to measure at least 16 milliseconds of sleeping while running.');

    // Stop again.
    $value8 = Timer::stop('test');
    $value9 = Timer::read('test');
    $this
      ->assertEquals($value8['time'], $value9, 'Timer measured after stopping not equal to stop time.');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TimerTest::testTimer public function Tests Timer::read() time accumulation accuracy across multiple restarts.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 259