-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopBridgeTest.php
More file actions
159 lines (123 loc) · 4.46 KB
/
EventLoopBridgeTest.php
File metadata and controls
159 lines (123 loc) · 4.46 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace ReactParallel\Tests\EventLoop;
use parallel\Channel;
use parallel\Runtime;
use React\EventLoop\Loop;
use ReactParallel\EventLoop\CanceledFuture;
use ReactParallel\EventLoop\EventLoopBridge;
use ReactParallel\EventLoop\KilledRuntime;
use ReactParallel\EventLoop\Metrics;
use WyriHaximus\AsyncTestUtilities\AsyncTestCase;
use WyriHaximus\Metrics\Configuration;
use WyriHaximus\Metrics\InMemory\Registry;
use function bin2hex;
use function dirname;
use function parallel\run;
use function random_bytes;
use function range;
use function React\Async\async;
use function React\Async\await;
use function React\Promise\all;
use function React\Promise\resolve;
use function sleep;
use function usleep;
final class EventLoopBridgeTest extends AsyncTestCase
{
/** @test */
public function read(): void
{
$d = bin2hex(random_bytes(13));
$channels = [Channel::make($d . '_a', Channel::Infinite), Channel::make($d . '_b', Channel::Infinite)];
$eventLoopBridge = (new EventLoopBridge())->withMetrics(Metrics::create(new Registry(Configuration::create())));
$future = run(static function () use ($channels): string {
foreach (range(0, 13) as $i) {
usleep(100);
foreach (range(0, 13) as $j) {
foreach ($channels as $channel) {
$channel->send($i);
}
}
}
sleep(1);
foreach ($channels as $channel) {
$channel->close();
}
sleep(1);
return 'Elmo';
});
$promises = [];
foreach ($channels as $channel) {
$promises[] = async(static function (EventLoopBridge $eventLoopBridge, Channel $channel): array {
$items = [];
foreach ($eventLoopBridge->observe($channel) as $item) {
$items[] = $item;
}
return $items;
})($eventLoopBridge, $channel);
}
$promises[] = resolve($eventLoopBridge->await($future));
$rd = await(all($promises));
$range = [];
foreach (range(0, 13) as $i) {
foreach (range(0, 13) as $j) {
$range[] = $i;
}
}
self::assertSame([$range, $range, 'Elmo'], $rd);
}
/** @test */
public function close(): void
{
$d = bin2hex(random_bytes(13));
$channel = Channel::make($d . '_a', Channel::Infinite);
Loop::addTimer(1, static function () use ($channel): void {
$channel->close();
});
$onNext = false;
$eventLoopBridge = (new EventLoopBridge())->withMetrics(Metrics::create(new Registry(Configuration::create())));
foreach ($eventLoopBridge->observe($channel) as $item) {
$onNext = true;
}
self::assertFalse($onNext, 'onNext should never be called');
}
/** @test */
public function cancel(): void
{
self::expectException(CanceledFuture::class);
$future = run(static fn () => sleep(3));
$eventLoopBridge = (new EventLoopBridge())->withMetrics(Metrics::create(new Registry(Configuration::create())));
Loop::addTimer(1, static function () use ($future): void {
$future->cancel();
});
$eventLoopBridge->await($future);
}
/** @test */
public function kill(): void
{
self::expectException(KilledRuntime::class);
$runtime = new Runtime();
$future = $runtime->run(static function (): string {
sleep(3);
return 'hammer';
});
$eventLoopBridge = (new EventLoopBridge())->withMetrics(Metrics::create(new Registry(Configuration::create())));
Loop::addTimer(1, static function () use ($runtime): void {
$runtime->kill();
});
$eventLoopBridge->await($future);
}
/** @test */
public function futureError(): void
{
self::expectException(CookieMonsterException::class);
self::expectExceptionMessage('Cookie Monster');
$future = run(static function (): void {
require_once dirname(__DIR__) . '/vendor/autoload.php';
sleep(1);
throw new CookieMonsterException('Cookie Monster');
});
$eventLoopBridge = (new EventLoopBridge())->withMetrics(Metrics::create(new Registry(Configuration::create())));
$eventLoopBridge->await($future);
}
}