Skip to content

Commit

Permalink
Make usleep longer and simplify assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
brikou authored and fabpot committed Oct 15, 2013
1 parent 879305e commit beae3b1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
33 changes: 15 additions & 18 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php
Expand Up @@ -20,6 +20,8 @@
*/
class StopwatchEventTest extends \PHPUnit_Framework_TestCase
{
const DELTA = 20;

public function testGetOrigin()
{
$event = new StopwatchEvent(12);
Expand Down Expand Up @@ -66,20 +68,18 @@ public function testDuration()
{
$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(20000);
usleep(200000);
$event->stop();
$total = $event->getDuration();
$this->assertTrue($total >= 11 && $total <= 29, $total.' should be 20 (between 11 and 29)');
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);

$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(10000);
usleep(100000);
$event->stop();
$event->start();
usleep(10000);
usleep(100000);
$event->stop();
$total = $event->getDuration();
$this->assertTrue($total >= 11 && $total <= 29, $total.' should be 20 (between 11 and 29)');
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
}

/**
Expand All @@ -96,12 +96,11 @@ public function testEnsureStopped()
// this also test overlap between two periods
$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(10000);
usleep(100000);
$event->start();
usleep(10000);
usleep(100000);
$event->ensureStopped();
$total = $event->getDuration();
$this->assertTrue($total >= 21 && $total <= 39, $total.' should be 30 (between 21 and 39)');
$this->assertEquals(300, $event->getDuration(), null, self::DELTA);
}

public function testStartTime()
Expand All @@ -116,10 +115,9 @@ public function testStartTime()

$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(10000);
usleep(100000);
$event->stop();
$start = $event->getStartTime();
$this->assertTrue($start >= 0 && $start <= 20);
$this->assertEquals(0, $event->getStartTime(), null, self::DELTA);
}

public function testEndTime()
Expand All @@ -133,13 +131,12 @@ public function testEndTime()

$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(10000);
usleep(100000);
$event->stop();
$event->start();
usleep(10000);
usleep(100000);
$event->stop();
$end = $event->getEndTime();
$this->assertTrue($end >= 11 && $end <= 29, $end.' should be 20 (between 11 and 29)');
$this->assertEquals(200, $event->getEndTime(), null, self::DELTA);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php
Expand Up @@ -20,6 +20,8 @@
*/
class StopwatchTest extends \PHPUnit_Framework_TestCase
{
const DELTA = 20;

public function testStart()
{
$stopwatch = new Stopwatch();
Expand All @@ -33,26 +35,24 @@ public function testStop()
{
$stopwatch = new Stopwatch();
$stopwatch->start('foo', 'cat');
usleep(20000);
usleep(200000);
$event = $stopwatch->stop('foo');

$this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event);
$total = $event->getDuration();
$this->assertTrue($total > 10 && $total <= 29, $total.' should be 20 (between 10 and 29)');
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
}

public function testLap()
{
$stopwatch = new Stopwatch();
$stopwatch->start('foo', 'cat');
usleep(10000);
usleep(100000);
$event = $stopwatch->lap('foo');
usleep(10000);
usleep(100000);
$stopwatch->stop('foo');

$this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event);
$total = $event->getDuration();
$this->assertTrue($total > 10 && $total <= 29, $total.' should be 20 (between 10 and 29)');
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
}

/**
Expand Down

0 comments on commit beae3b1

Please sign in to comment.