FR
apps/cryo-studio / tests / LiveTest.php · commit 76aa68d
<?phpnamespace Tests;use App\Cryo\LiveClient;use App\Form\MainForm;use App\Gui\MemoryChart;/** The Live screen: a running script's memory every 2 s, and snapshots taken from it that join the analysed ones. */final class LiveTest extends CryoTestCase{    private FakeExporter $server;    private function liveForm(?FakeExporter $server = null): MainForm    {        $form = $this->form();        $this->server = $server ?? new FakeExporter();        $form->live()->setTransport($this->server);        $this->clickOn($form, 'navLive');        return $form;    }    private function connect(MainForm $form, string $url = 'http://127.0.0.1:9600', string $token = ''): void    {        $this->w($form, 'liveUrl')->text = $url;        $this->w($form, 'liveToken')->text = $token;        $this->clickOn($form, 'liveConnect');    }    public function testLiveDrawsTheMemoryOfTheScriptEveryTwoSeconds(): void    {        $form = $this->liveForm();        self::assertSame('live', $form->view());        self::assertFalse((bool)$this->w($form, 'liveSnapshot')->props['enabled'], 'nothing to snapshot before connecting');        $this->connect($form, '127.0.0.1:9600/metrics');        self::assertSame(['GET', 'http://127.0.0.1:9600/metrics.json', []], $this->server->requests[0]);        self::assertStringContainsString('Connected to http://127.0.0.1:9600 · pid 4242', $this->text($form, 'liveStatus'));        self::assertSame('Disconnect', $this->w($form, 'liveConnect')->props['text']);        self::assertTrue((bool)$this->w($form, 'liveSnapshot')->props['enabled']);        $this->game->run(125);                                        // a little more than 2 s: one more poll        self::assertCount(2, $this->server->requests);        $this->game->run(120);        self::assertCount(3, $this->server->requests);        /** @var MemoryChart $chart */        $chart = $this->w($form, 'liveChart');        self::assertCount(3, $chart->points);        self::assertSame([0.0, 2_250_000.0], $chart->points[0]);        self::assertSame([4.0, 2_750_000.0], $chart->points[2], 'seconds from the first point, memory of each answer');        self::assertStringContainsString('Memory 2.6 MB', $this->text($form, 'liveCounters'));        self::assertStringContainsString('1203 live objects', $this->text($form, 'liveCounters'));        self::assertStringContainsString('App\\Query', $this->items($form, 'liveClasses')[0]);        self::assertStringContainsString('803', $this->items($form, 'liveClasses')[0]);        $this->game->shot('live');        $this->clickOn($form, 'liveConnect');                         // Disconnect: no more requests        $this->game->run(300);        self::assertCount(3, $this->server->requests);        self::assertSame('Connect', $this->w($form, 'liveConnect')->props['text']);        self::assertSame([], $chart->points);    }    public function testTwoLiveSnapshotsJoinTheSnapshotsAndGiveADelta(): void    {        $server = new FakeExporter();        $demo = dirname(__DIR__) . '/assets/demo/';        $server->snapshots = [(string)file_get_contents($demo . 'shop-3.cryo'), (string)file_get_contents($demo . 'shop-4.cryo')];        $form = $this->liveForm($server);        $this->connect($form);        $this->clickOn($form, 'liveSnapshot');        self::assertSame(['POST', 'http://127.0.0.1:9600/snapshot', []], $server->requests[1]);        self::assertSame(['Live 4242-1 · 410 objects'], $this->items($form, 'snapshots'), 'the snapshot replaces the demo');        self::assertStringContainsString('cryo-live-4242-1.cryo added to the snapshots (1 in all)', $this->text($form, 'liveStatus'));        $this->game->run(125);        $this->clickOn($form, 'liveSnapshot');        self::assertCount(2, $this->items($form, 'snapshots'));        self::assertSame(1, $this->w($form, 'snapshots')->props['selected'], 'the new snapshot is selected');        self::assertSame(['snapshot 1', 'snapshot 2'], array_column($this->w($form, 'liveChart')->props['marks'], 1));        $this->game->shot('live-snapshots');        $this->clickOn($form, 'navLeaks');                            // two Live snapshots of one process: a delta        self::assertStringContainsString('(2 snapshots)', $this->text($form, 'viewTitle'));        self::assertStringStartsWith('[LEAK] shop_worker.php:17 SqlLogger::log', $this->items($form, 'rows')[0]);        $this->clickOn($form, 'navClasses');        self::assertNotSame([], $this->items($form, 'classes'), 'every screen uses the Live snapshots');        foreach (glob(sys_get_temp_dir() . '/cryo-studio-live/cryo-live-4242-*.cryo') ?: [] as $f) {            @unlink($f);        }    }    public function testATokenIsSentAndAWrongOneIsSaid(): void    {        $server = new FakeExporter();        $server->token = 's3cr3t';        $form = $this->liveForm($server);        $this->connect($form);        self::assertStringContainsString('wants a token', $this->text($form, 'liveStatus'));        self::assertFalse((bool)$this->w($form, 'liveSnapshot')->props['enabled']);        $this->connect($form, 'http://127.0.0.1:9600', 's3cr3t');    // Disconnect, then Connect again        $this->clickOn($form, 'liveConnect');        self::assertSame(['Authorization' => 'Bearer s3cr3t'], end($server->requests)[2]);        self::assertStringContainsString('Connected to', $this->text($form, 'liveStatus'));    }    public function testNoAnswerIsSaidAndTheWindowNeverWaitsForTheScript(): void    {        $server = new FakeExporter();        $server->down = true;        $form = $this->liveForm($server);        $this->connect($form);        self::assertStringContainsString('No answer from http://127.0.0.1:9600', $this->text($form, 'liveStatus'));        self::assertStringContainsString('connection refused', $this->text($form, 'liveStatus'));        $server->down = false;        $server->defer = true;                                        // answers come in a later frame, as with fetch()        $this->game->run(130);        $before = count($server->requests);        $this->game->run(400);                                        // 6 s without an answer: one request, frames keep running        self::assertCount($before, $server->requests, 'never a second request while one is running');        $server->flush();        $this->game->run(1);        self::assertStringContainsString('Connected to', $this->text($form, 'liveStatus'));    }    public function testASnapshotThatTimesOutIsSaid(): void    {        $form = $this->liveForm();        $this->connect($form);        $this->clickOn($form, 'liveSnapshot');        self::assertStringContainsString('did not reach a safe point', $this->text($form, 'liveStatus'));        self::assertCount(5, $this->items($form, 'snapshots'), 'the demo stays');        self::assertTrue((bool)$this->w($form, 'liveSnapshot')->props['enabled'], 'it can be asked again');    }    public function testTheLiveScreenSpeaksFrench(): void    {        $lang = \JumPHP\Kernel\Engine::get()->lang;        $lang->setLang('fr');        try {            $form = $this->liveForm();            self::assertStringContainsString('Live : mémoire d’un script en cours', $this->text($form, 'viewTitle'));            self::assertSame('Non connecté', $this->text($form, 'liveStatus'));            $this->connect($form);            self::assertStringContainsString('Connecté à http://127.0.0.1:9600 · pid 4242', $this->text($form, 'liveStatus'));            self::assertStringContainsString('objets vivants', $this->text($form, 'liveCounters'));            $this->game->shot('fr-live');        } finally {            $lang->setLang('en');        }    }    public function testTheAddressIsNormalized(): void    {        self::assertSame('http://127.0.0.1:9600', LiveClient::normalizeUrl(' 127.0.0.1:9600 '));        self::assertSame('http://host:9600', LiveClient::normalizeUrl('http://host:9600/metrics.json'));        self::assertSame('https://box.example/cryo', LiveClient::normalizeUrl('https://box.example/cryo/snapshot'));        self::assertSame('', LiveClient::normalizeUrl('ftp://host'));        self::assertSame('', LiveClient::normalizeUrl(''));        self::assertSame('cryo-live-1-2.cryo', LiveClient::fileName('attachment; filename="cryo-live-1-2.cryo"'));        self::assertSame('passwd', LiveClient::fileName('attachment; filename="../../etc/passwd"'));        self::assertNull(LiveClient::fileName('inline'));    }}