Debian Codegraph @

tests/phpunit/unit/includes/libs/telemetry/ClockTest.php

raw symbols SCIP
1 <?php
2 namespace Wikimedia\Tests\Telemetry;
3
4 use MediaWikiUnitTestCase;
5 use Wikimedia\Telemetry\Clock;
6
7 /**
8 * @covers \Wikimedia\Telemetry\Clock
9 */
10 class ClockTest extends MediaWikiUnitTestCase {
11 private Clock $clock;
12
13 protected function setUp(): void {
14 parent::setUp();
15
16 $this->clock = new Clock();
17 }
18
19 public function testShouldReturnCurrentTime(): void {
20 $this->assertClockReturnsCurrentTime();
21 }
22
23 public function testShouldAllowMockingTimestamp(): void {
24 Clock::setMockTime( 2_000 );
25
26 $this->assertSame( 2_000, $this->clock->getCurrentNanoTime() );
27
28 Clock::setMockTime( null );
29
30 $this->assertClockReturnsCurrentTime();
31 }
32
33 /**
34 * Utility function to assert that the Clock being tested returns the current time, with some leeway.
35 * @return void
36 */
37 private function assertClockReturnsCurrentTime() {
38 $this->markTestSkipped( 'Flaky (T379562) - to be replaced by ConvertibleTimestamp::microtime()' );
39
40 $referenceTime = (int)( 1e9 * microtime( true ) ) - hrtime( true );
41 $currentTime = $referenceTime + hrtime( true );
42
43 usleep( 1 );
44
45 $now = $this->clock->getCurrentNanoTime();
46
47 usleep( 1 );
48
49 $afterTime = $referenceTime + hrtime( true );
50
51 $this->assertGreaterThan(
52 $currentTime,
53 $now,
54 'Too large time difference'
55 );
56 $this->assertLessThanOrEqual(
57 $afterTime,
58 $now,
59 'Too large time difference'
60 );
61 }
62 }