Skip to content

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:

  1. Your normal suite, run with PAM’s PHP.
  2. A persistent smoke test that starts real PAM workers, replays requests, and fails on leaked state or memory growth.
Terminal window
pam artisan test

or let PAM pick PHPUnit or Pest and print a machine-readable summary:

Terminal window
pam test
{"tool":"phpunit","result":"passed","tests":2,"passed":2,"assertions":2,"duration_ms":169}

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).
tests/Persistent/soak.sh
#!/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; done
BEFORE=$(rss)
for _ in $(seq 1 "$REQUESTS"); do curl -sf "$URL/api/ping" > /dev/null; done
AFTER=$(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 16
test "$VIOLATIONS" -eq 0
Terminal window
chmod +x tests/Persistent/soak.sh
REQUESTS=1000 tests/Persistent/soak.sh
RSS before=91828224 after=91934720 growth=0MiB
State violations: 0

To make it meaningful for your application:

  • replace /api/ping with 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-laravel and resolve the metrics registry at boot (see Observability) so violations are counted; and
  • raise REQUESTS in nightly CI.

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:

tests/Feature/NoStaticLeakTest.php
<?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();
}
}

pam:check-production exits 1 when any check fails, so it can gate a deploy pipeline:

Terminal window
APP_ENV=production APP_DEBUG=false pam artisan config:cache
pam artisan pam:check-production --json

See the Production Checklist.