2016-12-29 14:09:49 -06:00
|
|
|
<?php
|
2016-12-15 17:28:22 -06:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
|
|
|
use Amp;
|
|
|
|
use Amp\Pause;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2016-12-15 17:28:22 -06:00
|
|
|
|
2017-03-11 14:57:03 +01:00
|
|
|
class IntervalTest extends \PHPUnit\Framework\TestCase {
|
2016-12-15 17:28:22 -06:00
|
|
|
const TIMEOUT = 10;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-15 17:28:22 -06:00
|
|
|
public function testInterval() {
|
|
|
|
$count = 3;
|
2017-01-03 19:10:27 -06:00
|
|
|
$stream = Amp\interval(self::TIMEOUT, $count);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-15 17:28:22 -06:00
|
|
|
$i = 0;
|
2017-01-03 19:10:27 -06:00
|
|
|
$stream = Amp\each($stream, function ($value) use (&$i) {
|
2016-12-15 17:28:22 -06:00
|
|
|
$this->assertSame(++$i, $value);
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-03 19:10:27 -06:00
|
|
|
Amp\wait($stream);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-15 17:28:22 -06:00
|
|
|
$this->assertSame($count, $i);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-15 17:28:22 -06:00
|
|
|
/**
|
|
|
|
* @depends testInterval
|
|
|
|
*/
|
|
|
|
public function testSlowConsumer() {
|
|
|
|
$invoked = 0;
|
|
|
|
$count = 5;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked, $count) {
|
2017-01-03 19:10:27 -06:00
|
|
|
$stream = Amp\interval(self::TIMEOUT, $count);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-03 19:10:27 -06:00
|
|
|
$stream->listen(function () use (&$invoked) {
|
2016-12-15 17:28:22 -06:00
|
|
|
++$invoked;
|
|
|
|
return new Pause(self::TIMEOUT * 2);
|
|
|
|
});
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-15 17:28:22 -06:00
|
|
|
$this->assertSame($count, $invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-15 17:28:22 -06:00
|
|
|
/**
|
|
|
|
* @expectedException \Error
|
|
|
|
* @expectedExceptionMessage The number of times to emit must be a positive value
|
|
|
|
*/
|
|
|
|
public function testInvalidCount() {
|
2017-03-10 21:31:57 +01:00
|
|
|
Amp\interval(self::TIMEOUT, -1);
|
2016-12-15 17:28:22 -06:00
|
|
|
}
|
|
|
|
}
|