GuzzleHttp并发和cookie

时间:2018-09-27 08:56:01

标签: php phpunit guzzle

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

我对所有这些东西都是陌生的,并且玩了一些游戏。我想发送并发请求,但是我也必须使用cookie功能。

并发工作完美,我也成功使用了cookie。但是如果我将两者结合起来,并发性就会丢失。

到目前为止我的代码:

// create session 
$jar = new \GuzzleHttp\Cookie\CookieJar;

// 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()锁。
  当然,没有会话,就没有锁...可以解释一切

1 个答案:

答案 0 :(得分:0)

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

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