Skip to content

Commit

Permalink
bug #34179 [Stopwatch] Fixed a bug in StopwatchEvent::getStartTime (T…
Browse files Browse the repository at this point in the history
…imoBakx)

This PR was merged into the 3.4 branch.

Discussion
----------

[Stopwatch] Fixed a bug in StopwatchEvent::getStartTime

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #34088
| License       | MIT
| Doc PR        | N/A

When using a `StopwatchEvent` with an `$origin` that's smaller than the first start time, calling `getStartTime()` before ending the event will give `0` instead of the correct number.

The proposed fix in #34088 fixes this.

Commits
-------

b2b7eab [Stopwatch] Fixed a bug in stopwatch event getStartTime
  • Loading branch information
fabpot committed Nov 3, 2019
2 parents a1b5079 + b2b7eab commit 767b265
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Symfony/Component/Stopwatch/StopwatchEvent.php
Expand Up @@ -154,7 +154,15 @@ public function getPeriods()
*/
public function getStartTime()
{
return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0;
if (isset($this->periods[0])) {
return $this->periods[0]->getStartTime();
}

if ($this->started) {
return $this->started[0];
}

return 0;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php
Expand Up @@ -152,6 +152,27 @@ public function testStartTime()
$this->assertEqualsWithDelta(0, $event->getStartTime(), self::DELTA);
}

public function testStartTimeWhenStartedLater()
{
$event = new StopwatchEvent(microtime(true) * 1000);
usleep(100000);
$this->assertLessThanOrEqual(0.5, $event->getStartTime());

$event = new StopwatchEvent(microtime(true) * 1000);
usleep(100000);
$event->start();
$event->stop();
$this->assertLessThanOrEqual(101, $event->getStartTime());

$event = new StopwatchEvent(microtime(true) * 1000);
usleep(100000);
$event->start();
usleep(100000);
$this->assertEqualsWithDelta(100, $event->getStartTime(), self::DELTA);
$event->stop();
$this->assertEqualsWithDelta(100, $event->getStartTime(), self::DELTA);
}

public function testInvalidOriginThrowsAnException()
{
$this->expectException('InvalidArgumentException');
Expand Down

0 comments on commit 767b265

Please sign in to comment.