Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve test suite to be less fragile #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/AbstractLoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ public function testRemoveInvalid()
// remove a valid stream from the event loop that was never added in the first place
$this->loop->removeReadStream($stream);
$this->loop->removeWriteStream($stream);

$this->assertTrue(true);
}

/** @test */
Expand Down
96 changes: 65 additions & 31 deletions tests/Timer/AbstractTimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,103 @@ abstract class AbstractTimerTest extends TestCase
{
abstract public function createLoop();

public function testAddTimer()
public function testAddTimerReturnsNonPeriodicTimerInstance()
{
// usleep is intentionally high
$loop = $this->createLoop();

$timer = $loop->addTimer(0.001, $this->expectCallableNever());

$this->assertInstanceOf('React\EventLoop\TimerInterface', $timer);
$this->assertFalse($timer->isPeriodic());
}

public function testAddTimerWillBeInvokedOnceAndBlocksLoopWhenRunning()
{
$loop = $this->createLoop();

$loop->addTimer(0.001, $this->expectCallableOnce());
usleep(1000);
$this->tickLoop($loop);

$start = microtime(true);
$loop->run();
$end = microtime(true);

// make no strict assumptions about actual time interval.
// must be at least 0.001s (1ms) and should not take longer than 0.1s
$this->assertGreaterThanOrEqual(0.001, $end - $start);
$this->assertLessThan(0.1, $end - $start);
}

public function testAddPeriodicTimer()
public function testAddPeriodicTimerReturnsPeriodicTimerInstance()
{
$loop = $this->createLoop();

$loop->addPeriodicTimer(0.001, $this->expectCallableExactly(3));
usleep(1000);
$this->tickLoop($loop);
usleep(1000);
$this->tickLoop($loop);
usleep(1000);
$this->tickLoop($loop);
$periodic = $loop->addPeriodicTimer(0.1, $this->expectCallableNever());

$this->assertInstanceOf('React\EventLoop\TimerInterface', $periodic);
$this->assertTrue($periodic->isPeriodic());
}

public function testAddPeriodicTimerWithCancel()
public function testAddPeriodicTimerWillBeInvokedUntilItIsCancelled()
{
$loop = $this->createLoop();

$timer = $loop->addPeriodicTimer(0.001, $this->expectCallableExactly(2));

usleep(1000);
$this->tickLoop($loop);
usleep(1000);
$this->tickLoop($loop);
$periodic = $loop->addPeriodicTimer(0.1, $this->expectCallableExactly(3));

$loop->cancelTimer($timer);
// make no strict assumptions about actual time interval.
// leave some room to ensure this ticks exactly 3 times.
$loop->addTimer(0.399, function () use ($loop, $periodic) {
$loop->cancelTimer($periodic);
});

usleep(1000);
$this->tickLoop($loop);
$loop->run();
}

public function testAddPeriodicTimerCancelsItself()
public function testAddPeriodicTimerWillBeInvokedWithMaximumAccuracyUntilItIsCancelled()
{
$loop = $this->createLoop();

$i = 0;
$periodic = $loop->addPeriodicTimer(0.001, function () use (&$i) {
++$i;
});

$loop->addTimer(0.02, function () use ($loop, $periodic) {
$loop->cancelTimer($periodic);
});

$loop->run();

// make no strict assumptions about number of invocations.
// we know it must be no more than 20 times and should at least be
// invoked twice for really slow loops
$this->assertLessThanOrEqual(20, $i);
$this->assertGreaterThan(2, $i);
}

public function testAddPeriodicTimerCancelsItself()
{
$loop = $this->createLoop();

$i = 0;
$loop->addPeriodicTimer(0.001, function ($timer) use (&$i, $loop) {
$i++;

if ($i == 2) {
if ($i === 5) {
$loop->cancelTimer($timer);
}
});

usleep(1000);
$this->tickLoop($loop);
usleep(1000);
$this->tickLoop($loop);
usleep(1000);
$this->tickLoop($loop);
$start = microtime(true);
$loop->run();
$end = microtime(true);

$this->assertEquals(5, $i);

$this->assertSame(2, $i);
// make no strict assumptions about time interval.
// 5 invocations must take at least 0.005s (5ms) and should not take
// longer than 0.1s for slower loops.
$this->assertGreaterThanOrEqual(0.005, $end - $start);
$this->assertLessThan(0.1, $end - $start);
}

public function testMinimumIntervalOneMicrosecond()
Expand Down
2 changes: 2 additions & 0 deletions tests/Timer/TimersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public function testBlockedTimer()
}));

$timers->tick();

$this->assertTrue(true);
}
}