test: add comprehensive tests for Discovery module components
- Add tests for Results registries (AttributeRegistry, InterfaceRegistry, TemplateRegistry) - Add tests for Processing components (ProcessingContext) - Add tests for Memory components (MemoryGuard) - Add tests for Value Objects (DiscoveryOptions, DiscoveryContext) All new tests pass and cover core functionality including: - Registry operations (add, get, has, serialize/deserialize, optimize) - Processing context (reflection caching, file context management) - Memory guard (memory checks, statistics, emergency cleanup) - Value objects (factory methods, scan types, cache keys, metrics)
This commit is contained in:
89
tests/Framework/Discovery/Memory/MemoryGuardTest.php
Normal file
89
tests/Framework/Discovery/Memory/MemoryGuardTest.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Framework\Discovery\Memory;
|
||||
|
||||
use App\Framework\Core\ValueObjects\Byte;
|
||||
use App\Framework\Discovery\Memory\DiscoveryMemoryManager;
|
||||
use App\Framework\Discovery\Memory\GuardAction;
|
||||
use App\Framework\Discovery\Memory\MemoryGuard;
|
||||
use App\Framework\Discovery\Memory\MemoryStatus;
|
||||
use App\Framework\Discovery\ValueObjects\MemoryStrategy;
|
||||
|
||||
describe('MemoryGuard', function () {
|
||||
beforeEach(function () {
|
||||
$this->memoryManager = new DiscoveryMemoryManager(
|
||||
strategy: MemoryStrategy::BATCH,
|
||||
memoryLimit: Byte::fromMegabytes(128),
|
||||
memoryPressureThreshold: 0.8,
|
||||
memoryMonitor: null,
|
||||
logger: null,
|
||||
eventDispatcher: null,
|
||||
clock: null
|
||||
);
|
||||
|
||||
$this->guard = new MemoryGuard($this->memoryManager);
|
||||
});
|
||||
|
||||
it('can be instantiated', function () {
|
||||
expect($this->guard)->toBeInstanceOf(MemoryGuard::class);
|
||||
});
|
||||
|
||||
it('can check memory status', function () {
|
||||
$result = $this->guard->check();
|
||||
|
||||
expect($result)->toBeInstanceOf(\App\Framework\Discovery\Memory\GuardResult::class);
|
||||
expect($result->actions)->toBeArray();
|
||||
});
|
||||
|
||||
it('tracks check counter', function () {
|
||||
$result1 = $this->guard->check();
|
||||
$result2 = $this->guard->check();
|
||||
$result3 = $this->guard->check();
|
||||
|
||||
// All should succeed
|
||||
expect($result1)->toBeInstanceOf(\App\Framework\Discovery\Memory\GuardResult::class);
|
||||
expect($result2)->toBeInstanceOf(\App\Framework\Discovery\Memory\GuardResult::class);
|
||||
expect($result3)->toBeInstanceOf(\App\Framework\Discovery\Memory\GuardResult::class);
|
||||
});
|
||||
|
||||
it('can get statistics', function () {
|
||||
$this->guard->check();
|
||||
$this->guard->check();
|
||||
|
||||
$stats = $this->guard->getStatistics();
|
||||
|
||||
expect($stats)->toBeInstanceOf(\App\Framework\Discovery\Memory\GuardStatistics::class);
|
||||
expect($stats->totalChecks)->toBeGreaterThanOrEqual(2);
|
||||
});
|
||||
|
||||
it('handles emergency callback', function () {
|
||||
$callbackCalled = false;
|
||||
$callback = function () use (&$callbackCalled) {
|
||||
$callbackCalled = true;
|
||||
};
|
||||
|
||||
$guard = new MemoryGuard($this->memoryManager, $callback);
|
||||
|
||||
// With normal memory, callback shouldn't be called
|
||||
$guard->check();
|
||||
|
||||
// Callback might be called in critical situations
|
||||
expect($callbackCalled)->toBeBool();
|
||||
});
|
||||
|
||||
it('can reset guard state', function () {
|
||||
$this->guard->check();
|
||||
$this->guard->check();
|
||||
|
||||
$statsBefore = $this->guard->getStatistics();
|
||||
expect($statsBefore->totalChecks)->toBeGreaterThanOrEqual(2);
|
||||
|
||||
$this->guard->reset();
|
||||
|
||||
$statsAfter = $this->guard->getStatistics();
|
||||
expect($statsAfter->totalChecks)->toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user