Skip to content
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

Force ping after transport exception #36417

Merged
merged 1 commit into from Apr 12, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
Expand Down Expand Up @@ -43,6 +44,29 @@ public function testSendDoesNotPingBelowThreshold(): void
$this->assertNotContains("NOOP\r\n", $stream->getCommands());
}

public function testSendPingAfterTransportException(): void
{
$stream = new DummyStream();
$envelope = new Envelope(new Address('sender@example.org'), [new Address('recipient@example.org')]);

$transport = new SmtpTransport($stream);
$transport->send(new RawMessage('Message 1'), $envelope);
$stream->close();
$catch = false;

try {
$transport->send(new RawMessage('Message 2'), $envelope);
} catch (TransportException $exception) {
$catch = true;
}
$this->assertTrue($catch);
$this->assertTrue($stream->isClosed());

$transport->send(new RawMessage('Message 3'), $envelope);

$this->assertFalse($stream->isClosed());
}

public function testSendDoesPingAboveThreshold(): void
{
$stream = new DummyStream();
Expand Down Expand Up @@ -76,13 +100,23 @@ class DummyStream extends AbstractStream
*/
private $commands;

/**
* @var bool
*/
private $closed = true;

public function initialize(): void
{
$this->closed = false;
$this->nextResponse = '220 localhost';
}

public function write(string $bytes, $debug = true): void
{
if ($this->closed) {
throw new TransportException('Unable to write bytes on the wire.');
}

$this->commands[] = $bytes;

if (0 === strpos($bytes, 'DATA')) {
Expand Down Expand Up @@ -120,4 +154,14 @@ protected function getReadConnectionDescription(): string
{
return 'null';
}

public function close(): void
{
$this->closed = true;
}

public function isClosed(): bool
{
return $this->closed;
}
}
5 changes: 2 additions & 3 deletions src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
Expand Up @@ -206,12 +206,11 @@ protected function doSend(SentMessage $message): void
$this->stream->flush();
$this->executeCommand("\r\n.\r\n", [250]);
$message->appendDebug($this->stream->getDebug());
$this->lastMessageTime = microtime(true);
} catch (TransportExceptionInterface $e) {
$e->appendDebug($this->stream->getDebug());

$this->lastMessageTime = 0;
throw $e;
} finally {
$this->lastMessageTime = microtime(true);
}
}

Expand Down