-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #97 from leroy-merlin-br/fix/abstract-producer-config
Fix - Dynamic Config
- Loading branch information
Showing
17 changed files
with
380 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
namespace Metamorphosis\TopicHandler; | ||
|
||
class ConfigOptionsFactory | ||
{ | ||
public function makeByConfigName(string $configName, string $topicName, string $brokerName): ConfigOptions | ||
{ | ||
$topic = $this->getTopic($configName, $topicName); | ||
$broker = config($configName.'.brokers.'.$brokerName); | ||
|
||
$params = array_merge($topic, compact('broker')); | ||
|
||
return $this->makeConfigOptions($params); | ||
} | ||
|
||
public function makeByConfigNameWithSchema( | ||
string $configName, | ||
string $topicName, | ||
string $brokerName, | ||
string $schemaName | ||
): ConfigOptions { | ||
$topic = $this->getTopic($configName, $topicName); | ||
$broker = config($configName.'.brokers.'.$brokerName); | ||
$avroSchema = config($configName.'.avro_schemas.'.$schemaName); | ||
|
||
$params = array_merge($topic, compact('broker', 'avroSchema')); | ||
|
||
return $this->makeConfigOptions($params); | ||
} | ||
|
||
private function makeConfigOptions($params): ConfigOptions | ||
{ | ||
return app(ConfigOptions::class, $params); | ||
} | ||
|
||
private function getTopic(string $configName, string $topicName): array | ||
{ | ||
$topic = config($configName.'.topics.'.$topicName); | ||
$topic['topicId'] = $topic['topic_id']; | ||
|
||
return $topic; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
tests/Integration/Dummies/MessageProducerWithConfigOptions.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
namespace Tests\Integration\Dummies; | ||
|
||
use Illuminate\Support\Facades\Log; | ||
use Metamorphosis\TopicHandler\Producer\AbstractProducer; | ||
use Metamorphosis\TopicHandler\Producer\HandleableResponseInterface; | ||
use RdKafka\Message; | ||
use RuntimeException; | ||
|
||
class MessageProducerWithConfigOptions extends AbstractProducer implements HandleableResponseInterface | ||
{ | ||
public function success(Message $message): void | ||
{ | ||
Log::info('Record successfully sent to broker.', [ | ||
'topic' => $message->topic_name, | ||
'payload' => $message->payload, | ||
'key' => $message->key, | ||
'partition' => $message->partition, | ||
]); | ||
} | ||
|
||
public function failed(Message $message): void | ||
{ | ||
Log::error('Unable to delivery record to broker.', [ | ||
'topic' => $message->topic_name, | ||
'payload' => $message->payload, | ||
'key' => $message->key, | ||
'partition' => $message->partition, | ||
'error' => $message->err, | ||
]); | ||
|
||
throw new RuntimeException('error sending message!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
namespace Tests\Integration; | ||
|
||
use Illuminate\Support\Facades\Log; | ||
use Metamorphosis\Facades\Metamorphosis; | ||
use Metamorphosis\TopicHandler\ConfigOptions; | ||
use Tests\Integration\Dummies\MessageConsumer; | ||
use Tests\Integration\Dummies\MessageProducerWithConfigOptions; | ||
use Tests\LaravelTestCase; | ||
|
||
class ProducerWithConfigOptionsTest extends LaravelTestCase | ||
{ | ||
/** | ||
* @var ConfigOptions | ||
*/ | ||
private $configOptions; | ||
|
||
public function testShouldRunAProducerMessagesWithConfigOptions(): void | ||
{ | ||
// Given That I | ||
$this->haveAHandlerConfigured(); | ||
|
||
// I Expect That | ||
$this->myMessagesHaveBeenProduced(); | ||
|
||
// When I | ||
$this->haveSomeRandomMessageProduced(); | ||
|
||
// I Expect That | ||
$this->myMessagesHaveBeenLogged(); | ||
|
||
// When I | ||
$this->runTheConsumer(); | ||
} | ||
|
||
protected function runTheConsumer(): void | ||
{ | ||
$dummy = new MessageConsumer($this->configOptions); | ||
$this->instance('\App\Kafka\Consumers\ConsumerOverride', $dummy); | ||
config([ | ||
'kafka_new_config' => [ | ||
'brokers' => [ | ||
'override' => [ | ||
'connections' => 'kafka:9092', | ||
], | ||
], | ||
'topics' => [ | ||
'default' => [ | ||
'broker' => 'override', | ||
'consumer' => [ | ||
'consumer_groups' => [ | ||
'test-consumer-group' => [ | ||
'handler' => '\App\Kafka\Consumers\ConsumerOverride', | ||
'offset_reset' => 'earliest', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]); | ||
$this->artisan( | ||
'kafka:consume', | ||
[ | ||
'topic' => 'default', | ||
'consumer_group' => 'test-consumer-group', | ||
'--timeout' => 20000, | ||
'--times' => 2, | ||
'--config_name' => 'kafka_new_config', | ||
] | ||
); | ||
} | ||
|
||
protected function haveAHandlerConfigured(): void | ||
{ | ||
$this->configOptions = new ConfigOptions( | ||
'sale_order_override', | ||
['connections' => 'kafka:9092'], | ||
0, | ||
[], | ||
[], | ||
20000, | ||
false, | ||
true, | ||
600, | ||
10 | ||
); | ||
} | ||
|
||
private function haveSomeRandomMessageProduced(): void | ||
{ | ||
$saleOrderProducer = app( | ||
MessageProducerWithConfigOptions::class, | ||
[ | ||
'record' => ['saleOrderId' => 'SALE_ORDER_ID'], | ||
'configOptions' => $this->configOptions, | ||
'key' => 1, | ||
] | ||
); | ||
|
||
$saleOrderDispatcher = Metamorphosis::build($saleOrderProducer); | ||
$saleOrderDispatcher->handle($saleOrderProducer->createRecord()); | ||
} | ||
|
||
private function myMessagesHaveBeenLogged() | ||
{ | ||
Log::shouldReceive('alert') | ||
->with('{"saleOrderId":"SALE_ORDER_ID"}'); | ||
} | ||
|
||
private function myMessagesHaveBeenProduced() | ||
{ | ||
Log::shouldReceive('info') | ||
->with('Record successfully sent to broker.', [ | ||
'topic' => 'sale_order_override', | ||
'payload' => '{"saleOrderId":"SALE_ORDER_ID"}', | ||
'key' => '1', | ||
'partition' => 0, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.