Skip to content

Commit

Permalink
Merge pull request #39 from mente/feature-sandbox
Browse files Browse the repository at this point in the history
sandbox mode to allow working without actual rabbitmq instance
  • Loading branch information
Oliboy50 authored Jan 10, 2017
2 parents 4d39d26 + 8998120 commit 6ebec35
Show file tree
Hide file tree
Showing 13 changed files with 632 additions and 6 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ When you want to consume a message out of a queue :
$msg = $this->get('m6_web_amqp.consumer.myconsumer')->getMessage();
```

The AmQPBundle does not provide a daemon mode to run AMQP consumers and will not. You can do it with the [M6Web/DaemonBundle](https://github.com/M6Web/DaemonBundle).
The AmqpBundle does not provide a daemon mode to run AMQP consumers and will not. You can do it with the [M6Web/DaemonBundle](https://github.com/M6Web/DaemonBundle).

## Installation ##

Expand Down Expand Up @@ -67,12 +67,14 @@ By default, the sf2 event dispatcher will throw an event on each command (the ev
```yaml
m6_web_amqp:
event_dispatcher: false
```
```
Here a configuration example:
Here a configuration example:
```yaml
m6_web_amqp:
sandbox:
enabled: false #optional - default false
connections:
default:
host: 'localhost' # optional - default 'localhost'
Expand Down Expand Up @@ -149,7 +151,7 @@ If you need to add option default publish attributes for each message, publish_a
publish_attributes: { 'content_type' : 'application/json', 'delivery_mode': 'persistent', 'priority': 8, 'expiration': '3200'}
```
If you don't want to use the configuration to define the __routing key__ (for instance, if it should be computed for each message),
If you don't want to use the configuration to define the __routing key__ (for instance, if it should be computed for each message),
you can define it during the call to `publishMessage()` :
```php
Expand All @@ -170,8 +172,8 @@ $msg = $this->get('m6_web_amqp.consumer.myconsumer')->getMessage();
The consumer does not wait for a message : getMessage will return null immediately if no message is available or return a AMQPEnvelope object if a message can be consumed.
The "flags" argument of getMessage accepts AMQP_AUTOACK (auto acknowledge by default) or AMQP_NOPARAM (manual acknowledge).
To manually acknowledge a message, use the consumer's ackMessage/nackMessage methods with a delivery_tag argument's value from the AMQPEnvelope object.
If you choose to not acknowledge the message, the second parameter of nackMessage accepts AMQP_REQUEUE to requeue the message or AMQP_NOPARAM to forget it.
To manually acknowledge a message, use the consumer's ackMessage/nackMessage methods with a delivery_tag argument's value from the AMQPEnvelope object.
If you choose to not acknowledge the message, the second parameter of nackMessage accepts AMQP_REQUEUE to requeue the message or AMQP_NOPARAM to forget it.
Be careful with qos parameters, you should know that it can hurt your performances. Please [read this](http://www.rabbitmq.com/blog/2012/05/11/some-queuing-theory-throughput-latency-and-bandwidth/).
Also be aware that currently there is no `global` parameter available within PHP `amqp` extension.
Expand Down Expand Up @@ -214,3 +216,15 @@ rabbitmq:
- "15672:15672"
- "5672:5672"
```
# Testing
If you use this library in your application tests you will need rabbitmq instance running. If you don't want to test rabbitmq producers and consumers you can enable sandbox mode:
```yaml
m6_web_amqp:
sandbox:
enabled: true
```
In this mode there will be no connection established to rabbitmq server. Producers silently accept message, consumers silently assume there are no messages available.
9 changes: 9 additions & 0 deletions src/AmqpBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function getConfigTreeBuilder()
->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
->booleanNode('event_dispatcher')->defaultTrue()->end()
->booleanNode('prototype')->defaultFalse()->end()
->arrayNode('sandbox')
->addDefaultsIfNotSet()
->children()
->booleanNode('enabled')
->defaultFalse()
->info('Sandbox mode do not establish connections but mocks them via in-memory queue. Useful for tests')
->end()
->end()
->end()
->end();

$this->addConnections($rootNode);
Expand Down
4 changes: 4 additions & 0 deletions src/AmqpBundle/DependencyInjection/M6WebAmqpExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('data_collector.yml');
}

if ($config['sandbox']['enabled']) {
$loader->load('sandbox_services.yml');
}

$this->loadConnections($container, $config);
$this->loadProducers($container, $config);
$this->loadConsumers($container, $config);
Expand Down
7 changes: 7 additions & 0 deletions src/AmqpBundle/Resources/config/sandbox_services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
m6_web_amqp.exchange.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullExchange'
m6_web_amqp.queue.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullQueue'
m6_web_amqp.connection.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullConnection'
m6_web_amqp.channel.class: 'M6Web\Bundle\AmqpBundle\Sandbox\NullChannel'
m6_web_amqp.envelope.class: '\M6Web\Bundle\AmqpBundle\Sandbox\NullEnvelope'

16 changes: 16 additions & 0 deletions src/AmqpBundle/Sandbox/NullChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace M6Web\Bundle\AmqpBundle\Sandbox;

use AMQPConnection;

/**
* Channel which does not do anything with connection
*/
class NullChannel extends \AMQPChannel
{
public function __construct(AMQPConnection $amqp_connection)
{
//noop
}
}
57 changes: 57 additions & 0 deletions src/AmqpBundle/Sandbox/NullConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace M6Web\Bundle\AmqpBundle\Sandbox;

/**
* Connection that does not connect to anything
*/
class NullConnection extends \AMQPConnection
{
/**
* {@inheritdoc}
*/
public function connect()
{
return true;
}

/**
* {@inheritdoc}
*/
public function pconnect()
{
return true;
}

/**
* {@inheritdoc}
*/
public function pdisconnect()
{
return true;
}

/**
* {@inheritdoc}
*/
public function disconnect()
{
return true;
}

/**
* {@inheritdoc}
*/
public function reconnect()
{
return true;
}

/**
* {@inheritdoc}
*/
public function preconnect()
{
return true;
}
}
Loading

0 comments on commit 6ebec35

Please sign in to comment.