<?phpnamespace Tests;use JumPHP\Async\Task;use JumPHP\Http\HttpError;use JumPHP\Http\HttpResponse;/** * A fake cryo_metrics_serve() exporter: answers /metrics.json and POST /snapshot like ext-cryo does (status, JSON, * Content-Disposition, binary .cryo), at once or later ($defer: the answer comes in a later frame, as with fetch()). */final class FakeExporter{ /** @var list<array{0: string, 1: string, 2: array<string, string>}> */ public array $requests = []; public ?string $token = null; public bool $down = false; public bool $defer = false; public float $memory = 2_000_000; /** @var list<string> the .cryo files the next snapshots return */ public array $snapshots = []; /** @var list<array{0: Task, 1: \Closure}> */ private array $pending = []; private int $polls = 0; private int $taken = 0; /** @param array<string, string> $headers */ public function __invoke(string $method, string $url, array $headers, float $timeout): Task { $this->requests[] = [$method, $url, $headers]; $task = new Task(); $answer = fn () => $this->down ? new HttpError("HTTP $url: connection refused", $url) : $this->answer($method, $url, $headers); if ($this->defer) { $this->pending[] = [$task, $answer]; } else { self::settle($task, $answer()); } return $task; } /** The deferred answers arrive. */ public function flush(): void { [$pending, $this->pending] = [$this->pending, []]; foreach ($pending as [$task, $answer]) { self::settle($task, $answer()); } } private static function settle(Task $task, HttpResponse|HttpError $answer): void { $answer instanceof HttpError ? $task->settle(null, $answer) : $task->settle($answer); } /** @param array<string, string> $headers */ private function answer(string $method, string $url, array $headers): HttpResponse { $json = ['content-type' => 'application/json']; if ($this->token !== null && ($headers['Authorization'] ?? '') !== 'Bearer ' . $this->token) { return new HttpResponse(401, '{"error":"a token is required: ?token= or Authorization: Bearer"}', $json + ['www-authenticate' => 'Bearer realm="cryo"'], $url); } $path = (string)parse_url($url, PHP_URL_PATH); if ($path === '/metrics.json' && $method === 'GET') { $this->memory += 250_000; $this->polls++; return new HttpResponse(200, (string)json_encode([ 'time' => 1_800_000_000 + 2.0 * $this->polls, 'pid' => 4242, 'memory' => $this->memory, 'memory_real' => 4_194_304, 'memory_peak' => $this->memory + 1000, 'memory_peak_real' => 4_194_304, 'memory_rss' => 21_000_000, 'objects_alive' => 1200 + $this->polls, 'gc_runs' => 3, 'gc_collected' => 40, 'gc_roots' => 12, 'tracking' => true, 'class_counting' => true, 'sampling' => false, 'samples' => 0, 'class_counts' => ['App\\Query' => 800 + $this->polls, 'App\\Price' => 400], ]), $json, $url); } if ($path === '/snapshot' && $method === 'POST') { $file = array_shift($this->snapshots); if ($file === null) { return new HttpResponse(504, '{"error":"the PHP process did not reach a safe point within 10.0 s"}', $json, $url); } $this->taken++; return new HttpResponse(200, $file, ['content-type' => 'application/octet-stream', 'content-disposition' => 'attachment; filename="cryo-live-4242-' . $this->taken . '.cryo"', 'x-cryo-snapshot' => (string)$this->taken], $url); } return new HttpResponse(404, '{"error":"not found"}', $json, $url); }}