From 72fd84607b7d11aa02be1b3bbdaf43e4a42b731f Mon Sep 17 00:00:00 2001 From: Bradley Schofield Date: Wed, 15 May 2024 13:58:48 +0900 Subject: [PATCH] Improve test for sampler --- tests/e2e/AdapterBase.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/e2e/AdapterBase.php b/tests/e2e/AdapterBase.php index a06d184..966a159 100755 --- a/tests/e2e/AdapterBase.php +++ b/tests/e2e/AdapterBase.php @@ -58,4 +58,35 @@ public function testAdapter(): void $response = $logger->addLog($this->log); $this->assertEquals($this->expected, $response); } + + /** + * @throws \Throwable + */ + public function testSampler(): void + { + if (empty($this->log) || empty($this->adapter)) { + throw new \Exception('Log or adapter not set'); + } + + $logger = new Logger($this->adapter); + $logger->setSample(0.1); + + $results = []; + $zeroCount = 0; + + for ($x = 0; $x <= 100; $x++) { + $result = $logger->addLog($this->log); + $results[] = $result; + if ($result === 0) { + $zeroCount++; + } + } + + $totalCount = count($results); + $zeroPercentage = ($zeroCount / $totalCount) * 100; + + echo "Percentage of 0 returns: " . $zeroPercentage . "%"; + + $this->assertLessThan(10, $zeroPercentage); + } }