Testing
Your existing PHPUnit or Pest suite runs unchanged under PAM. It is the best way to check that your code works, but it cannot catch the one class of bug that is specific to persistent workers: state that survives from one request to the next. Laravel’s test helpers boot a fresh application for every test.
So test in two layers:
- Your normal suite, run with PAM’s PHP.
- A persistent smoke test that starts real PAM workers, replays requests, and fails on leaked state or memory growth.
Running Your Test Suite
Section titled “Running Your Test Suite”pam artisan testor let PAM pick PHPUnit or Pest and print a machine-readable summary:
pam test{"tool":"phpunit","result":"passed","tests":2,"passed":2,"assertions":2,"duration_ms":169}Persistent Worker Smoke Test
Section titled “Persistent Worker Smoke Test”This script boots the application with pam start, sends traffic to a single
worker so every request reuses the same process, and fails when:
- the worker’s RSS grows more than 16 MiB after warm-up; or
- the state guard records a violation (it runs in strict mode here).
#!/usr/bin/env bash# Boot the app under PAM, replay requests against one worker, and fail on leaks.set -euo pipefail
PORT="${PORT:-3490}"ADMIN="${ADMIN:-127.0.0.1:3491}"REQUESTS="${REQUESTS:-2000}"URL="http://127.0.0.1:${PORT}"
PAM_PORT="$PORT" PAM_LARAVEL_STATE_GUARD_STRICT=true \ pam start pam.php --workers 1 --admin-address "$ADMIN" > storage/logs/soak.log 2>&1 &PAM_PID=$!trap 'kill -TERM "$PAM_PID" 2>/dev/null; wait "$PAM_PID" 2>/dev/null || true' EXIT
until curl -sf "http://${ADMIN}/ready" > /dev/null; do sleep 0.2; done
rss() { curl -s "http://${ADMIN}/metrics" | awk '/^pam_process_resident_memory_bytes/ {print $2}'; }
for _ in $(seq 1 200); do curl -sf "$URL/api/ping" > /dev/null; doneBEFORE=$(rss)for _ in $(seq 1 "$REQUESTS"); do curl -sf "$URL/api/ping" > /dev/null; doneAFTER=$(rss)
GROWTH_MB=$(( (AFTER - BEFORE) / 1048576 ))echo "RSS before=${BEFORE} after=${AFTER} growth=${GROWTH_MB}MiB"
VIOLATIONS=$(curl -s "$URL/__pam/metrics" | pam -r 'echo count(json_decode(stream_get_contents(STDIN), true)["stateViolations"] ?? []);')echo "State violations: ${VIOLATIONS}"
test "$GROWTH_MB" -le 16test "$VIOLATIONS" -eq 0chmod +x tests/Persistent/soak.shREQUESTS=1000 tests/Persistent/soak.shRSS before=91828224 after=91934720 growth=0MiBState violations: 0To make it meaningful for your application:
- replace
/api/pingwith the routes that matter: an authenticated request, a form submission, a file upload, a request in another locale; - alternate users: log in as Alice, then make an anonymous request and assert it does not see Alice’s data;
- install
pushinbr/pam-laraveland resolve the metrics registry at boot (see Observability) so violations are counted; and - raise
REQUESTSin nightly CI.
Catching Leaks in Feature Tests
Section titled “Catching Leaks in Feature Tests”You can still write a cheap regression test for a specific leak by calling two requests in one test. It runs in one application instance, not a PAM worker, but it catches code that keeps state in statics:
<?php
namespace Tests\Feature;
use App\Models\User;use Illuminate\Foundation\Testing\RefreshDatabase;use Tests\TestCase;
class NoStaticLeakTest extends TestCase{ use RefreshDatabase;
public function test_a_guest_does_not_inherit_the_previous_user(): void { $alice = User::factory()->create(['name' => 'Alice']);
$this->actingAs($alice)->getJson('/api/me')->assertJson(['name' => 'Alice']);
$this->app['auth']->forgetGuards();
$this->getJson('/api/me')->assertUnauthorized(); }}Production Readiness in CI
Section titled “Production Readiness in CI”pam:check-production exits 1 when any check fails, so it can gate a deploy
pipeline:
APP_ENV=production APP_DEBUG=false pam artisan config:cachepam artisan pam:check-production --jsonSee the Production Checklist.