From 37a98a17164ecbbc1aa27c6d62807dac3f98e7ca Mon Sep 17 00:00:00 2001 From: Hector Mendoza Jacobo Date: Mon, 25 Nov 2024 21:53:30 +0000 Subject: [PATCH] Use the round method to store miliseconds on RPCLogEvent --- src/Logging/RpcLogEvent.php | 2 +- .../{LogEventTest.php => RpcLogEventTest.php} | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) rename tests/Logging/{LogEventTest.php => RpcLogEventTest.php} (68%) diff --git a/src/Logging/RpcLogEvent.php b/src/Logging/RpcLogEvent.php index 5adc691a3..684fca35c 100644 --- a/src/Logging/RpcLogEvent.php +++ b/src/Logging/RpcLogEvent.php @@ -125,7 +125,7 @@ public function __construct(null|float $startTime = null) $this->milliseconds = round(microtime(true) * 1000); if ($startTime) { - $this->latency = (int) $this->milliseconds - $startTime; + $this->latency = (int) round($this->milliseconds - $startTime); } } } diff --git a/tests/Logging/LogEventTest.php b/tests/Logging/RpcLogEventTest.php similarity index 68% rename from tests/Logging/LogEventTest.php rename to tests/Logging/RpcLogEventTest.php index d25fc04be..63a948ef4 100644 --- a/tests/Logging/LogEventTest.php +++ b/tests/Logging/RpcLogEventTest.php @@ -17,27 +17,31 @@ namespace Google\Auth\Tests\Logging; -use Google\Auth\Logging\LogEvent; +use Google\Auth\Logging\RpcLogEvent; use Google\Auth\Tests\BaseTest; -class LogEventTest extends BaseTest +class RpcLogEventTest extends BaseTest { public function testInstanceAddsTimestamp() { - $item = new LogEvent(); + $item = new RpcLogEvent(); $this->assertNotNull($item->timestamp); } public function testConstructorWithoutParameterHasNoLatency() { - $item = new LogEvent(); + $item = new RpcLogEvent(); $this->assertNull($item->latency); } public function testConstructorWithParameterHasLatencySet() { - $currentMicrotimeInMillis = microtime(true) * 1000; - $item = new LogEvent($currentMicrotimeInMillis); + // We sustract 1000 ms to simulate a microtime 1000ms in the past + $previousMicrotimeInMillis = (microtime(true) * 1000) - 1000; + $item = new RpcLogEvent($previousMicrotimeInMillis); $this->assertNotNull($item->latency); + + // Adding a delta to the test due timing on how this executes + $this->assertEqualsWithDelta(1000, $item->latency, 5); } }