Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/catch any throwable bkp #132

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Connectors/Consumer/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Metamorphosis\Connectors\Consumer;

use Exception;
use Metamorphosis\Consumers\ConsumerInterface;
use Metamorphosis\Exceptions\ResponseTimeoutException;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\Middlewares\Handler\Dispatcher;
use Metamorphosis\Record\ConsumerRecord;
use Metamorphosis\TopicHandler\Consumer\Handler as ConsumerHandler;
use RdKafka\Message;
use Throwable;

class Manager
{
Expand Down Expand Up @@ -65,8 +65,8 @@ public function handleMessage(): void
$this->consumerHandler->warning($exception);

return;
} catch (Exception $exception) {
$this->consumerHandler->failed($exception);
} catch (Throwable $throwable) {
$this->consumerHandler->failed($throwable);

return;
}
Expand Down
4 changes: 2 additions & 2 deletions src/TopicHandler/Consumer/AbstractHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Metamorphosis\TopicHandler\Consumer;

use Exception;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\TopicHandler\ConfigOptions\Consumer as ConsumerConfigOptions;
use Throwable;

abstract class AbstractHandler implements Handler
{
Expand All @@ -29,7 +29,7 @@ public function warning(ResponseWarningException $exception): void
/**
* @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter
*/
public function failed(Exception $exception): void
public function failed(Throwable $throwable): void
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/TopicHandler/Consumer/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Metamorphosis\TopicHandler\Consumer;

use Exception;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\Record\RecordInterface;
use Throwable;

interface Handler
{
Expand All @@ -26,5 +26,5 @@ public function finished(): void;
/**
* Handle failure process.
*/
public function failed(Exception $exception): void;
public function failed(Throwable $throwable): void;
}
6 changes: 3 additions & 3 deletions tests/Integration/Dummies/MessageConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Tests\Integration\Dummies;

use Exception;
use Illuminate\Support\Facades\Log;
use Metamorphosis\Exceptions\ResponseWarningException;
use Metamorphosis\Record\RecordInterface;
use Metamorphosis\TopicHandler\Consumer\AbstractHandler;
use Throwable;

class MessageConsumer extends AbstractHandler
{
Expand All @@ -24,10 +24,10 @@ public function warning(ResponseWarningException $exception): void
]);
}

public function failed(Exception $exception): void
public function failed(Throwable $throwable): void
{
Log::error('Failed to handle kafka record for sku.', [
'exception' => $exception,
'exception' => $throwable,
]);
}
}
54 changes: 48 additions & 6 deletions tests/Unit/Connectors/Consumer/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

Check warning on line 1 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 29 and the first side effect is on line 23.

Check failure on line 1 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

An error occurred during processing; checking has been aborted. The error message was: Undefined index: scope_closer in /home/runner/work/metamorphosis/metamorphosis/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 193

Check warning on line 1 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 29 and the first side effect is on line 23.

Check failure on line 1 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

An error occurred during processing; checking has been aborted. The error message was: Undefined array key "scope_closer" in /home/runner/work/metamorphosis/metamorphosis/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 193

Check warning on line 1 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 29 and the first side effect is on line 23.

Check failure on line 1 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

An error occurred during processing; checking has been aborted. The error message was: Undefined array key "scope_closer" in /home/runner/work/metamorphosis/metamorphosis/vendor/slevomat/coding-standard/SlevomatCodingStandard/Helpers/UseStatementHelper.php on line 193

namespace Tests\Unit\Connectors\Consumer;

use Error;
use Exception;
use InvalidArgumentException;
use Metamorphosis\Connectors\Consumer\Manager;
use Metamorphosis\ConsumerConfigManager;
use Metamorphosis\Consumers\ConsumerInterface;
Expand All @@ -15,10 +17,37 @@
use RdKafka\Message as KafkaMessage;
use Tests\LaravelTestCase;
use Tests\Unit\Dummies\ConsumerHandlerDummy;
use Throwable;
use TypeError;

