php-sdk/tests/Unit/Server/Session/SessionManagerTest.php
Christopher Hertel 5e0731f706
Add configurable session garbage collection (gcProbability/gcDivisor) (#258)
Make session GC probability configurable via gcProbability and gcDivisor
parameters, mirroring PHP's session.gc_probability/session.gc_divisor.
Exposed through both SessionManager constructor and Builder::setSession().

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-15 08:14:38 +02:00

80 lines
2.5 KiB
PHP

<?php
/*
* This file is part of the official PHP MCP SDK.
*
* A collaboration between Symfony and the PHP Foundation.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mcp\Tests\Unit\Server\Session;
use Mcp\Exception\InvalidArgumentException;
use Mcp\Server\Session\InMemorySessionStore;
use Mcp\Server\Session\SessionManager;
use PHPUnit\Framework\TestCase;
class SessionManagerTest extends TestCase
{
public function testGcDisabledWhenProbabilityIsZero(): void
{
$store = $this->createMock(InMemorySessionStore::class);
$store->expects($this->never())->method('gc');
$manager = new SessionManager($store, gcProbability: 0);
// Call gc many times — it should never trigger
for ($i = 0; $i < 100; ++$i) {
$manager->gc();
}
}
public function testGcAlwaysRunsWhenProbabilityEqualsDivisor(): void
{
$store = $this->createMock(InMemorySessionStore::class);
$store->expects($this->exactly(10))->method('gc')->willReturn([]);
$manager = new SessionManager($store, gcProbability: 1, gcDivisor: 1);
for ($i = 0; $i < 10; ++$i) {
$manager->gc();
}
}
public function testGcAlwaysRunsWhenProbabilityExceedsDivisor(): void
{
$store = $this->createMock(InMemorySessionStore::class);
$store->expects($this->exactly(5))->method('gc')->willReturn([]);
$manager = new SessionManager($store, gcProbability: 100, gcDivisor: 1);
for ($i = 0; $i < 5; ++$i) {
$manager->gc();
}
}
public function testGcProbabilityMustBeNonNegative(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('gcProbability must be greater than or equal to 0.');
new SessionManager(new InMemorySessionStore(), gcProbability: -1);
}
public function testGcDivisorMustBePositive(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('gcDivisor must be greater than or equal to 1.');
new SessionManager(new InMemorySessionStore(), gcDivisor: 0);
}
public function testDefaultGcConfiguration(): void
{
// Default should be 1/100 — just verify construction works
$manager = new SessionManager(new InMemorySessionStore());
$this->assertInstanceOf(SessionManager::class, $manager);
}
}