Skip to content

Commit

Permalink
Replace SplObjectStorage with array.
Browse files Browse the repository at this point in the history
SplObjectStorage did not free its memory correctly.
  • Loading branch information
henrikbjorn committed Sep 18, 2013
1 parent 36efd6c commit 3e863c8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
16 changes: 10 additions & 6 deletions src/Bernard/Queue/PersistentQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct($name, Driver $driver, Serializer $serializer)

$this->driver = $driver;
$this->serializer = $serializer;
$this->receipts = new SplObjectStorage;
$this->receipts = array();

$this->register();
}
Expand Down Expand Up @@ -76,8 +76,10 @@ public function acknowledge(Envelope $envelope)
{
$this->errorIfClosed();

if (isset($this->receipts[$envelope])) {
$this->driver->acknowledgeMessage($this->name, $this->receipts[$envelope]);
if ($receipt = array_search($envelope, $this->receipts, true)) {
$this->driver->acknowledgeMessage($this->name, $receipt);

unset($this->receipts[$receipt]);
}
}

Expand All @@ -90,12 +92,14 @@ public function dequeue()

list($serialized, $receipt) = $this->driver->popMessage($this->name);

if (!$serialized) {
return;
}

if ($serialized) {
$envelope = $this->serializer->deserialize($serialized);

$this->receipts->attach($envelope, $receipt);

return $envelope;
return $this->receipts[$receipt] = $envelope;
}
}

Expand Down
20 changes: 10 additions & 10 deletions tests/Bernard/Tests/Queue/PersistentQueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PersistentQueueTest extends AbstractQueueTest
{
public function setUp()
{
$this->connection = $this->getMock('Bernard\Driver');
$this->driver = $this->getMock('Bernard\Driver');
$this->serializer = $this->getMock('Bernard\Serializer');
}

Expand All @@ -19,7 +19,7 @@ public function testEnqueue()

$this->serializer->expects($this->once())->method('serialize')->with($this->equalTo($envelope))
->will($this->returnValue('serialized message'));
$this->connection->expects($this->once())->method('pushMessage')
$this->driver->expects($this->once())->method('pushMessage')
->with($this->equalTo('send-newsletter'), $this->equalTo('serialized message'));

$queue = $this->createQueue('send-newsletter');
Expand All @@ -30,10 +30,10 @@ public function testAcknowledge()
{
$envelope = new Envelope($this->getMock('Bernard\Message'));

$this->connection->expects($this->once())->method('acknowledgeMessage')
$this->driver->expects($this->once())->method('acknowledgeMessage')
->with($this->equalTo('send-newsletter'), $this->equalTo('receipt'));

$this->connection->expects($this->once())->method('popMessage')->with($this->equalTo('send-newsletter'))
$this->driver->expects($this->once())->method('popMessage')->with($this->equalTo('send-newsletter'))
->will($this->returnValue(array('message', 'receipt')));

$this->serializer->expects($this->once())->method('deserialize')
Expand All @@ -48,15 +48,15 @@ public function testAcknowledgeOnlyIfReceipt()
{
$envelope = new Envelope($this->getMock('Bernard\Message'));

$this->connection->expects($this->never())->method('acknowledgeMessage');
$this->driver->expects($this->never())->method('acknowledgeMessage');

$queue = $this->createQueue('send-newsletter');
$queue->acknowledge($envelope);
}

public function testCount()
{
$this->connection->expects($this->once())->method('countMessages')->with($this->equalTo('send-newsletter'))
$this->driver->expects($this->once())->method('countMessages')->with($this->equalTo('send-newsletter'))
->will($this->returnValue(10));

$queue = $this->createQueue('send-newsletter');
Expand All @@ -68,10 +68,10 @@ public function testDequeue()
{
$messageWrapper = new Envelope($this->getMock('Bernard\Message'));

$this->connection->expects($this->at(1))->method('popMessage')->with($this->equalTo('send-newsletter'))
$this->driver->expects($this->at(1))->method('popMessage')->with($this->equalTo('send-newsletter'))
->will($this->returnValue(array('serialized', null)));

$this->connection->expects($this->at(2))->method('popMessage')->with($this->equalTo('send-newsletter'))
$this->driver->expects($this->at(2))->method('popMessage')->with($this->equalTo('send-newsletter'))
->will($this->returnValue(null));

$this->serializer->expects($this->once())->method('deserialize')->with($this->equalTo('serialized'))
Expand All @@ -92,7 +92,7 @@ public function testPeekDeserializesMessages($index, $limit)
$this->serializer->expects($this->at(1))->method('deserialize')->with($this->equalTo('message2'));
$this->serializer->expects($this->at(2))->method('deserialize')->with($this->equalTo('message3'));

$this->connection->expects($this->once())->method('peekQueue')->with($this->equalTo('send-newsletter'), $this->equalTo($index), $this->equalTo($limit))
$this->driver->expects($this->once())->method('peekQueue')->with($this->equalTo('send-newsletter'), $this->equalTo($index), $this->equalTo($limit))
->will($this->returnValue(array('message1', 'message2', 'message3')));

$queue = $this->createQueue('send-newsletter');
Expand All @@ -118,6 +118,6 @@ public function peekDataProvider()

protected function createQueue($name)
{
return new PersistentQueue($name, $this->connection, $this->serializer);
return new PersistentQueue($name, $this->driver, $this->serializer);
}
}

0 comments on commit 3e863c8

Please sign in to comment.