-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6e9898
commit 4a22069
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace tests; | ||
|
||
use think\middleware\throttle\CounterFixed; | ||
use think\middleware\throttle\CounterSlider; | ||
use think\middleware\throttle\LeakyBucket; | ||
use think\middleware\throttle\TokenBucket; | ||
|
||
/** | ||
* 不同节流驱动单元测试 | ||
*/ | ||
class ThrottleDriverTest extends Base | ||
{ | ||
function driver_run(string $derive_name) { | ||
$config = $this->get_default_throttle_config(); | ||
$config['driver_name'] = $derive_name; | ||
$config['visit_rate'] = '60/m'; | ||
$this->set_throttle_config($config); | ||
$allowCount = 0; | ||
for ($i = 0; $i < 200; $i++) { | ||
$request = $this->create_request('/'); | ||
if ($this->visit_with_http_code($request)) { | ||
$allowCount++; | ||
} | ||
} | ||
return $allowCount; | ||
} | ||
|
||
function test_counter_fixed() { | ||
$this->assertEquals(60, $this->driver_run(CounterFixed::class)); | ||
} | ||
|
||
function test_counter_slider() { | ||
$this->assertEquals(60, $this->driver_run(CounterSlider::class)); | ||
} | ||
|
||
function test_leaky_bucket() { | ||
// 漏桶算法,速率 1/s | ||
$this->assertEquals(1, $this->driver_run(LeakyBucket::class)); | ||
} | ||
function test_token_bucket() { | ||
$this->assertEquals(60, $this->driver_run(TokenBucket::class)); | ||
} | ||
} |