GuzzleHttp concurrency and cookies



为了测试我们的PHP后端,我们使用PHPUnit(v7.3.5(GuzzleHttp(v6.3.3(扩展进行接口测试。

我对所有这些东西都是新手,玩得有点圆。我想发送并发请求,但我也必须使用 cookie 功能。

并发效果很好,我对 cookie 也很成功。但是,如果我将两者结合起来,则并发性就会丢失。

到目前为止我的代码:

// create session 
$jar = new GuzzleHttpCookieCookieJar;
// create client
$client = new Client([
'base_uri' => 'http://localhost',
'cookies' => $jar,   
]);
// login
$client->get("index.php", [
'form_params' => [
'usr' => 'myUserName',
'pwd' => '#myPass*'
],
]);
// fill up request array
$requests = new array(
new Request('GET', 'myPage1'),
new Request('GET', 'myPage2'),
new Request('GET', 'myPage3'),
new Request('GET', 'myPage4'),
new Request('GET', 'myPage5'),
new Request('GET', 'myPage6'),
...
new Request('GET', 'myPage100'),
);
// create pool
$pool = new Pool($client, $requests, [
'concurrency' => 5,
'fulfilled' => function ($response, $index) {...},
'rejected' => function ($reason, $index) {...}
]);
// wait until all request are sent
$promise = $pool->promise();
$promise->wait();

如果我注释掉// 'cookies' => $jar,并发效果很好。

是不可能同时实现两者还是我错过了什么?

事实证明,问题不在于测试本身。
我在服务器上遇到了session_start((锁。
当然,没有会话,就没有锁...解释一切

您可能需要在单独的过程中运行测试,并在测试函数的注释中添加指令:

/**
* @runInSeparateProcess
*/
public function testYourTestFunction()
{
// Test code here
}

最新更新