From 4a220697232a71569617ba4bd815c9ebe9a3d437 Mon Sep 17 00:00:00 2001 From: hongweipeng Date: Sun, 14 Apr 2024 15:37:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=B9=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E8=8A=82=E6=B5=81=E9=A9=B1=E5=8A=A8=E7=9A=84=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/ThrottleDriverTest.php | 46 ++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/ThrottleDriverTest.php diff --git a/tests/ThrottleDriverTest.php b/tests/ThrottleDriverTest.php new file mode 100644 index 0000000..17afe6e --- /dev/null +++ b/tests/ThrottleDriverTest.php @@ -0,0 +1,46 @@ +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)); + } +}