|
| 1 | +<?php |
| 2 | + |
| 3 | +use React\Promise\Deferred; |
| 4 | + |
| 5 | +class FunctionAwaitAnyTest extends TestCase |
| 6 | +{ |
| 7 | + /** |
| 8 | + * @expectedException UnderflowException |
| 9 | + */ |
| 10 | + public function testAwaitAnyEmpty() |
| 11 | + { |
| 12 | + $this->block->awaitAny(array()); |
| 13 | + } |
| 14 | + |
| 15 | + public function testAwaitAnyFirstResolved() |
| 16 | + { |
| 17 | + $all = array( |
| 18 | + $this->createPromiseRejected(1), |
| 19 | + $this->createPromiseResolved(2, 0.01), |
| 20 | + $this->createPromiseResolved(3, 0.02) |
| 21 | + ); |
| 22 | + |
| 23 | + $this->assertEquals(2, $this->block->awaitAny($all)); |
| 24 | + } |
| 25 | + |
| 26 | + public function testAwaitAnyFirstResolvedConcurrently() |
| 27 | + { |
| 28 | + $d1 = new Deferred(); |
| 29 | + $d2 = new Deferred(); |
| 30 | + $d3 = new Deferred(); |
| 31 | + |
| 32 | + $this->loop->addTimer(0.01, function() use ($d1, $d2, $d3) { |
| 33 | + $d1->reject(1); |
| 34 | + $d2->resolve(2); |
| 35 | + $d3->resolve(3); |
| 36 | + }); |
| 37 | + |
| 38 | + $all = array( |
| 39 | + $d1->promise(), |
| 40 | + $d2->promise(), |
| 41 | + $d3->promise() |
| 42 | + ); |
| 43 | + |
| 44 | + $this->assertEquals(2, $this->block->awaitAny($all)); |
| 45 | + } |
| 46 | + |
| 47 | + public function testAwaitAnyAllRejected() |
| 48 | + { |
| 49 | + $all = array( |
| 50 | + $this->createPromiseRejected(1), |
| 51 | + $this->createPromiseRejected(2) |
| 52 | + ); |
| 53 | + |
| 54 | + $this->setExpectedException('UnderflowException'); |
| 55 | + $this->block->awaitAny($all); |
| 56 | + } |
| 57 | + |
| 58 | + public function testAwaitAnyInterrupted() |
| 59 | + { |
| 60 | + $promise = $this->createPromiseResolved(2, 0.02); |
| 61 | + $this->createTimerInterrupt(0.01); |
| 62 | + |
| 63 | + $this->assertEquals(2, $this->block->awaitAny(array($promise))); |
| 64 | + } |
| 65 | +} |
0 commit comments