Skip to content

[client] Lazy producer. #845

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

Merged
merged 6 commits into from
May 14, 2019
Merged
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: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ matrix:
- php: 7.2
sudo: false
env: SYMFONY_VERSION=4.0.* UNIT_TESTS=true
- php: 7.3
sudo: false
env: SYMFONY_VERSION=4.1.* UNIT_TESTS=true
- php: 7.3
sudo: false
env: SYMFONY_VERSION=4.2.* UNIT_TESTS=true
- php: 7.1
services: docker
sudo: required
Expand Down
21 changes: 10 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"ramsey/uuid": "^2|^3.5",
"psr/log": "^1",
"psr/container": "^1",
"symfony/event-dispatcher": "4.0.*",
"makasim/temp-file": "^0.2",
"google/cloud-pubsub": "^0.6.1|^1.0",
"doctrine/orm": "~2.4",
Expand All @@ -40,16 +39,16 @@
"phpunit/phpunit": "^5.5",
"phpstan/phpstan": "^0.10",
"queue-interop/queue-spec": "^0.6",
"symfony/browser-kit": "4.0.*",
"symfony/config": "4.0.*",
"symfony/process": "4.0.*",
"symfony/console": "4.0.*",
"symfony/dependency-injection": "4.0.*",
"symfony/event-dispatcher": "4.0.*",
"symfony/expression-language": "4.0.*",
"symfony/http-kernel": "4.0.*",
"symfony/filesystem": "4.0.*",
"symfony/framework-bundle": "4.0.*",
"symfony/browser-kit": "^3.4|^4",
"symfony/config": "^3.4|^4",
"symfony/process": "^3.4|^4",
"symfony/console": "^3.4|^4",
"symfony/dependency-injection": "^3.4|^4",
"symfony/event-dispatcher": "^3.4|^4",
"symfony/expression-language": "^3.4|^4",
"symfony/http-kernel": "^3.4|^4",
"symfony/filesystem": "^3.4|^4",
"symfony/framework-bundle": "^3.4|^4",
"empi89/php-amqp-stubs": "*@dev",
"doctrine/doctrine-bundle": "~1.2",
"kwn/php-rdkafka-stubs": "^1.0.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ services:
alias: 'enqueue.client.default.producer'
public: true

test_enqueue.client.default.lazy_producer:
alias: 'enqueue.client.default.lazy_producer'
public: true

test_enqueue.transport.default.context:
alias: 'enqueue.transport.default.context'
public: true
Expand Down
79 changes: 79 additions & 0 deletions pkg/enqueue-bundle/Tests/Functional/LazyProducerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Enqueue\Bundle\Tests\Functional;

use Enqueue\Bundle\Tests\Functional\App\CustomAppKernel;
use Enqueue\Symfony\Client\LazyProducer;
use Symfony\Component\Filesystem\Filesystem;

/**
* @group functional
*/
class LazyProducerTest extends WebTestCase
{
public function setUp()
{
// do not call parent::setUp.
// parent::setUp();
}

public function tearDown()
{
if (static::$kernel) {
$fs = new Filesystem();
$fs->remove(static::$kernel->getLogDir());
$fs->remove(static::$kernel->getCacheDir());
}

parent::tearDown();
}

public function testShouldAllowGetLazyProducerWithoutError()
{
$this->customSetUp([
'default' => [
'transport' => [
'dsn' => 'invalidDSN',
],
],
]);

/** @var LazyProducer $producer */
$producer = static::$container->get('test_enqueue.client.default.lazy_producer');
$this->assertInstanceOf(LazyProducer::class, $producer);

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The DSN is invalid.');
$producer->sendEvent('foo', 'foo');
}

/**
* @return string
*/
public static function getKernelClass()
{
include_once __DIR__.'/App/CustomAppKernel.php';

return CustomAppKernel::class;
}

protected function customSetUp(array $enqueueConfig)
{
static::$class = null;

static::$client = static::createClient(['enqueue_config' => $enqueueConfig]);
static::$client->getKernel()->boot();
static::$kernel = static::$client->getKernel();
static::$container = static::$kernel->getContainer();
}

protected static function createKernel(array $options = []): CustomAppKernel
{
/** @var CustomAppKernel $kernel */
$kernel = parent::createKernel($options);

$kernel->setEnqueueConfig(isset($options['enqueue_config']) ? $options['enqueue_config'] : []);

return $kernel;
}
}
6 changes: 3 additions & 3 deletions pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,9 @@ protected function customSetUp(array $enqueueConfig)
{
static::$class = null;

$this->client = static::createClient(['enqueue_config' => $enqueueConfig]);
$this->client->getKernel()->boot();
static::$kernel = $this->client->getKernel();
static::$client = static::createClient(['enqueue_config' => $enqueueConfig]);
static::$client->getKernel()->boot();
static::$kernel = static::$client->getKernel();
static::$container = static::$kernel->getContainer();

/** @var DriverInterface $driver */
Expand Down
17 changes: 10 additions & 7 deletions pkg/enqueue-bundle/Tests/Functional/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ abstract class WebTestCase extends BaseWebTestCase
/**
* @var Client
*/
protected $client;
protected static $client;

/**
* @var ContainerInterface
Expand All @@ -25,18 +25,21 @@ protected function setUp()
parent::setUp();

static::$class = null;

$this->client = static::createClient();

if (false == static::$container) {
static::$container = static::$kernel->getContainer();
}
static::$client = static::createClient();
static::$container = static::$kernel->getContainer();

/** @var TraceableProducer $producer */
$producer = static::$container->get('test_enqueue.client.default.traceable_producer');
$producer->clearTraces();
}

