Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 290a729

Browse files
committed
Fixing issue where worker-only middleware were run in all contexts
1 parent ec8e34f commit 290a729

6 files changed

+82
-8
lines changed

src/Symfony/Bridge/Doctrine/Messenger/DoctrineClearEntityManagerMiddleware.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\ORM\EntityManagerInterface;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Middleware\StackInterface;
17+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1718

1819
/**
1920
* Clears entity manager after calling all handlers.
@@ -27,7 +28,9 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
2728
try {
2829
return $stack->next()->handle($envelope, $stack);
2930
} finally {
30-
$entityManager->clear();
31+
if (null !== $envelope->last(ReceivedStamp::class)) {
32+
$entityManager->clear();
33+
}
3134
}
3235
}
3336
}

src/Symfony/Bridge/Doctrine/Messenger/DoctrineCloseConnectionMiddleware.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\ORM\EntityManagerInterface;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Middleware\StackInterface;
17+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1718

1819
/**
1920
* Closes connection and therefore saves number of connections.
@@ -29,7 +30,9 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
2930

3031
return $stack->next()->handle($envelope, $stack);
3132
} finally {
32-
$connection->close();
33+
if (null !== $envelope->last(ReceivedStamp::class)) {
34+
$connection->close();
35+
}
3336
}
3437
}
3538
}

src/Symfony/Bridge/Doctrine/Messenger/DoctrinePingConnectionMiddleware.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\ORM\EntityManagerInterface;
1515
use Symfony\Component\Messenger\Envelope;
1616
use Symfony\Component\Messenger\Middleware\StackInterface;
17+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1718

1819
/**
1920
* Checks whether the connection is still open or reconnects otherwise.
@@ -23,6 +24,15 @@
2324
class DoctrinePingConnectionMiddleware extends AbstractDoctrineMiddleware
2425
{
2526
protected function handleForManager(EntityManagerInterface $entityManager, Envelope $envelope, StackInterface $stack): Envelope
27+
{
28+
if (null !== $envelope->last(ReceivedStamp::class)) {
29+
$this->pingConnection($entityManager);
30+
}
31+
32+
return $stack->next()->handle($envelope, $stack);
33+
}
34+
35+
private function pingConnection(EntityManagerInterface $entityManager)
2636
{
2737
$connection = $entityManager->getConnection();
2838

@@ -34,7 +44,5 @@ protected function handleForManager(EntityManagerInterface $entityManager, Envel
3444
if (!$entityManager->isOpen()) {
3545
$this->managerRegistry->resetManager($this->entityManagerName);
3646
}
37-
38-
return $stack->next()->handle($envelope, $stack);
3947
}
4048
}

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineClearEntityManagerMiddlewareTest.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerMiddleware;
1717
use Symfony\Component\Messenger\Envelope;
1818
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
19+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
1920
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2021

2122
class DoctrineClearEntityManagerMiddlewareTest extends MiddlewareTestCase
@@ -34,7 +35,10 @@ public function testMiddlewareClearEntityManager()
3435

3536
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
3637

37-
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
38+
$envelope = new Envelope(new \stdClass(), [
39+
new ReceivedStamp('async'),
40+
]);
41+
$middleware->handle($envelope, $this->getStackMock());
3842
}
3943

4044
public function testInvalidEntityManagerThrowsException()
@@ -51,4 +55,22 @@ public function testInvalidEntityManagerThrowsException()
5155

5256
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
5357
}
58+
59+
public function testMiddlewareDoesNotClearInNonWorkerContext()
60+
{
61+
$entityManager = $this->createMock(EntityManagerInterface::class);
62+
$entityManager->expects($this->never())
63+
->method('clear');
64+
65+
$managerRegistry = $this->createMock(ManagerRegistry::class);
66+
$managerRegistry
67+
->method('getManager')
68+
->with('default')
69+
->willReturn($entityManager);
70+
71+
$middleware = new DoctrineClearEntityManagerMiddleware($managerRegistry, 'default');
72+
73+
$envelope = new Envelope(new \stdClass());
74+
$middleware->handle($envelope, $this->getStackMock());
75+
}
5476
}

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrineCloseConnectionMiddlewareTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bridge\Doctrine\Messenger\DoctrineCloseConnectionMiddleware;
1818
use Symfony\Component\Messenger\Envelope;
1919
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
20+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2021
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2122

2223
class DoctrineCloseConnectionMiddlewareTest extends MiddlewareTestCase
@@ -49,7 +50,10 @@ public function testMiddlewareCloseConnection()
4950
->method('close')
5051
;
5152

52-
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
53+
$envelope = new Envelope(new \stdClass(), [
54+
new ReceivedStamp('async'),
55+
]);
56+
$this->middleware->handle($envelope, $this->getStackMock());
5357
}
5458

5559
public function testInvalidEntityManagerThrowsException()
@@ -66,4 +70,14 @@ public function testInvalidEntityManagerThrowsException()
6670

6771
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
6872
}
73+
74+
public function testMiddlewareNotCloseInNonWorkerContext()
75+
{
76+
$this->connection->expects($this->never())
77+
->method('close')
78+
;
79+
80+
$envelope = new Envelope(new \stdClass());
81+
$this->middleware->handle($envelope, $this->getStackMock());
82+
}
6983
}

src/Symfony/Bridge/Doctrine/Tests/Messenger/DoctrinePingConnectionMiddlewareTest.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Bridge\Doctrine\Messenger\DoctrinePingConnectionMiddleware;
1818
use Symfony\Component\Messenger\Envelope;
1919
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
20+
use Symfony\Component\Messenger\Stamp\ReceivedStamp;
2021
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase;
2122

2223
class DoctrinePingConnectionMiddlewareTest extends MiddlewareTestCase
@@ -56,7 +57,10 @@ public function testMiddlewarePingOk()
5657
->method('connect')
5758
;
5859

59-
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
60+
$envelope = new Envelope(new \stdClass(), [
61+
new ReceivedStamp('async'),
62+
]);
63+
$this->middleware->handle($envelope, $this->getStackMock());
6064
}
6165

6266
public function testMiddlewarePingResetEntityManager()
@@ -70,7 +74,10 @@ public function testMiddlewarePingResetEntityManager()
7074
->with($this->entityManagerName)
7175
;
7276

73-
$this->middleware->handle(new Envelope(new \stdClass()), $this->getStackMock());
77+
$envelope = new Envelope(new \stdClass(), [
78+
new ReceivedStamp('async'),
79+
]);
80+
$this->middleware->handle($envelope, $this->getStackMock());
7481
}
7582

7683
public function testInvalidEntityManagerThrowsException()
@@ -87,4 +94,21 @@ public function testInvalidEntityManagerThrowsException()
8794

8895
$middleware->handle(new Envelope(new \stdClass()), $this->getStackMock(false));
8996
}
97+
98+
public function testMiddlewareNoPingInNonWorkerContext()
99+
{
100+
$this->connection->expects($this->never())
101+
->method('ping')
102+
->willReturn(false);
103+
104+
$this->connection->expects($this->never())
105+
->method('close')
106+
;
107+
$this->connection->expects($this->never())
108+
->method('connect')
109+
;
110+
111+
$envelope = new Envelope(new \stdClass());
112+
$this->middleware->handle($envelope, $this->getStackMock());
113+
}
90114
}

0 commit comments

Comments
 (0)