Skip to content

Commit

Permalink
Add destroy logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Apr 19, 2024
1 parent d2870ab commit 588c25c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Pools/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,16 @@ public function reclaim(): Pool

return $this->pool->reclaim($this);
}

/**
* @return Pool
*/
public function destroy(): Pool
{
if ($this->pool === null) {
throw new Exception('You cannot destroy connection that does not have a pool.');
}

return $this->pool->destroy($this);
}
}
21 changes: 21 additions & 0 deletions src/Pools/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,27 @@ public function reclaim(Connection $connection = null): self
return $this;
}


/**
* @param Connection|null $connection
* @return self
*/
public function destroy(Connection $connection = null): self
{
if ($connection !== null) {
array_push($this->pool, true);
unset($this->active[$connection->getID()]);
return $this;
}

foreach ($this->active as $connection) {
array_push($this->pool, true);
unset($this->active[$connection->getID()]);
}

return $this;
}

/**
* @return bool
*/
Expand Down
32 changes: 32 additions & 0 deletions tests/Pools/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,36 @@ public function testReclaimException(): void
$this->expectException(Exception::class);
$this->object->reclaim();
}

public function testDestroy(): void
{
$i = 0;
$object = new Pool('testDestroy', 2, function () use (&$i) {
$i++;
return $i <= 2 ? 'x' : 'y';
});

$this->assertEquals(2, $object->count());

$connection1 = $object->pop();
$connection2 = $object->pop();

$this->assertEquals(0, $object->count());

$this->assertEquals('x', $connection1->getResource());
$this->assertEquals('x', $connection2->getResource());

$connection1->destroy();
$connection2->destroy();

$this->assertEquals(2, $object->count());

$connection1 = $object->pop();
$connection2 = $object->pop();

$this->assertEquals(0, $object->count());

$this->assertEquals('y', $connection1->getResource());
$this->assertEquals('y', $connection2->getResource());
}
}
31 changes: 31 additions & 0 deletions tests/Pools/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,35 @@ public function testRetry(): void

$this->assertGreaterThanOrEqual(4, $timeDiff);
}

public function testDestroy(): void
{
$i = 0;
$object = new Pool('testDestroy', 2, function () use (&$i) {
$i++;
return $i <= 2 ? 'x' : 'y';
});

$this->assertEquals(2, $object->count());

$connection1 = $object->pop();
$connection2 = $object->pop();

$this->assertEquals(0, $object->count());

$this->assertEquals('x', $connection1->getResource());
$this->assertEquals('x', $connection2->getResource());

$object->destroy();

$this->assertEquals(2, $object->count());

$connection1 = $object->pop();
$connection2 = $object->pop();

$this->assertEquals(0, $object->count());

$this->assertEquals('y', $connection1->getResource());
$this->assertEquals('y', $connection2->getResource());
}
}

0 comments on commit 588c25c

Please sign in to comment.