-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathPest.php
More file actions
94 lines (82 loc) · 2.58 KB
/
Copy pathPest.php
File metadata and controls
94 lines (82 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
use React\EventLoop\TimerInterface;
use React\Promise\Promise;
use React\Promise\PromiseInterface;
use React\Socket\SocketServer;
function getPrivateProperty(object $object, string $propertyName)
{
$reflector = new ReflectionClass($object);
$property = $reflector->getProperty($propertyName);
$property->setAccessible(true);
return $property->getValue($object);
}
function delay($time, ?LoopInterface $loop = null)
{
if ($loop === null) {
$loop = Loop::get();
}
/** @var TimerInterface $timer */
$timer = null;
return new Promise(function ($resolve) use ($loop, $time, &$timer) {
$timer = $loop->addTimer($time, function () use ($resolve) {
$resolve(null);
});
}, function () use (&$timer, $loop) {
$loop->cancelTimer($timer);
$timer = null;
throw new \RuntimeException('Timer cancelled');
});
}
function timeout(PromiseInterface $promise, $time, ?LoopInterface $loop = null)
{
$canceller = null;
if (\method_exists($promise, 'cancel')) {
$canceller = function () use (&$promise) {
$promise->cancel();
$promise = null;
};
}
if ($loop === null) {
$loop = Loop::get();
}
return new Promise(function ($resolve, $reject) use ($loop, $time, $promise) {
$timer = null;
$promise = $promise->then(function ($v) use (&$timer, $loop, $resolve) {
if ($timer) {
$loop->cancelTimer($timer);
}
$timer = false;
$resolve($v);
}, function ($v) use (&$timer, $loop, $reject) {
if ($timer) {
$loop->cancelTimer($timer);
}
$timer = false;
$reject($v);
});
if ($timer === false) {
return;
}
// start timeout timer which will cancel the input promise
$timer = $loop->addTimer($time, function () use ($time, &$promise, $reject) {
$reject(new \RuntimeException('Timed out after ' . $time . ' seconds'));
if (\method_exists($promise, 'cancel')) {
$promise->cancel();
}
$promise = null;
});
}, $canceller);
}
function findFreePort()
{
$server = new SocketServer('127.0.0.1:0');
$address = $server->getAddress();
$port = $address ? parse_url($address, PHP_URL_PORT) : null;
$server->close();
if (!$port) {
throw new \RuntimeException("Could not find a free port for testing.");
}
return (int)$port;
}