$ErrorActionPreference = "Stop" Write-Host "==========================================================" -ForegroundColor Cyan Write-Host " P1-01: Client Hung & Kill Recovery Verification " -ForegroundColor Cyan Write-Host "==========================================================" -ForegroundColor Cyan $serviceLogDir = "C:\Program Files\ClassNotify\logs" if (-not (Test-Path $serviceLogDir)) { $serviceLogDir = "C:\ProgramData\ClassNotify\logs" } function Get-RecentServiceLogs([int]$lines = 25) { $latestLog = Get-ChildItem -Path $serviceLogDir -Filter "service-*.log" -ErrorAction SilentlyContinue | Sort-Object LastWriteTime -Descending | Select-Object -First 1 if ($latestLog) { return (Get-Content -Path $latestLog.FullName -Tail $lines -ErrorAction SilentlyContinue) } return @() } # ------------------------------------------------------------------------------ # Test A: Kill Client Process & Verify Auto-Recovery # ------------------------------------------------------------------------------ Write-Host "`n>>> [Test A] Kill Client Process & Verify Auto-Recovery..." -ForegroundColor Yellow $procBefore = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue if ($procBefore) { Write-Host "Current Client PID: $($procBefore.Id), SessionId: $($procBefore.SessionId)" -ForegroundColor White } else { Write-Host "Client is not currently running. Waiting 5s for watchdog to launch it..." -ForegroundColor White Start-Sleep -Seconds 5 $procBefore = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue if ($procBefore) { Write-Host "Client launched by Watchdog! PID: $($procBefore.Id), SessionId: $($procBefore.SessionId)" -ForegroundColor Green } } Write-Host "Killing ClassNotify.Client process..." -ForegroundColor Yellow Stop-Process -Name "ClassNotify.Client" -Force -ErrorAction SilentlyContinue $stopTime = Get-Date Write-Host "Client killed at $($stopTime.ToString('HH:mm:ss')). Waiting for Watchdog recovery (up to 12s)..." -ForegroundColor White $recovered = $false $newProc = $null for ($i = 0; $i -lt 24; $i++) { Start-Sleep -Milliseconds 500 $newProc = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue if ($newProc -and ($null -eq $procBefore -or $newProc.Id -ne $procBefore.Id)) { $recovered = $true break } } $recoveryDuration = [math]::Round(((Get-Date) - $stopTime).TotalSeconds, 1) if ($recovered) { Write-Host "[PASS] Client auto-recovered in $recoveryDuration seconds! New PID: $($newProc.Id), SessionId: $($newProc.SessionId)" -ForegroundColor Green } else { Write-Host "[FAIL] Client was not recovered within timeout!" -ForegroundColor Red } Start-Sleep -Seconds 3 Write-Host "`nRecent Service Logs after Test A:" -ForegroundColor Cyan Get-RecentServiceLogs 15 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } # ------------------------------------------------------------------------------ # Test B: Simulate Hang & Verify Hung Detection and Recovery # ------------------------------------------------------------------------------ Write-Host "`n>>> [Test B] Simulate Hang & Verify Hung Detection..." -ForegroundColor Yellow # Kill current normal client Stop-Process -Name "ClassNotify.Client" -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 1 # Launch client with --simulate-hang argument in user session $clientExe = "C:\Program Files\ClassNotify\Client\ClassNotify.Client.exe" Write-Host "Launching client with --simulate-hang (PID will hold mutex and not process IPC)..." -ForegroundColor Yellow $hungProc = Start-Process -FilePath $clientExe -ArgumentList "--simulate-hang" -PassThru Write-Host "Simulated Hung Client PID: $($hungProc.Id), SessionId: $($hungProc.SessionId)" -ForegroundColor White Write-Host "Waiting up to 25s for Watchdog 3/3 timeout detection, process termination, and recovery..." -ForegroundColor White $hangRecovered = $false $hangStopTime = Get-Date $hangNewProc = $null for ($i = 0; $i -lt 50; $i++) { Start-Sleep -Milliseconds 500 # Check if hung process was terminated $hungStillRunning = Get-Process -Id $hungProc.Id -ErrorAction SilentlyContinue if (-not $hungStillRunning) { # Check if a new client process has been launched $hangNewProc = Get-Process -Name "ClassNotify.Client" -ErrorAction SilentlyContinue if ($hangNewProc -and $hangNewProc.Id -ne $hungProc.Id) { $hangRecovered = $true break } } } $hangDuration = [math]::Round(((Get-Date) - $hangStopTime).TotalSeconds, 1) if ($hangRecovered) { Write-Host "[PASS] Hung client (PID: $($hungProc.Id)) was successfully terminated and replaced by healthy client (PID: $($hangNewProc.Id)) in $hangDuration seconds!" -ForegroundColor Green } else { Write-Host "[FAIL] Hung client recovery failed or timed out!" -ForegroundColor Red } Start-Sleep -Seconds 3 Write-Host "`nRecent Service Logs after Test B:" -ForegroundColor Cyan Get-RecentServiceLogs 25 | ForEach-Object { Write-Host " $_" -ForegroundColor Gray } Write-Host "`n==========================================================" -ForegroundColor Green Write-Host " P1-01 Verification Finished " -ForegroundColor Green Write-Host "==========================================================" -ForegroundColor Green