|
| 1 | +<?php |
| 2 | + |
| 3 | +use Clue\Promise\Timeout; |
| 4 | +use React\Promise; |
| 5 | + |
| 6 | +class FunctionAwaitTest extends TestCase |
| 7 | +{ |
| 8 | + public function testResolvedWillResolveRightAway() |
| 9 | + { |
| 10 | + $promise = Promise\resolve(); |
| 11 | + |
| 12 | + $promise = Timeout\await($promise, 3, $this->loop); |
| 13 | + |
| 14 | + $this->expectPromiseResolved($promise); |
| 15 | + } |
| 16 | + |
| 17 | + public function testResolvedWillNotStartTimer() |
| 18 | + { |
| 19 | + $promise = Promise\resolve(); |
| 20 | + |
| 21 | + Timeout\await($promise, 3, $this->loop); |
| 22 | + |
| 23 | + $time = microtime(true); |
| 24 | + $this->loop->run(); |
| 25 | + $time = microtime(true) - $time; |
| 26 | + |
| 27 | + $this->assertLessThan(0.5, $time); |
| 28 | + } |
| 29 | + |
| 30 | + public function testRejectedWillRejectRightAway() |
| 31 | + { |
| 32 | + $promise = Promise\reject(); |
| 33 | + |
| 34 | + $promise = Timeout\await($promise, 3, $this->loop); |
| 35 | + |
| 36 | + $this->expectPromiseRejected($promise); |
| 37 | + } |
| 38 | + |
| 39 | + public function testRejectedWillNotStartTimer() |
| 40 | + { |
| 41 | + $promise = Promise\reject(); |
| 42 | + |
| 43 | + Timeout\await($promise, 3, $this->loop); |
| 44 | + |
| 45 | + $time = microtime(true); |
| 46 | + $this->loop->run(); |
| 47 | + $time = microtime(true) - $time; |
| 48 | + |
| 49 | + $this->assertLessThan(0.5, $time); |
| 50 | + } |
| 51 | + |
| 52 | + public function testPendingWillRejectOnTimeout() |
| 53 | + { |
| 54 | + $promise = $this->getMock('React\Promise\PromiseInterface'); |
| 55 | + |
| 56 | + $promise = Timeout\await($promise, 0.01, $this->loop); |
| 57 | + |
| 58 | + $this->loop->run(); |
| 59 | + |
| 60 | + $this->expectPromiseRejected($promise); |
| 61 | + } |
| 62 | + |
| 63 | + public function testPendingCancellableWillBeCancelledOnTimeout() |
| 64 | + { |
| 65 | + if (!interface_exists('React\Promise\CancellablePromiseInterface', true)) { |
| 66 | + $this->markTestSkipped('Your (outdated?) Promise API does not support cancellable promises'); |
| 67 | + } |
| 68 | + |
| 69 | + $promise = $this->getMock('React\Promise\CancellablePromiseInterface'); |
| 70 | + $promise->expects($this->once())->method('cancel'); |
| 71 | + |
| 72 | + |
| 73 | + Timeout\await($promise, 0.01, $this->loop); |
| 74 | + |
| 75 | + $this->loop->run(); |
| 76 | + } |
| 77 | +} |
0 commit comments