|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ilzrv\LaravelSlowQueryDetector\Tests; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Log; |
| 6 | + |
| 7 | +class SlowCodeTest extends TestCase |
| 8 | +{ |
| 9 | + public function testBeNotifiedWhenCodeExceededMaxTime() |
| 10 | + { |
| 11 | + $this->request(function () { |
| 12 | + usleep(1000001); // 1 s |
| 13 | + }); |
| 14 | + |
| 15 | + Log::assertLogged('critical', function ($message, $context) { |
| 16 | + return \Str::contains($message, 'SQD'); |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + public function testBeSilentWhenCodeRunsFine() |
| 21 | + { |
| 22 | + $this->request(function () { |
| 23 | + usleep(900000); // 0.9 s |
| 24 | + }); |
| 25 | + |
| 26 | + Log::assertNotLogged('critical'); |
| 27 | + } |
| 28 | + |
| 29 | + public function testBeNotifiedWhenCodeHasTooManyQueries() |
| 30 | + { |
| 31 | + $this->request(function () { |
| 32 | + $connection = $this->getConnectionWithSleepFunction(); |
| 33 | + for ($i = 0; $i < 51; $i++) { |
| 34 | + $connection->select((\DB::raw('select sleep(1)'))); |
| 35 | + } |
| 36 | + }); |
| 37 | + |
| 38 | + Log::assertLogged('critical', function ($message, $context) { |
| 39 | + return \Str::contains($message, 'SQD'); |
| 40 | + }); |
| 41 | + } |
| 42 | + |
| 43 | + public function testBeSilentWhenCodeHasFineCountQueries() |
| 44 | + { |
| 45 | + $this->request(function () { |
| 46 | + $connection = $this->getConnectionWithSleepFunction(); |
| 47 | + for ($i = 0; $i < 50; $i++) { |
| 48 | + $connection->select((\DB::raw('select sleep(1)'))); |
| 49 | + } |
| 50 | + }); |
| 51 | + |
| 52 | + Log::assertNotLogged('critical'); |
| 53 | + } |
| 54 | +} |
0 commit comments