ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
CallbackDurationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
20 namespace HTTP\Services;
21 
24 
28 class CallbackDurationTest extends TestCase
29 {
30 // // there isn't a good way to test the shutdown-function with PHPUnit (yet?).
31 // public function testCallbackStretchingWithExit() : void
32 // {
33 // $callback = $this->getTestCallbackWithLength(1, true);
34 // $duration = new CallbackDuration(1);
35 //
36 // $this->expectException(\LogicException::class);
37 // $this->expectExceptionMessage("Callback could not be stretched because it halted the programm.");
38 // $duration->stretch($callback);
39 // }
40 
42  {
43  $callback = $this->getTestCallbackWithLength(2);
44  $duration = new CallbackDuration(1);
45 
46  $this->expectException(\RuntimeException::class);
47  $this->expectExceptionMessage("Execution of callback exceeded the given duration and could not be stretched.");
48  $duration->stretch($callback);
49  }
50 
51  public function testCallbackStretching(): void
52  {
53  $callback = $this->getTestCallbackWithLength(1);
54  $duration = new CallbackDuration(3);
55 
56  $start_time = microtime(true);
57  $duration->stretch($callback);
58  $end_time = microtime(true);
59 
60  $elapsed_time = ($end_time - $start_time); // in microseconds (us)
61  $expected_duration_in_us = (0.002);
62 
63  $this->assertGreaterThanOrEqual($expected_duration_in_us, ($end_time - $start_time));
64  }
65 
66  protected function getTestCallbackWithLength(int $duration_in_ms, bool $should_halt = false): callable
67  {
68  return static function () use ($duration_in_ms, $should_halt) {
69  usleep(1_000 * $duration_in_ms);
70  if ($should_halt) {
71  exit;
72  }
73  };
74  }
75 }
exit
Definition: login.php:29
$duration
getTestCallbackWithLength(int $duration_in_ms, bool $should_halt=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...