run() public static method

Executes a callable until a timeout is reached or the callable returns true.
public static run ( Callable $callable, integer $timeout ) : mixed
$callable Callable The callable to execute.
$timeout integer The timeout value.
return mixed
Example #1
0
             $start = microtime(true);
             $closure = function () {
                 Code::run(function () {
                     while (true) {
                         sleep(1);
                     }
                 }, 1);
             };
             expect($closure)->toThrow(new TimeoutException('Timeout reached, execution aborted after 1 second(s).'));
             $end = microtime(true);
             expect($end - $start)->toBeGreaterThan(1);
         });
         it("throws all unexpected exceptions", function () {
             $closure = function () {
                 Code::run(function () {
                     throw new Exception("Error Processing Request");
                 }, 1);
             };
             expect($closure)->toThrow(new Exception("Error Processing Request"));
         });
     });
 }
 describe("::spin()", function () {
     it("runs the passed closure", function () {
         $start = microtime(true);
         expect(Code::spin(function () {
             return true;
         }, 1))->toBe(true);
         $end = microtime(true);
         expect($end - $start)->toBeLessThan(1);
     });