class ManagerTest extends LaravelTestCase
{

/**
* @dataProvider getThrowableScenarios
*/
public function testShouldHandlerAnyThrowable(Throwable $throwable): void

Check failure on line 29 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 29 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 29 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4
{
// Set
$consumer = m::mock(ConsumerInterface::class);
$consumerHandler = m::mock(ConsumerHandler::class);
$dispatcher = m::mock(Dispatcher::class);

$runner = new Manager($consumer, $consumerHandler, $dispatcher, true, false);

// Expectations
$consumer->expects()
->consume()
->andThrow($throwable);

$consumerHandler->expects()
->failed($throwable);

// Actions
$runner->handleMessage();
}

Check failure on line 48 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 48 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 48 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

public function testShouldHandleMultiplesMessages(): void

Check failure on line 50 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 50 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 50 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4
{
// Set
$consumerRecord = $this->instance(
Expand Down Expand Up @@ -50,28 +79,24 @@
$kafkaMessage3->payload = 'original message 3';
$kafkaMessage3->err = RD_KAFKA_RESP_ERR_NO_ERROR;

$messages = [$kafkaMessage1, $kafkaMessage2, $kafkaMessage3];
$count = 0;
$exception = new Exception('Exception occurs when consuming.');

// Expectations
$consumer->shouldReceive('consume')
->times(4)
->andReturnUsing(
function () use ($messages, &$count, $exception) {
$message = $messages[$count] ?? null;
if (!$message) {

Check failure on line 88 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 16 spaces, found 20

Check failure on line 88 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 16 spaces, found 20

Check failure on line 88 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 16 spaces, found 20
throw $exception;
}

Check failure on line 90 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 16 spaces, found 20

Check failure on line 90 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 16 spaces, found 20

Check failure on line 90 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 16 spaces, found 20
$count++;

return $message;
}

Check failure on line 94 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 12 spaces, found 16

Check failure on line 94 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 12 spaces, found 16

Check failure on line 94 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 12 spaces, found 16
);

$consumerHandler->expects()
->failed($exception);

$dispatcher->expects()
->handle($consumerRecord)
->times(3);
Expand All @@ -80,10 +105,9 @@
$runner->handleMessage();
$runner->handleMessage();
$runner->handleMessage();
$runner->handleMessage();
}

Check failure on line 108 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 108 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 108 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

public function testShouldCallWarningWhenErrorOccurs(): void

Check failure on line 110 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 110 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 110 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4
{
// Set
$consumer = m::mock(ConsumerInterface::class);
Expand Down Expand Up @@ -114,7 +138,7 @@

// Actions
$runner->handleMessage();
}

Check failure on line 141 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (7.4)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 141 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

Check failure on line 141 in tests/Unit/Connectors/Consumer/ManagerTest.php

View workflow job for this annotation

GitHub Actions / Testing and Code Quality for PHP (8.0)

Line indented incorrectly; expected 0 spaces, found 4

public function testShouldHandleAsyncCommit(): void
{
Expand Down Expand Up @@ -187,6 +211,23 @@
$runner->handleMessage();
}

public function getThrowableScenarios(): array
{
return [
'Exception' => [
'throwable' => new Exception(),
],
'Error' => [
'throwable' => new Error(),
],
'InvalidArgumentException' => [
'throwable' => new InvalidArgumentException(),
],
'TypeError' => [
'throwable' => new TypeError(),
],
];

protected function setUp(): void
{
parent::setUp();
Expand All @@ -203,5 +244,6 @@
'middlewares' => [],
'consumer_group' => 'consumer-id',
]);

}
}
4 changes: 2 additions & 2 deletions tests/Unit/Dummies/ConsumerHandlerDummy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace Tests\Unit\Dummies;

use Exception;
use Metamorphosis\Record\RecordInterface;
use Metamorphosis\TopicHandler\Consumer\AbstractHandler;
use Throwable;

class ConsumerHandlerDummy extends AbstractHandler
{
public function handle(RecordInterface $data): void
{
}

public function failed(Exception $exception): void
public function failed(Throwable $throwable): void
{
}
}
Loading