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;
|
2017-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Loop;
|
2017-04-23 14:39:19 +02:00
|
|
|
use Amp\Producer;
|
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-04-23 13:52:23 +02:00
|
|
|
Loop::run(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-03-21 18:24:06 +01:00
|
|
|
$producer->onEmit($callback);
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-21 17:23:37 +01:00
|
|
|
$producer->onResolve(function ($exception, $result) use ($value) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$this->assertSame($result, $value);
|
|
|
|
});
|
2017-04-23 13:52:23 +02: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
|
|
|
/**
|
|
|
|
* @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-03-21 18:24:06 +01:00
|
|
|
$producer->onEmit($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-03-21 17:23:37 +01:00
|
|
|
$producer->onResolve(function ($reason) use ($exception) {
|
2017-01-04 02:10:27 +01:00
|
|
|
$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-03-21 18:24:06 +01:00
|
|
|
$producer->onEmit(function () {
|
2017-04-28 14:42:02 +02:00
|
|
|
return new Delayed(self::TIMEOUT);
|
2017-01-04 02:10:27 +01:00
|
|
|
});
|
2017-03-10 21:31:57 +01:00
|
|
|
});
|
2017-01-07 13:47:45 +01:00
|
|
|
|
2017-03-14 22:18:47 +01:00
|
|
|
$this->assertGreaterThan(self::TIMEOUT * $emits - 1 /* 1ms grace period */, $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;
|
|
|
|
});
|
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$producer->onEmit(function () {
|
2017-03-12 21:02:26 +01:00
|
|
|
return new ReactPromise(function ($resolve) {
|
|
|
|
Loop::delay(self::TIMEOUT, $resolve);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-14 22:18:47 +01:00
|
|
|
$this->assertGreaterThan(self::TIMEOUT * $emits - 1 /* 1ms grace period */, $time * 1000);
|
2017-03-12 21:02:26 +01:00
|
|
|
}
|
|
|
|
|
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-03-21 18:24:06 +01:00
|
|
|
$producer->onEmit(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-03-15 17:12:49 +01:00
|
|
|
Amp\Promise\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;
|
|
|
|
|
2017-03-21 18:24:06 +01:00
|
|
|
$producer->onEmit(function () use (&$invoked) {
|
2017-03-14 22:05:19 +01:00
|
|
|
$invoked = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertFalse($invoked);
|
|
|
|
}
|
2016-12-17 15:16:17 +01:00
|
|
|
}
|