@echo off setlocal EnableDelayedExpansion cd /d "%~dp0.." set BASE=http://localhost:8080 set PASS=Example-only-not-a-real-secret-123 set FIXTURE=tests\fixtures\sample.png set TMP=%TEMP%\lab5-lease if not exist "%TMP%" mkdir "%TMP%" call :guid set RND=%GUIDVAL% set U=lease%RND% call :register "%U%" "Lease" u if errorlevel 1 exit /b 1 set UID=!u_ID! call :login "%U%" u if errorlevel 1 exit /b 1 set TOKEN=!u_TOKEN! curl.exe -s -X POST "%BASE%/api/v1/chats" ^ -H "Authorization: Bearer %TOKEN%" -H "Content-Type: application/json" ^ -d "{\"title\":\"Lease expiry\",\"member_ids\":[\"%UID%\"]}" ^ -o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code" call :check_code chat.code 201 "create chat" if errorlevel 1 exit /b 1 for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a echo === Stop worker and upload (job stays queued) === docker compose --compatibility stop worker curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^ -H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^ --data-binary "@%FIXTURE%" -o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code" call :check_code att.code 202 "upload" if errorlevel 1 ( docker compose --compatibility up -d & exit /b 1 ) for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a echo attachment=%ATT_ID% echo === CASE A: expired lease on attempt 5 must become failed === for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "UPDATE jobs SET status='processing', attempt=5, lease_id='injected', lease_expires_at=now()-interval '2 minutes' WHERE attachment_id='%ATT_ID%'; UPDATE attachments SET status='processing' WHERE id='%ATT_ID%'; SELECT 'injected'"`) do set INJ=%%a echo injection=%INJ% docker compose --compatibility start worker powershell -NoProfile -Command "for($i=0;$i -lt 60;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT_ID%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'failed'){ $m | ConvertTo-Json -Depth 6 | Set-Content '%TMP%\a.json'; exit 0 }; Start-Sleep -Seconds 1 }; exit 1" if errorlevel 1 ( echo FAIL: expired lease on attempt 5 did not become failed for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT status || ':' || attempt FROM jobs WHERE attachment_id='%ATT_ID%'"`) do echo job state = %%a exit /b 1 ) for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\a.json' -Raw | ConvertFrom-Json).error_code"') do set ECODE=%%a echo PASS: case A failed with error_code=%ECODE% for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT status || ':' || attempt FROM jobs WHERE attachment_id='%ATT_ID%'"`) do set JSTATE=%%a echo job state after = %JSTATE% if not "%JSTATE%"=="failed:5" ( echo FAIL: expected failed:5, got %JSTATE% & exit /b 1 ) echo PASS: job row is failed:5 (no stuck processing:5) for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT count(*) FROM job_failures WHERE attachment_id='%ATT_ID%'"`) do set FC=%%a if "%FC%"=="0" ( echo FAIL: no job_failures row for case A & exit /b 1 ) echo PASS: job_failures has %FC% row(s) echo === CASE B: expired lease on attempt 2 must be requeued and reach ready === curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample2.png" ^ -H "Authorization: Bearer %TOKEN%" -H "Content-Type: image/png" ^ --data-binary "@%FIXTURE%" -o "%TMP%\att2.json" -w "%%{http_code}" > "%TMP%\att2.code" call :check_code att2.code 202 "upload 2" if errorlevel 1 exit /b 1 for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att2.json' -Raw | ConvertFrom-Json).id"') do set ATT2=%%a for /f "usebackq delims=" %%a in (`docker compose exec -T postgres psql -U messenger -d messenger -tAc "UPDATE jobs SET status='processing', attempt=2, lease_id='injected', lease_expires_at=now()-interval '2 minutes' WHERE attachment_id='%ATT2%'; UPDATE attachments SET status='processing' WHERE id='%ATT2%'; SELECT 'injected'"`) do set INJ2=%%a echo injection=%INJ2% powershell -NoProfile -Command "for($i=0;$i -lt 90;$i++){ $m=Invoke-RestMethod -Uri '%BASE%/api/v1/attachments/%ATT2%' -Headers @{Authorization='Bearer %TOKEN%'}; if($m.status -eq 'ready'){ exit 0 }; if($m.status -eq 'failed'){ exit 2 }; Start-Sleep -Seconds 1 }; exit 1" if errorlevel 1 ( echo FAIL: case B did not reach ready & exit /b 1 ) echo PASS: case B requeued after lease expiry and reached ready echo === No conn busy in worker logs === docker compose logs worker --tail 300 > "%TMP%\worker.log" 2>&1 findstr /i /c:"conn busy" "%TMP%\worker.log" >nul if not errorlevel 1 ( echo FAIL: conn busy still present in worker logs & exit /b 1 ) echo PASS: no conn busy in worker logs echo. echo LEASE-EXPIRY CHECKS PASSED exit /b 0 :guid for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a exit /b 0 :register curl.exe -s -X POST "%BASE%/api/v1/users" ^ -H "Content-Type: application/json" ^ -d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^ -o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code" set /p RC=<"%TMP%\%~3.code" if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 ) for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a" exit /b 0 :login curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^ -u "%~1:%PASS%" ^ -o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code" set /p RC=<"%TMP%\%~2.code" if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 ) for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a" exit /b 0 :check_code set /p RC=<"%TMP%\%~1" if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 ) exit /b 0