Skip to content

Commit 5119101

Browse files
committed
Merge pull request #52 from jeskew/feature/sync-queue
Feature/sync queue
2 parents 27bbd52 + 05dc5fd commit 5119101

6 files changed

Lines changed: 236 additions & 2 deletions

File tree

docs/configuration.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ Configure the Bundle
22
====================
33

44
The bundle allows you to specify different Message Queue providers - however,
5-
Amazon AWS and IronMQ are the only ones currently supported.
5+
Amazon AWS and IronMQ are the only ones currently supported. Blocking, synchronous queues
6+
are also supported through the ``sync`` driver to aid development and debugging.
67

78
We are actively looking to add more and would be more than happy to accept contributions.
89

@@ -22,6 +23,7 @@ For specific instructions on how to configure each provider, please view their d
2223

2324
aws-provider
2425
iron-mq-provider
26+
sync-provider
2527

2628
Caching
2729
-------
@@ -113,7 +115,7 @@ A working configuration would look like the following
113115
project_id: YOUR_IRONMQ_PROJECT_ID_HERE
114116
queues:
115117
my_queue_key:
116-
provider: ironmq #or aws
118+
provider: ironmq #or aws or sync
117119
options:
118120
queue_name: my_actual_queue_name
119121
push_notifications: true

docs/sync-provider.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Sync Provider
2+
-------------
3+
4+
The sync provider immediately dispatches and resolves queued events. It is not intended
5+
for production use but instead to support local development, debugging and testing
6+
of queue-based code paths.
7+
8+
Configuration
9+
^^^^^^^^^^^^^
10+
11+
To use the sync queue, set the ``provider`` of a given queue to ``sync``. No further
12+
configuration is necessary.
13+
14+
.. code-block:: yaml
15+
16+
#app/config_dev.yml
17+
18+
uecode_qpush:
19+
queues:
20+
my_queue_name:
21+
provider: sync

src/DependencyInjection/UecodeQPushExtension.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public function load(array $configs, ContainerBuilder $container)
7575
$container
7676
);
7777
break;
78+
case 'sync':
79+
$class = $container->getParameter('uecode_qpush.provider.sync');
80+
$client = $this->createSyncClient();
81+
break;
7882
}
7983

8084
$definition = new Definition(
@@ -176,6 +180,11 @@ private function createIronMQClient($config, ContainerBuilder $container)
176180
return new Reference('uecode_qpush.provider.ironmq');
177181
}
178182

183+
private function createSyncClient()
184+
{
185+
return new Reference('event_dispatcher');
186+
}
187+
179188
/**
180189
* Returns the Extension Alias
181190
*

src/Provider/SyncProvider.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2014 Underground Elephant
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
* @package qpush-bundle
19+
* @copyright Underground Elephant 2014
20+
* @license Apache License, Version 2.0
21+
*/
22+
23+
namespace Uecode\Bundle\QPushBundle\Provider;
24+
25+
26+
use Doctrine\Common\Cache\Cache;
27+
use Symfony\Bridge\Monolog\Logger;
28+
use Uecode\Bundle\QPushBundle\Event\Events;
29+
use Uecode\Bundle\QPushBundle\Event\MessageEvent;
30+
use Uecode\Bundle\QPushBundle\Message\Message;
31+
32+
class SyncProvider extends AbstractProvider
33+
{
34+
/**
35+
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
36+
*/
37+
protected $dispatcher;
38+
39+
public function __construct(
40+
$name,
41+
array $options,
42+
$client,
43+
Cache $cache,
44+
Logger $logger
45+
) {
46+
$this->name = $name;
47+
$this->options = $options;
48+
$this->dispatcher = $client;
49+
$this->cache = $cache;
50+
$this->logger = $logger;
51+
}
52+
53+
public function getProvider()
54+
{
55+
return 'Sync';
56+
}
57+
58+
public function publish(array $message, array $options = [])
59+
{
60+
$message = new Message(time(), $message, []);
61+
62+
$this->dispatcher->dispatch(
63+
Events::Message($this->name),
64+
new MessageEvent($this->name, $message)
65+
);
66+
67+
$context = ['MessageId' => $message->getId()];
68+
$this->log(200, 'Message received and dispatched on Sync Queue', $context);
69+
}
70+
71+
public function create() {}
72+
73+
public function destroy() {}
74+
75+
public function delete($id) {}
76+
77+
public function receive(array $options = []) {}
78+
}

