Skip to content

Commit 54cb3cd

Browse files
authored
添加常驻内存型的单元测试 (#29)
1 parent ef6b0d6 commit 54cb3cd

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

tests/BaseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function get_response(\think\Request $request): \think\Response {
2727
// 设置 throttle 配置
2828
$app->config->set($this->throttle_config, 'throttle');
2929

30-
$response = $app->http->run($request);
30+
$response = $app->http->run($request);
3131
$app->refClear();
3232
return $response;
3333
}

tests/ResidentMemoryTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
4+
namespace tests;
5+
6+
7+
/**
8+
* 常驻内存型的单元测试,当 TP 运行在常驻内存型的时候。
9+
* 一个 App 实例,处理多次请求
10+
* Class ResidentMemoryTest
11+
* @package tests
12+
*/
13+
class ResidentMemoryTest extends BaseTest
14+
{
15+
public function test_resident_memory()
16+
{
17+
$app = new GCApp(static::$ROOT_PATH);
18+
$app->setRuntimePath(static::$RUNTIME_PATH);
19+
$app->middleware->import(include $this->middleware_file, $this->middleware_type);
20+
$app->config->set($this->get_default_throttle_config(), 'throttle');
21+
22+
// 处理多个请求
23+
$allowCount1 = 0;
24+
$allowCount2 = 0;
25+
26+
for ($i = 0; $i < 200; $i++) {
27+
// 受访问频率限制
28+
$request = new \think\Request();
29+
$request->setMethod('GET');
30+
$request->setUrl('/');
31+
$response = $app->http->run($request);
32+
if ($response->getCode() == 200) {
33+
$allowCount1++;
34+
}
35+
36+
// 不受访问频率限制
37+
$request = new \think\Request();
38+
$request->setMethod('POST');
39+
$request->setUrl('/');
40+
41+
$response = $app->http->run($request);
42+
if ($response->getCode() == 200) {
43+
$allowCount2++;
44+
}
45+
}
46+
47+
$app->refClear();
48+
$this->assertEquals(100, $allowCount1);
49+
$this->assertEquals(200, $allowCount2);
50+
}
51+
}

0 commit comments

Comments
 (0)