From 737469192c84ca89984c24d6eb8268d3881bc910 Mon Sep 17 00:00:00 2001 From: Clem Blanco Date: Fri, 6 Oct 2023 17:07:55 +0200 Subject: [PATCH] fix: undeleted messages (#74) --- src/Sub/Queue/Jobs/SnsEventDispatcherJob.php | 4 ++ .../Queue/Jobs/SnsEventDispatcherJobTest.php | 42 +++++++++++++++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/src/Sub/Queue/Jobs/SnsEventDispatcherJob.php b/src/Sub/Queue/Jobs/SnsEventDispatcherJob.php index 507e9c3..7e2905e 100644 --- a/src/Sub/Queue/Jobs/SnsEventDispatcherJob.php +++ b/src/Sub/Queue/Jobs/SnsEventDispatcherJob.php @@ -21,6 +21,8 @@ public function fire() 'message delivery is disabled for your SQS subscription.', $this->job); } + $this->release(); + return; } @@ -30,6 +32,8 @@ public function fire() 'subject' => $this->snsSubject(), ]); } + + $this->delete(); } /** diff --git a/tests/Sub/Queue/Jobs/SnsEventDispatcherJobTest.php b/tests/Sub/Queue/Jobs/SnsEventDispatcherJobTest.php index e82180f..fcc3b2c 100644 --- a/tests/Sub/Queue/Jobs/SnsEventDispatcherJobTest.php +++ b/tests/Sub/Queue/Jobs/SnsEventDispatcherJobTest.php @@ -113,7 +113,7 @@ public function it_will_handle_empty_messages_with_a_subject() } /** @test */ - public function it_will_not_handle_raw_notification_messages() + public function it_will_not_handle_raw_notification_messages_and_release_the_message_onto_the_queue() { Log::shouldReceive('error')->once()->with( m::pattern('/^SqsSnsQueue: Invalid SNS payload/'), @@ -122,32 +122,60 @@ public function it_will_not_handle_raw_notification_messages() $this->mockedJobData = $this->mockedRawNotificationMessage()['Messages'][0]; - $this->getJob()->fire(); + $job = $this->getJob(); + $job->shouldNotReceive('delete'); + $job->shouldReceive('release')->once(); + + $job->fire(); Event::assertNothingDispatched(); } /** @test */ - public function it_will_not_handle_messages_where_the_event_name_to_trigger_cannot_be_resolved() + public function it_will_not_handle_messages_where_the_event_name_to_trigger_cannot_be_resolved_and_delete_the_message_from_the_queue() { $this->mockedJobData = $this->mockedRichNotificationMessage([ 'TopicArn' => '', 'Subject' => '', ])['Messages'][0]; - $this->getJob()->fire(); + $job = $this->getJob(); + $job->shouldReceive('delete')->once(); + $job->shouldNotReceive('release'); + + $job->fire(); Event::assertNothingDispatched(); } + /** @test */ + public function it_will_delete_the_message_from_the_queue_when_it_managed_to_dispatch_an_event() + { + $this->mockedJobData = $this->mockedRichNotificationMessage([ + 'TopicArn' => 'TopicArn:123456', + ])['Messages'][0]; + + $job = $this->getJob(); + $job->shouldReceive('delete')->once(); + + $job->fire(); + + Event::assertDispatched('TopicArn:123456'); + } + + /** @return SnsEventDispatcherJob|\Mockery\LegacyMockInterface */ protected function getJob() { - return new SnsEventDispatcherJob( + $mock = m::mock(SnsEventDispatcherJob::class, [ $this->app, m::mock(SqsClient::class), $this->mockedJobData, 'connection-name', - 'https://sqs.someregion.amazonaws.com/1234567891011/pubsub-events' - ); + 'https://sqs.someregion.amazonaws.com/1234567891011/pubsub-events', + ])->makePartial(); + + $mock->shouldReceive('delete', 'release')->byDefault(); + + return $mock; } }