phpunit-util icon indicating copy to clipboard operation
phpunit-util copied to clipboard

The docs example test is left waiting indefinitely

Open ghost opened this issue 2 years ago • 3 comments

👋 Hi there,

At ChesslaBlab we're writing functional tests for the PHP Chess Server.

See:

  • https://github.com/chesslablab/chess-server/issues/193

At this moment I'm trying to run the docs example as it is shown in the two images below.

tab_01 Figure 1. php cli/testing.php

tab_02 Figure 2. vendor/bin/phpunit tests/functional/

However, the test is left waiting indefinitely.

<?php

namespace ChessServer\Tests\Functional;

use Amp\ByteStream;
use Amp\PHPUnit\AsyncTestCase;
use Amp\Socket;

class StartCommandTest extends AsyncTestCase
{
    public function test(): void
    {
        $socket = Socket\connect('tcp://127.0.0.1:8080');
        
        $socket->write('/start classical fen');

        $expected = '{"\/start":{"variant":"classical","mode":"fen","fen":"rnbqkbnr\/pppppppp\/8\/8\/8\/8\/PPPPPPPP\/RNBQKBNR w KQkq -"}}';

        $this->assertSame($expected, ByteStream\buffer($socket));
    }
}

Also this is the cli/testing.php script running the TCP socket server on port 8080 shown in Figure 1.

<?php

namespace ChessServer\Cli;

use ChessServer\Socket\TcpSocket;
use Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__.'/../');
$dotenv->load();

$server = new TcpSocket($_ENV['TCP_PORT']);

Any help would be greatly appreciated.

Thank you,

ghost avatar Oct 06 '23 14:10 ghost

Hi @programarivm, you're buffering the socket contents until the socket closes, but the socket is likely not closed in that test, so the test hangs.

kelunik avatar Oct 08 '23 15:10 kelunik

Hi @programarivm!

Amp\ByteStream\buffer() waits until the stream has closed to return the buffered content. Are you expecting $socket to have closed after the response is received? If not, rather than buffer() you'll want to consider reading in loop until the expected payload is received, using Amp\ByteStream\BufferedReader, or if the JSON is line-delimited, you may be able to use Amp\ByteStream\parseLineDelimitedJson().

If the above isn't the issue, I can pull the repos and investigate further.

trowski avatar Oct 08 '23 15:10 trowski

Thank you @kelunik and @trowski for the help.

Not too sure about this one yet. No worries, I suppose for the time being the Selenium automated tests will be kind of equivalent to the so-called functional tests.

jbassagana avatar Oct 09 '23 08:10 jbassagana