From 7d05926f9ce6ec3291ff6f0769adedb4f7a3b822 Mon Sep 17 00:00:00 2001 From: Christian Hammerl Date: Mon, 1 Mar 2021 22:08:20 +0100 Subject: [PATCH 1/2] Fix swallowing exceptions --- src/Bunny/Async/Client.php | 8 ++++-- test/Bunny/AsyncClientTest.php | 47 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) 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..ac24290 100644 --- a/test/Bunny/AsyncClientTest.php +++ b/test/Bunny/AsyncClientTest.php @@ -352,4 +352,51 @@ 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 (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 (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(); + } } From 2cd452baf58d1e76ca5c90293922b4a88ff0611d Mon Sep 17 00:00:00 2001 From: Christian Hammerl Date: Mon, 1 Mar 2021 23:47:02 +0100 Subject: [PATCH 2/2] Fix passing channel to next callback --- test/Bunny/AsyncClientTest.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/Bunny/AsyncClientTest.php b/test/Bunny/AsyncClientTest.php index ac24290..e167dbe 100644 --- a/test/Bunny/AsyncClientTest.php +++ b/test/Bunny/AsyncClientTest.php @@ -365,7 +365,9 @@ public function testThrowsExceptionsWithoutDoneCallback() $client->connect()->then(function (Client $client) { return $client->channel(); })->then(function (Channel $channel) { - return $channel->queueDeclare('issue36', false, false); + return $channel->queueDeclare('issue36', false, false)->then(function () use ($channel) { + return $channel; + }); })->then(function (Channel $channel) { return $channel->queueDeclare('issue36', false, true); })->done(); @@ -388,7 +390,9 @@ public function testDoesNotThrowExceptionsWithDoneCallback() $client->connect()->then(function (Client $client) { return $client->channel(); })->then(function (Channel $channel) { - return $channel->queueDeclare('issue36', false, false); + 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) {