src/Resources/config/parameters.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ parameters:
33
uecode_qpush.registry.class: Uecode\Bundle\QPushBundle\Provider\ProviderRegistry
44
uecode_qpush.provider.aws: Uecode\Bundle\QPushBundle\Provider\AwsProvider
55
uecode_qpush.provider.ironmq: Uecode\Bundle\QPushBundle\Provider\IronMqProvider
6+
uecode_qpush.provider.sync: Uecode\Bundle\QPushBundle\Provider\SyncProvider
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Uecode\Bundle\QPushBundle\Tests\Provider;
4+
5+
6+
use Uecode\Bundle\QPushBundle\Event\Events;
7+
use Uecode\Bundle\QPushBundle\Provider\SyncProvider;
8+
9+
class SyncProviderTest extends \PHPUnit_Framework_TestCase
10+
{
11+
/**
12+
* @var \Uecode\Bundle\QPushBundle\Provider\SyncProvider
13+
*/
14+
protected $provider;
15+
16+
/**
17+
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
18+
*/
19+
protected $dispatcher;
20+
21+
/**
22+
* @var \Symfony\Bridge\Monolog\Logger
23+
*/
24+
protected $logger;
25+
26+
public function setUp()
27+
{
28+
$this->dispatcher = $this->getMock(
29+
'Symfony\Component\EventDispatcher\EventDispatcherInterface'
30+
);
31+
32+
$this->provider = $this->getSyncProvider();
33+
}
34+
35+
public function testGetProvider()
36+
{
37+
$provider = $this->provider->getProvider();
38+
39+
$this->assertEquals('Sync', $provider);
40+
}
41+
42+
public function testPublish()
43+
{
44+
$this->dispatcher
45+
->expects($this->once())
46+
->method('dispatch')
47+
->with(
48+
Events::Message($this->provider->getName()),
49+
new \PHPUnit_Framework_Constraint_IsInstanceOf('Uecode\Bundle\QPushBundle\Event\MessageEvent')
50+
);
51+
52+
$this->provider->publish(['foo' => 'bar']);
53+
}
54+
55+
public function testCreate()
56+
{
57+
$this->setNoOpExpectation();
58+
59+
$this->provider->create();
60+
}
61+
62+
public function testDestroy()
63+
{
64+
$this->setNoOpExpectation();
65+
66+
$this->provider->destroy();
67+
}
68+
69+
public function testDelete()
70+
{
71+
$this->setNoOpExpectation();
72+
73+
$this->provider->delete('foo');
74+
}
75+
76+
public function testReceive()
77+
{
78+
$this->setNoOpExpectation();
79+
80+
$this->provider->receive();
81+
}
82+
83+
84+
protected function getSyncProvider()
85+
{
86+
$options = [
87+
'logging_enabled' => false,
88+
'push_notifications' => true,
89+
'notification_retries' => 3,
90+
'message_delay' => 0,
91+
'message_timeout' => 30,
92+
'message_expiration' => 604800,
93+
'messages_to_receive' => 1,
94+
'receive_wait_time' => 3,
95+
'subscribers' => [
96+
[ 'protocol' => 'http', 'endpoint' => 'http://fake.com' ]
97+
]
98+
];
99+
100+
$cache = $this->getMock(
101+
'Doctrine\Common\Cache\PhpFileCache',
102+
[],
103+
['/tmp', 'qpush.aws.test.php']
104+
);
105+
106+
$this->logger = $this->getMock(
107+
'Symfony\Bridge\Monolog\Logger', [], ['qpush.test']
108+
);
109+
110+
return new SyncProvider('test', $options, $this->dispatcher, $cache, $this->logger);
111+
}
112+
113+
protected function setNoOpExpectation()
114+
{
115+
$this->dispatcher
116+
->expects($this->never())
117+
->method(new \PHPUnit_Framework_Constraint_IsAnything());
118+
119+
$this->logger
120+
->expects($this->never())
121+
->method(new \PHPUnit_Framework_Constraint_IsAnything());
122+
}
123+
}

0 commit comments

Comments
 (0)