protected function tearDown()
{
static::$client = null;
static::$kernel = null;
static::$container = null;
}

/**
* @return string
*/
Expand Down
15 changes: 12 additions & 3 deletions pkg/enqueue/Symfony/Client/DependencyInjection/ClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
use Enqueue\Consumption\QueueConsumer;
use Enqueue\Rpc\RpcFactory;
use Enqueue\Symfony\Client\FlushSpoolProducerListener;
use Enqueue\Symfony\Client\LazyProducer;
use Enqueue\Symfony\ContainerProcessorRegistry;
use Enqueue\Symfony\DependencyInjection\TransportFactory;
use Enqueue\Symfony\DiUtils;
use Interop\Queue\Context;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -119,14 +121,21 @@ public function build(ContainerBuilder $container, array $config): void
;

$container->register($this->diUtils->format('producer'), Producer::class)
// @deprecated
->setPublic(true)
->addArgument($this->diUtils->reference('driver'))
->addArgument($this->diUtils->reference('rpc_factory'))
->addArgument($this->diUtils->reference('client_extensions'))
;

$lazyProducer = $container->register($this->diUtils->format('lazy_producer'), LazyProducer::class);
$lazyProducer->addArgument(ServiceLocatorTagPass::register($container, [
$this->diUtils->format('producer') => new Reference($this->diUtils->format('producer')),
]));
$lazyProducer->addArgument($this->diUtils->format('producer'));

$container->register($this->diUtils->format('spool_producer'), SpoolProducer::class)
->addArgument($this->diUtils->reference('producer'))
->addArgument($this->diUtils->reference('lazy_producer'))
;

$container->register($this->diUtils->format('client_extensions'), ChainExtension::class)
Expand Down Expand Up @@ -200,12 +209,12 @@ public function build(ContainerBuilder $container, array $config): void
$this->diUtils->format('queue_consumer') => $this->diUtils->reference('queue_consumer'),
$this->diUtils->format('driver') => $this->diUtils->reference('driver'),
$this->diUtils->format('delegate_processor') => $this->diUtils->reference('delegate_processor'),
$this->diUtils->format('producer') => $this->diUtils->reference('producer'),
$this->diUtils->format('producer') => $this->diUtils->reference('lazy_producer'),
]));
}

if ($this->default) {
$container->setAlias(ProducerInterface::class, $this->diUtils->format('producer'));
$container->setAlias(ProducerInterface::class, $this->diUtils->format('lazy_producer'));
$container->setAlias(SpoolProducer::class, $this->diUtils->format('spool_producer'));

if (DiUtils::DEFAULT_CONFIG !== $this->diUtils->getConfigName()) {
Expand Down
37 changes: 37 additions & 0 deletions pkg/enqueue/Symfony/Client/LazyProducer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Enqueue\Symfony\Client;

use Enqueue\Client\ProducerInterface;
use Enqueue\Rpc\Promise;
use Psr\Container\ContainerInterface;

class LazyProducer implements ProducerInterface
{
private $container;

private $producerId;

public function __construct(ContainerInterface $container, string $producerId)
{
$this->container = $container;
$this->producerId = $producerId;
}

public function sendEvent(string $topic, $message): void
{
$this->getRealProducer()->sendEvent($topic, $message);
}

public function sendCommand(string $command, $message, bool $needReply = false): ?Promise
{
return $this->getRealProducer()->sendCommand($command, $message, $needReply);
}

private function getRealProducer(): ProducerInterface
{
return $this->container->get($this->producerId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ private function assertLocatorServices(ContainerBuilder $container, $locatorId,
$locatorId = (string) $locatorId;

$this->assertTrue($container->hasDefinition($locatorId));
$this->assertRegExp('/service_locator\..*?\.enqueue\./', $locatorId);
$this->assertRegExp('/\.?service_locator\..*?\.enqueue\./', $locatorId);

$match = [];
if (false == preg_match('/(service_locator\..*?)\.enqueue\./', $locatorId, $match)) {
if (false == preg_match('/(\.?service_locator\..*?)\.enqueue\./', $locatorId, $match)) {
$this->fail('preg_match should not failed');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ private function assertLocatorServices(ContainerBuilder $container, $locatorId,
$locatorId = (string) $locatorId;

$this->assertTrue($container->hasDefinition($locatorId));
$this->assertRegExp('/service_locator\..*?\.enqueue\./', $locatorId);
$this->assertRegExp('/\.?service_locator\..*?\.enqueue\./', $locatorId);

$match = [];
if (false == preg_match('/(service_locator\..*?)\.enqueue\./', $locatorId, $match)) {
if (false == preg_match('/(\.?service_locator\..*?)\.enqueue\./', $locatorId, $match)) {
$this->fail('preg_match should not failed');
}

Expand Down
Loading