diff --git a/src/Bunny/Async/Client.php b/src/Bunny/Async/Client.php index af4b971..f208c2d 100644 --- a/src/Bunny/Async/Client.php +++ b/src/Bunny/Async/Client.php @@ -161,13 +161,17 @@ protected function flushWriteBuffer() if ($this->writeBuffer->isEmpty()) { $this->eventLoop->removeWriteStream($stream); $this->flushWriteBufferPromise = null; - $deferred->resolve(true); + $this->eventLoop->futureTick(function () use ($deferred) { + $deferred->resolve(true); + }); } } catch (\Exception $e) { $this->eventLoop->removeWriteStream($stream); $this->flushWriteBufferPromise = null; - $deferred->reject($e); + $this->eventLoop->futureTick(function () use ($deferred, $e) { + $deferred->reject($e); + }); } }); diff --git a/test/Bunny/AsyncClientTest.php b/test/Bunny/AsyncClientTest.php index 2b3e11e..e167dbe 100644 --- a/test/Bunny/AsyncClientTest.php +++ b/test/Bunny/AsyncClientTest.php @@ -352,4 +352,55 @@ public function testHeartBeatCallback() $this->assertEquals(2, $called); } + public function testThrowsExceptionsWithoutDoneCallback() + { + $loop = Factory::create(); + + $loop->addTimer(1, function () { + throw new TimeoutException(); + }); + + $client = $this->helper->createClient($loop); + + $client->connect()->then(function (Client $client) { + return $client->channel(); + })->then(function (Channel $channel) { + return $channel->queueDeclare('issue36', false, false)->then(function () use ($channel) { + return $channel; + }); + })->then(function (Channel $channel) { + return $channel->queueDeclare('issue36', false, true); + })->done(); + + $this->expectException(ClientException::class); + + $loop->run(); + } + + public function testDoesNotThrowExceptionsWithDoneCallback() + { + $loop = Factory::create(); + + $loop->addTimer(1, function () { + throw new TimeoutException(); + }); + + $client = $this->helper->createClient($loop); + + $client->connect()->then(function (Client $client) { + return $client->channel(); + })->then(function (Channel $channel) { + return $channel->queueDeclare('issue36', false, false)->then(function () use ($channel) { + return $channel; + }); + })->then(function (Channel $channel) { + return $channel->queueDeclare('issue36', false, true); + })->done(null, function ($reason) use ($loop) { + $this->assertInstanceOf(ClientException::class, $reason); + $this->assertStringContainsString('PRECONDITION_FAILED', $reason->getMessage()); + $loop->stop(); + }); + + $loop->run(); + } }