ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
CallbackDurationTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
2 
18 namespace HTTP\Services;
19 
22 
26 class CallbackDurationTest extends TestCase
27 {
28 
29 // // there isn't a good way to test the shutdown-function with PHPUnit (yet?).
30 // public function testCallbackStretchingWithExit() : void
31 // {
32 // $callback = $this->getTestCallbackWithLength(1, true);
33 // $duration = new CallbackDuration(1);
34 //
35 // $this->expectException(\LogicException::class);
36 // $this->expectExceptionMessage("Callback could not be stretched because it halted the programm.");
37 // $duration->stretch($callback);
38 // }
39 
41  {
42  $callback = $this->getTestCallbackWithLength(2);
43  $duration = new CallbackDuration(1);
44 
45  $this->expectException(\RuntimeException::class);
46  $this->expectExceptionMessage("Execution of callback exceeded the given duration and could not be stretched.");
47  $duration->stretch($callback);
48  }
49 
50  public function testCallbackStretching() : void
51  {
52  $callback = $this->getTestCallbackWithLength(1);
53  $duration = new CallbackDuration(3);
54 
55  $start_time = microtime(true);
56  $duration->stretch($callback);
57  $end_time = microtime(true);
58 
59  $elapsed_time = ($end_time - $start_time); // in microseconds (us)
60  $expected_duration_in_us = (0.002);
61 
62  $this->assertGreaterThanOrEqual($expected_duration_in_us, ($end_time - $start_time));
63  }
64 
65  protected function getTestCallbackWithLength(int $duration_in_ms, bool $should_halt = false) : callable
66  {
67  return static function () use ($duration_in_ms, $should_halt) {
68  usleep(1_000 * $duration_in_ms);
69  if ($should_halt) {
70  exit;
71  }
72  };
73  }
74 }
exit
Definition: login.php:28
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...