push($payload); echo " ๐Ÿ“Š Queue size after push: " . $queue->size() . "\n"; // Debug: Check directory contents $priorityFiles = $queue->getPriorityJobFiles(); echo " ๐Ÿ“ Priority files count: " . count($priorityFiles) . "\n"; $delayedFiles = $queue->getDelayedJobFiles(); echo " โฑ๏ธ Delayed files count: " . count($delayedFiles) . "\n"; assert($queue->size() === 1, "โŒ Queue should have 1 job"); $poppedPayload = $queue->pop(); assert($poppedPayload !== null, "โŒ Should pop a payload"); assert($poppedPayload->job instanceof FileSystemTestJob, "โŒ Should be FileSystemTestJob"); assert($poppedPayload->job->id === 'job-1', "โŒ Job ID should match"); assert($queue->size() === 0, "โŒ Queue should be empty after pop"); echo "โœ… Basic FileQueue operations with Filesystem module work correctly\n\n"; // Test 2: Priority-based Job Processing echo "2๏ธโƒฃ Testing Priority-based Job Processing...\n"; $queue->clear(); $lowJob = JobPayload::create(new FileSystemTestJob('low', 'Low priority'), QueuePriority::low()); $normalJob = JobPayload::create(new FileSystemTestJob('normal', 'Normal priority'), QueuePriority::normal()); $highJob = JobPayload::create(new FileSystemTestJob('high', 'High priority'), QueuePriority::high()); $criticalJob = JobPayload::create(new FileCriticalTask('critical', 'Critical task'), QueuePriority::critical()); // Push in random order $queue->push($normalJob); $queue->push($lowJob); $queue->push($criticalJob); $queue->push($highJob); assert($queue->size() === 4, "โŒ Queue should have 4 jobs"); // Should pop in priority order: critical > high > normal > low $first = $queue->pop(); $second = $queue->pop(); $third = $queue->pop(); $fourth = $queue->pop(); assert($first !== null && $first->job instanceof FileCriticalTask, "โŒ First should be critical task"); assert($second !== null && $second->job->id === 'high', "โŒ Second should be high priority"); assert($third !== null && $third->job->id === 'normal', "โŒ Third should be normal priority"); assert($fourth !== null && $fourth->job->id === 'low', "โŒ Fourth should be low priority"); echo "โœ… Priority-based job processing works correctly\n\n"; // Test 3: Delayed Jobs with Filesystem echo "3๏ธโƒฃ Testing Delayed Jobs with Filesystem Module...\n"; $queue->clear(); $immediateJob = JobPayload::create(new FileSystemTestJob('immediate', 'Process now')); $delayedJob = JobPayload::create( new FileSystemTestJob('delayed', 'Process later'), QueuePriority::normal(), Duration::fromSeconds(2) // 2 second delay ); $queue->push($delayedJob); $queue->push($immediateJob); // Should only get immediate job initially $popped = $queue->pop(); assert($popped !== null, "โŒ Should pop immediate job"); assert($popped->job->id === 'immediate', "โŒ Should be immediate job"); // Delayed job should still be in queue but not poppable yet assert($queue->size() === 1, "โŒ Should have 1 delayed job remaining"); // Wait for delay to pass and try again echo " โณ Waiting 3 seconds for delayed job to become ready...\n"; sleep(3); $delayedPopped = $queue->pop(); assert($delayedPopped !== null, "โŒ Should pop delayed job after delay"); assert($delayedPopped->job->id === 'delayed', "โŒ Should be delayed job"); echo "โœ… Delayed jobs with filesystem work correctly\n\n"; // Test 4: Peek Operations echo "4๏ธโƒฃ Testing Peek Operations...\n"; $queue->clear(); $peekJob = JobPayload::create(new FileSystemTestJob('peek', 'Test peek')); $queue->push($peekJob); $originalSize = $queue->size(); $peeked = $queue->peek(); assert($peeked !== null, "โŒ Should peek a payload"); assert($peeked->job->id === 'peek', "โŒ Peeked job should have correct data"); assert($queue->size() === $originalSize, "โŒ Queue size should not change after peek"); echo "โœ… Peek operations work correctly\n\n"; // Test 5: Queue Statistics with Priority Breakdown echo "5๏ธโƒฃ Testing Queue Statistics...\n"; $queue->clear(); $payload1 = JobPayload::create(new FileSystemTestJob('stats-1', 'test'), QueuePriority::high()); $payload2 = JobPayload::create(new FileSystemTestJob('stats-2', 'test'), QueuePriority::normal()); $payload3 = JobPayload::create(new FileSystemTestJob('stats-3', 'test'), QueuePriority::high()); $queue->push($payload1); $queue->push($payload2); $queue->push($payload3); $stats = $queue->getStats(); assert($stats['total_size'] === 3, "โŒ Stats should show 3 total jobs"); assert($stats['priority_queue_size'] === 3, "โŒ Stats should show 3 priority jobs"); assert($stats['delayed_queue_size'] === 0, "โŒ Stats should show 0 delayed jobs"); assert(isset($stats['priority_breakdown']), "โŒ Stats should include priority breakdown"); $breakdown = $stats['priority_breakdown']; assert(isset($breakdown['high']), "โŒ Should have high priority breakdown"); assert(isset($breakdown['normal']), "โŒ Should have normal priority breakdown"); assert($breakdown['high'] === 2, "โŒ Should have 2 high priority jobs"); assert($breakdown['normal'] === 1, "โŒ Should have 1 normal priority job"); echo "โœ… Queue statistics work correctly\n\n"; // Test 6: Directory Structure Verification echo "6๏ธโƒฃ Testing Directory Structure...\n"; // Verify that the filesystem module created the correct directory structure $priorityDir = $queuePath . '/priority'; $delayedDir = $queuePath . '/delayed'; assert(is_dir($priorityDir), "โŒ Priority directory should exist"); assert(is_dir($delayedDir), "โŒ Delayed directory should exist"); echo "โœ… Directory structure is correct\n\n"; // Test 7: Clear Operations echo "7๏ธโƒฃ Testing Clear Operations...\n"; $queue->clear(); $payload1 = JobPayload::create(new FileSystemTestJob('clear-1', 'test')); $payload2 = JobPayload::create(new FileSystemTestJob('clear-2', 'test')); $delayedPayload = JobPayload::create( new FileSystemTestJob('clear-delayed', 'test'), QueuePriority::normal(), Duration::fromSeconds(3600) ); $queue->push($payload1); $queue->push($payload2); $queue->push($delayedPayload); assert($queue->size() === 3, "โŒ Should have 3 jobs before clear"); $clearedCount = $queue->clear(); assert($clearedCount === 3, "โŒ Should report 3 jobs cleared"); assert($queue->size() === 0, "โŒ Queue should be empty after clear"); echo "โœ… Clear operations work correctly\n\n"; // Test 8: Empty Queue Handling echo "8๏ธโƒฃ Testing Empty Queue Handling...\n"; $queue->clear(); assert($queue->size() === 0, "โŒ Queue should be empty"); $nullPayload = $queue->pop(); assert($nullPayload === null, "โŒ Popping from empty queue should return null"); $nullPeek = $queue->peek(); assert($nullPeek === null, "โŒ Peeking empty queue should return null"); echo "โœ… Empty queue handling works correctly\n\n"; // Cleanup echo "๐Ÿงน Cleaning up test files...\n"; function deleteDirectory($dir) { if (! is_dir($dir)) { return; } $files = array_diff(scandir($dir), ['.', '..']); foreach ($files as $file) { $path = $dir . DIRECTORY_SEPARATOR . $file; if (is_dir($path)) { deleteDirectory($path); } else { unlink($path); } } rmdir($dir); } deleteDirectory($queuePath); echo "๐ŸŽ‰ ALL FILEQUEUE FILESYSTEM TESTS PASSED!\n"; echo "โœจ FileQueue with Filesystem module integration is working correctly!\n";