<?php

/**
 * TEST: CGI worker pool.
 */

declare(strict_types=1);
use AmK\Server\CGI\Pool;
use React\EventLoop;
use React\Http;
use Tester\Assert;

require(__DIR__ . '/../../bootstrap.php');

$loop = EventLoop\Factory::create();
$pool = new Pool($loop, 2, null, __DIR__ . '/../../www');

register_shutdown_function([$pool, 'stop']);

$pool->handle(new Http\Io\ServerRequest(
	'GET',
	'/bind.php',
	[],
	null,
	'1.1',
	[
		'SCRIPT_FILENAME' => 'bind.php',
	]
));

$pool->handle(new Http\Io\ServerRequest(
	'GET',
	'/connect.php',
	[],
	null,
	'1.1',
	[
		'SCRIPT_FILENAME' => 'connect.php',
	]
))->then(function(Http\Response $r) use($pool, &$response, &$content): void {

	$response = $r;
	$content = '';

	/** @var $stream Http\Io\HttpBodyStream */
	$stream = $r->getBody();

	$stream->on('data', function(string $data) use(&$content): void {
		$content .= $data;
	});

	$stream->on('end', function() use($pool): void {
		$pool->stop();
	});

});

$loop->run();

Assert::same('Hello!', $content);