Skip to content

Commit

Permalink
Merge pull request #1462 from brefphp/partial-failures
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli authored Mar 15, 2023
2 parents f7aa548 + 1e4f312 commit e04eb8d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/Event/Sqs/SqsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ abstract class SqsHandler implements Handler
abstract public function handleSqs(SqsEvent $event, Context $context): void;

/** {@inheritDoc} */
public function handle($event, Context $context): array
public function handle($event, Context $context): array | null
{
// Reset the failed records to clear the internal state when using BREF_LOOP_MAX
$this->failedRecords = [];

$this->handleSqs(new SqsEvent($event), $context);

if (count($this->failedRecords) === 0) {
return [];
return null;
}

$failures = array_map(
Expand Down
15 changes: 0 additions & 15 deletions tests/Event/Sqs/SqsEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Bref\Test\Event\Sqs;

use Bref\Context\Context;
use Bref\Event\InvalidLambdaEvent;
use Bref\Event\Sqs\SqsEvent;
use Bref\Test\Fixture\SqsFakeHandler;
use PHPUnit\Framework\TestCase;

class SqsEventTest extends TestCase
Expand Down Expand Up @@ -43,17 +41,4 @@ public function test invalid event()
$this->expectExceptionMessage('This handler expected to be invoked with a SQS event. Instead, the handler was invoked with invalid event data');
new SqsEvent([]);
}

public function test partial failure()
{
$event = json_decode(file_get_contents(__DIR__ . '/handler.json'), true);
$result = (new SqsFakeHandler)->handle($event, Context::fake());

self::assertSame($result, [
'batchItemFailures' => [
['itemIdentifier' => '2'],
['itemIdentifier' => '4'],
],
]);
}
}
60 changes: 60 additions & 0 deletions tests/Event/Sqs/SqsHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types=1);

namespace Bref\Test\Event\Sqs;

use Bref\Context\Context;
use Bref\Event\Sqs\SqsEvent;
use Bref\Event\Sqs\SqsHandler;
use PHPUnit\Framework\TestCase;

class SqsHandlerTest extends TestCase
{
public function test partial failure()
{
$event = json_decode(file_get_contents(__DIR__ . '/handler.json'), true, 512, JSON_THROW_ON_ERROR);

// Fails half the messages
$handler = new class extends SqsHandler {
public function handleSqs(SqsEvent $event, Context $context): void
{
foreach ($event->getRecords() as $record) {
$body = json_decode($record->getBody(), true);

$isEven = $body['count'] % 2 === 0;
if ($isEven) {
$this->markAsFailed($record);
}
}
}
};

$result = $handler->handle($event, Context::fake());

self::assertSame($result, [
'batchItemFailures' => [
['itemIdentifier' => '2'],
['itemIdentifier' => '4'],
],
]);
}

/**
* @see https://github.com/brefphp/bref/issues/1461
*/
public function test response when no failure()
{
$event = json_decode(file_get_contents(__DIR__ . '/handler.json'), true, 512, JSON_THROW_ON_ERROR);

$handler = new class extends SqsHandler {
public function handleSqs(SqsEvent $event, Context $context): void
{
// success (does not call $this->markAsFailed())
}
};

$result = $handler->handle($event, Context::fake());

// The response should be null, not an empty array
self::assertNull($result);
}
}
23 changes: 0 additions & 23 deletions tests/Fixture/SqsFakeHandler.php

This file was deleted.

0 comments on commit e04eb8d

Please sign in to comment.