ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
CallbackDurationTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace HTTP\Services;
22
23use PHPUnit\Framework\TestCase;
25
29class CallbackDurationTest extends TestCase
30{
31 // // there isn't a good way to test the shutdown-function with PHPUnit (yet?).
32 // public function testCallbackStretchingWithExit() : void
33 // {
34 // $callback = $this->getTestCallbackWithLength(1, true);
35 // $duration = new CallbackDuration(1);
36 //
37 // $this->expectException(\LogicException::class);
38 // $this->expectExceptionMessage("Callback could not be stretched because it halted the programm.");
39 // $duration->stretch($callback);
40 // }
41
43 {
44 $callback = $this->getTestCallbackWithLength(2);
46
47 $this->expectException(\RuntimeException::class);
48 $this->expectExceptionMessage("Execution of callback exceeded the given duration and could not be stretched.");
49 $duration->stretch($callback);
50 }
51
52 public function testCallbackStretching(): void
53 {
54 $callback = $this->getTestCallbackWithLength(1);
56
57 $start_time = microtime(true);
58 $duration->stretch($callback);
59 $end_time = microtime(true);
60
61 $elapsed_time = ($end_time - $start_time); // in microseconds (us)
62 $expected_duration_in_us = (0.002);
63
64 $this->assertGreaterThanOrEqual($expected_duration_in_us, ($end_time - $start_time));
65 }
66
67 protected function getTestCallbackWithLength(int $duration_in_ms, bool $should_halt = false): callable
68 {
69 return static function () use ($duration_in_ms, $should_halt): void {
70 usleep(1_000 * $duration_in_ms);
71 if ($should_halt) {
72 exit;
73 }
74 };
75 }
76}
$duration
getTestCallbackWithLength(int $duration_in_ms, bool $should_halt=false)
exit