Skip to content

[Messenger] InMemoryTransport handle acknowledged and rejected messages #32783

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

Merged
merged 1 commit into from
Aug 18, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Deprecated passing a `ContainerInterface` instance as first argument of the `ConsumeMessagesCommand` constructor,
pass a `RoutableMessageBus` instance instead.
* Added support for auto trimming of Redis streams.
* `InMemoryTransport` handle acknowledged and rejected messages.

4.3.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,20 @@ public function testSend()
{
$envelope = new Envelope(new \stdClass());
$this->transport->send($envelope);
$this->assertSame([$envelope], $this->transport->get());
$this->assertSame([$envelope], $this->transport->getSent());
}

public function testQueue()
{
$envelope1 = new Envelope(new \stdClass());
$this->transport->send($envelope1);
$envelope2 = new Envelope(new \stdClass());
$this->transport->send($envelope2);
$this->assertSame([$envelope1, $envelope2], $this->transport->get());
$this->transport->ack($envelope1);
$this->assertSame([$envelope2], $this->transport->get());
$this->transport->reject($envelope2);
$this->assertSame([], $this->transport->get());
}

public function testAck()
Expand Down Expand Up @@ -63,5 +76,6 @@ public function testReset()
$this->assertEmpty($this->transport->get(), 'Should be empty after reset');
$this->assertEmpty($this->transport->getAcknowledged(), 'Should be empty after reset');
$this->assertEmpty($this->transport->getRejected(), 'Should be empty after reset');
$this->assertEmpty($this->transport->getSent(), 'Should be empty after reset');
}
}
23 changes: 21 additions & 2 deletions src/Symfony/Component/Messenger/Transport/InMemoryTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ class InMemoryTransport implements TransportInterface, ResetInterface
*/
private $rejected = [];

/**
* @var Envelope[]
*/
private $queue = [];

/**
* {@inheritdoc}
*/
public function get(): iterable
{
return $this->sent;
return array_values($this->queue);
}

/**
Expand All @@ -50,6 +55,8 @@ public function get(): iterable
public function ack(Envelope $envelope): void
{
$this->acknowledged[] = $envelope;
$id = spl_object_hash($envelope);
unset($this->queue[$id]);
}

/**
Expand All @@ -58,6 +65,8 @@ public function ack(Envelope $envelope): void
public function reject(Envelope $envelope): void
{
$this->rejected[] = $envelope;
$id = spl_object_hash($envelope);
unset($this->queue[$id]);
}

/**
Expand All @@ -66,13 +75,15 @@ public function reject(Envelope $envelope): void
public function send(Envelope $envelope): Envelope
{
$this->sent[] = $envelope;
$id = spl_object_hash($envelope);
$this->queue[$id] = $envelope;

return $envelope;
}

public function reset()
{
$this->sent = $this->rejected = $this->acknowledged = [];
$this->sent = $this->queue = $this->rejected = $this->acknowledged = [];
}

/**
Expand All @@ -90,4 +101,12 @@ public function getRejected(): array
{
return $this->rejected;
}

/**
* @return Envelope[]
*/
public function getSent(): array
{
return $this->sent;
}
}