2016-12-29 21:09:49 +01:00
|
|
|
<?php
|
2016-12-17 15:16:17 +01:00
|
|
|
|
|
|
|
namespace Amp\Test;
|
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
use Amp;
|
2017-03-11 14:43:57 +01:00
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Producer;
|
|
|
|
use Amp\Pause;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2017-03-12 21:02:26 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use React\Promise\Promise as ReactPromise;
|
2016-12-17 15:16:17 +01:00
|
|
|
|
2017-03-12 21:02:26 +01:00
|
|
|
class ProducerTest extends TestCase {
|
2017-01-04 02:10:27 +01:00
|
|
|
const TIMEOUT = 100;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @expectedException \Error
|
|
|
|
* @expectedExceptionMessage The callable did not return a Generator
|
|
|
|
*/
|
|
|
|
public function testNonGeneratorCallable() {
|
2017-03-10 21:31:57 +01:00
|
|
|
new Producer(function () {});
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
public function testEmit() {
|
|
|
|
$invoked = false;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(Amp\wrap(function () use (&$invoked) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$value = 1;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($value) {
|
|
|
|
yield $emit($value);
|
|
|
|
return $value;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$invoked = false;
|
|
|
|
$callback = function ($emitted) use (&$invoked, $value) {
|
|
|
|
$invoked = true;
|
|
|
|
$this->assertSame($emitted, $value);
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer->listen($callback);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer->when(function ($exception, $result) use ($value) {
|
|
|
|
$this->assertSame($result, $value);
|
|
|
|
});
|
|
|
|
}));
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitSuccessfulPromise() {
|
|
|
|
$invoked = false;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$invoked) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred = new Deferred();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($deferred) {
|
|
|
|
return yield $emit($deferred->promise());
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$value = 1;
|
|
|
|
$invoked = false;
|
|
|
|
$callback = function ($emitted) use (&$invoked, $value) {
|
|
|
|
$invoked = true;
|
|
|
|
$this->assertSame($emitted, $value);
|
|
|
|
};
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer->listen($callback);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred->resolve($value);
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
$this->assertTrue($invoked);
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
2017-01-04 02:10:27 +01:00
|
|
|
* @depends testEmitSuccessfulPromise
|
2016-12-17 15:16:17 +01:00
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testEmitFailedPromise() {
|
|
|
|
$exception = new \Exception;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred = new Deferred();
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($deferred) {
|
|
|
|
return yield $emit($deferred->promise());
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$deferred->fail($exception);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer->when(function ($reason) use ($exception) {
|
|
|
|
$this->assertSame($reason, $exception);
|
|
|
|
});
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
2017-01-04 02:10:27 +01:00
|
|
|
public function testEmitBackPressure() {
|
|
|
|
$emits = 3;
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use (&$time, $emits) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use (&$time, $emits) {
|
|
|
|
$time = microtime(true);
|
|
|
|
for ($i = 0; $i < $emits; ++$i) {
|
|
|
|
yield $emit($i);
|
|
|
|
}
|
|
|
|
$time = microtime(true) - $time;
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer->listen(function () {
|
|
|
|
return new Pause(self::TIMEOUT);
|
|
|
|
});
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertGreaterThan(self::TIMEOUT * $emits, $time * 1000);
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-12 21:02:26 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testEmitReactBackPressure() {
|
|
|
|
$emits = 3;
|
|
|
|
Loop::run(function () use (&$time, $emits) {
|
|
|
|
$producer = new Producer(function (callable $emit) use (&$time, $emits) {
|
|
|
|
$time = microtime(true);
|
|
|
|
for ($i = 0; $i < $emits; ++$i) {
|
|
|
|
yield $emit($i);
|
|
|
|
}
|
|
|
|
$time = microtime(true) - $time;
|
|
|
|
});
|
|
|
|
|
|
|
|
$producer->listen(function () {
|
|
|
|
return new ReactPromise(function ($resolve) {
|
|
|
|
Loop::delay(self::TIMEOUT, $resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertGreaterThan(self::TIMEOUT * $emits, $time * 1000);
|
|
|
|
}
|
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testSubscriberThrows() {
|
|
|
|
$exception = new \Exception;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
try {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) {
|
|
|
|
yield $emit(1);
|
|
|
|
yield $emit(2);
|
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer->listen(function () use ($exception) {
|
2016-12-17 15:16:17 +01:00
|
|
|
throw $exception;
|
|
|
|
});
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-17 15:16:17 +01:00
|
|
|
} catch (\Exception $caught) {
|
|
|
|
$this->assertSame($exception, $caught);
|
|
|
|
}
|
|
|
|
}
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
/**
|
|
|
|
* @depends testEmit
|
|
|
|
*/
|
|
|
|
public function testProducerCoroutineThrows() {
|
2016-12-17 15:16:17 +01:00
|
|
|
$exception = new \Exception;
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2016-12-17 15:16:17 +01:00
|
|
|
try {
|
2017-03-10 21:31:57 +01:00
|
|
|
Loop::run(function () use ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$producer = new Producer(function (callable $emit) use ($exception) {
|
|
|
|
yield $emit(1);
|
|
|
|
throw $exception;
|
2016-12-17 15:16:17 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-01-04 02:10:27 +01:00
|
|
|
Amp\wait($producer);
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2016-12-17 15:16:17 +01:00
|
|
|
} catch (\Exception $caught) {
|
|
|
|
$this->assertSame($exception, $caught);
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 22:05:19 +01:00
|
|
|
|
|
|
|
public function testListenAfterResolve() {
|
|
|
|
$invoked = false;
|
|
|
|
|
|
|
|
Loop::run(function () use (&$invoked) {
|
|
|
|
$producer = new Producer(function (callable $emit) use (&$invoked) {
|
|
|
|
yield $emit(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
yield $producer;
|
|
|
|
|
|
|
|
$producer->listen(function () use (&$invoked) {
|
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
}
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|