Skip to content

Commit 4d28cf9

Browse files
committed
[bundle] Async events. Add func tests for async listener
1 parent 69e9a35 commit 4d28cf9

File tree

6 files changed

+120
-7
lines changed

6 files changed

+120
-7
lines changed

docs/bundle/async_events.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ To make your listener async you have add `async: true` attribute to the tag `ker
3434
```yaml
3535
# app/config/config.yml
3636

37-
service:
37+
services:
3838
acme.foo_listener:
3939
class: 'AcmeBundle\Listener\FooListener'
4040
tags:
@@ -50,7 +50,7 @@ You can also add an async listener directly and register a custom message proces
5050
```yaml
5151
# app/config/config.yml
5252

53-
service:
53+
services:
5454
acme.async_foo_listener:
5555
class: 'Enqueue\Bundle\Events\AsyncListener'
5656
public: false
@@ -189,7 +189,7 @@ and register it:
189189
```yaml
190190
# app/config/config.yml
191191
192-
service:
192+
services:
193193
acme.foo_event_transofrmer:
194194
class: 'AcmeBundle\Listener\FooEventTransformer'
195195
arguments: ['@doctrine']

pkg/enqueue-bundle/DependencyInjection/EnqueueExtension.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function load(array $configs, ContainerBuilder $container)
116116
if (isset($config['async_events']['enabled'])) {
117117
$loader->load('events.yml');
118118

119-
if (isset($config['async_events']['spool_producer'])) {
119+
if (false == empty($config['async_events']['spool_producer'])) {
120120
$container->getDefinition('enqueue.events.async_listener')
121121
->replaceArgument(0, new Reference('enqueue.client.spool_producer'))
122122
;

pkg/enqueue-bundle/Resources/config/events.yml

-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ services:
88

99
enqueue.events.async_listener:
1010
class: 'Enqueue\Bundle\Events\AsyncListener'
11-
public: false
1211
arguments: ['@enqueue.client.producer', '@enqueue.events.registry']
1312

1413

1514
enqueue.events.event_dispatcher:
1615
class: 'Enqueue\Bundle\Events\ProxyEventDispatcher'
17-
public: false
1816
arguments:
1917
- '@event_dispatcher'
2018
- '@enqueue.events.async_listener'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Enqueue\Bundle\Tests\Functional\App;
4+
5+
class TestAsyncListener
6+
{
7+
public $calls = [];
8+
9+
public function onEvent()
10+
{
11+
$this->calls[] = func_get_args();
12+
}
13+
}

pkg/enqueue-bundle/Tests/Functional/App/config/config.yml

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,14 @@ enqueue:
3535
transport:
3636
default: 'null'
3737
'null': ~
38-
client: ~
38+
client:
39+
traceable_producer: true
3940
job: true
41+
async_events: true
42+
43+
44+
services:
45+
test_async_listener:
46+
class: 'Enqueue\Bundle\Tests\Functional\App\TestAsyncListener'
47+
tags:
48+
- { name: 'kernel.event_listener', async: true, event: 'test_async', method: 'onEvent' }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace Enqueue\Bundle\Tests\Functional\Events;
4+
5+
use Enqueue\Bundle\Events\AsyncListener;
6+
use Enqueue\Bundle\Tests\Functional\App\TestAsyncListener;
7+
use Enqueue\Bundle\Tests\Functional\WebTestCase;
8+
use Enqueue\Client\TraceableProducer;
9+
use Symfony\Component\EventDispatcher\Event;
10+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
11+
use Symfony\Component\EventDispatcher\GenericEvent;
12+
13+
class AsyncListenerTest extends WebTestCase
14+
{
15+
public function setUp()
16+
{
17+
parent::setUp();
18+
19+
/** @var AsyncListener $asyncListener */
20+
$asyncListener = $this->container->get('enqueue.events.async_listener');
21+
22+
$asyncListener->resetSyncMode();
23+
}
24+
25+
public function testShouldNotCallRealListenerIfMarkedAsAsync()
26+
{
27+
/** @var EventDispatcherInterface $dispatcher */
28+
$dispatcher = $this->container->get('event_dispatcher');
29+
30+
$dispatcher->dispatch('test_async', new GenericEvent('aSubject'));
31+
32+
/** @var TestAsyncListener $listner */
33+
$listner = $this->container->get('test_async_listener');
34+
35+
$this->assertEmpty($listner->calls);
36+
}
37+
38+
public function testShouldSendMessageToExpectedTopicInsteadOfCallingRealListener()
39+
{
40+
/** @var EventDispatcherInterface $dispatcher */
41+
$dispatcher = $this->container->get('event_dispatcher');
42+
43+
$event = new GenericEvent('theSubject', ['fooArg' => 'fooVal']);
44+
45+
$dispatcher->dispatch('test_async', $event);
46+
47+
/** @var TraceableProducer $producer */
48+
$producer = $this->container->get('enqueue.producer');
49+
50+
$traces = $producer->getTopicTraces('event.test_async');
51+
52+
$this->assertCount(1, $traces);
53+
54+
$this->assertEquals('event.test_async', $traces[0]['topic']);
55+
$this->assertEquals(serialize($event), $traces[0]['body']);
56+
}
57+
58+
public function testShouldSendMessageForEveryDispatchCall()
59+
{
60+
/** @var EventDispatcherInterface $dispatcher */
61+
$dispatcher = $this->container->get('event_dispatcher');
62+
63+
$dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal']));
64+
$dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal']));
65+
$dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal']));
66+
67+
/** @var TraceableProducer $producer */
68+
$producer = $this->container->get('enqueue.producer');
69+
70+
$traces = $producer->getTopicTraces('event.test_async');
71+
72+
$this->assertCount(3, $traces);
73+
}
74+
75+
public function testShouldSendMessageIfDispatchedFromInsideListener()
76+
{
77+
/** @var EventDispatcherInterface $dispatcher */
78+
$dispatcher = $this->container->get('event_dispatcher');
79+
80+
$dispatcher->addListener('foo', function (Event $event, $eventName, EventDispatcherInterface $dispatcher) {
81+
$dispatcher->dispatch('test_async', new GenericEvent('theSubject', ['fooArg' => 'fooVal']));
82+
});
83+
84+
$dispatcher->dispatch('foo');
85+
86+
/** @var TraceableProducer $producer */
87+
$producer = $this->container->get('enqueue.producer');
88+
89+
$traces = $producer->getTopicTraces('event.test_async');
90+
91+
$this->assertCount(1, $traces);
92+
}
93+
}

0 commit comments

Comments
 (0)