1
0
mirror of https://github.com/danog/amp.git synced 2025-01-22 21:31:18 +01:00
amp/test/IntervalTest.php
Aaron Piotrowski 5651240615 Update to promise spec v0.3
Dropped strict-types due to spec requiring weak types in callbacks.
2016-12-29 16:29:27 -06:00

52 lines
1.2 KiB
PHP

<?php
namespace Amp\Test;
use Amp;
use Amp\Pause;
use Interop\Async\Loop;
class IntervalTest extends \PHPUnit_Framework_TestCase {
const TIMEOUT = 10;
public function testInterval() {
$count = 3;
$observable = Amp\interval(self::TIMEOUT, $count);
$i = 0;
$observable = Amp\each($observable, function ($value) use (&$i) {
$this->assertSame(++$i, $value);
});
Amp\wait($observable);
$this->assertSame($count, $i);
}
/**
* @depends testInterval
*/
public function testSlowConsumer() {
$invoked = 0;
$count = 5;
Loop::execute(function () use (&$invoked, $count) {
$observable = Amp\interval(self::TIMEOUT, $count);
$observable->subscribe(function () use (&$invoked) {
++$invoked;
return new Pause(self::TIMEOUT * 2);
});
});
$this->assertSame($count, $invoked);
}
/**
* @expectedException \Error
* @expectedExceptionMessage The number of times to emit must be a positive value
*/
public function testInvalidCount() {
$observable = Amp\interval(self::TIMEOUT, -1);
}
}