-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfiniteTest.php
More file actions
182 lines (149 loc) · 5.83 KB
/
InfiniteTest.php
File metadata and controls
182 lines (149 loc) · 5.83 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
declare(strict_types=1);
namespace ReactParallel\Tests\Pool\Infinite;
use PHPUnit\Framework\Attributes\Test;
use React\EventLoop\Loop;
use ReactParallel\Contracts\PoolInterface;
use ReactParallel\EventLoop\EventLoopBridge;
use ReactParallel\Pool\Infinite\Infinite;
use ReactParallel\Pool\Infinite\Metrics;
use ReactParallel\Tests\AbstractPoolTest;
use WyriHaximus\Metrics\Factory as MetricsFactory;
use WyriHaximus\Metrics\Printer\Prometheus;
use WyriHaximus\PoolInfo\Info;
use WyriHaximus\PoolInfo\PoolInfoInterface;
use WyriHaximus\PoolInfo\PoolInfoTestTrait;
use function sleep;
final class InfiniteTest extends AbstractPoolTest
{
use PoolInfoTestTrait;
#[Test]
public function withAZeroTTLThreadsShouldBeKilledOffImmidetally(): void
{
$registry = MetricsFactory::create();
$pool = new Infinite(new EventLoopBridge(), 0.0)->withMetrics(Metrics::create($registry));
Loop::addTimer(1, static function () use ($pool, $registry): void {
self::assertSame([
Info::TOTAL => 1,
Info::BUSY => 1,
Info::CALLS => 0,
Info::IDLE => 0,
Info::SIZE => 1,
], [...$pool->info()]);
$metrics = $registry->print(new Prometheus());
self::assertStringContainsString('react_parallel_pool_infinite_threads{state="idle"} 0', $metrics);
self::assertStringContainsString('react_parallel_pool_infinite_threads{state="busy"} 1', $metrics);
});
$metrics = $registry->print(new Prometheus());
self::assertSame("\n\n# EOF\n", $metrics);
self::assertSame([
Info::TOTAL => 0,
Info::BUSY => 0,
Info::CALLS => 0,
Info::IDLE => 0,
Info::SIZE => 0,
], [...$pool->info()]);
$asteriks = $pool->run(static function (): int {
sleep(3);
return 42;
});
$metrics = $registry->print(new Prometheus());
self::assertStringContainsString('react_parallel_pool_infinite_threads{state="idle"} 0', $metrics);
self::assertStringContainsString('react_parallel_pool_infinite_threads{state="busy"} 0', $metrics);
self::assertStringContainsString('react_parallel_pool_infinite_execution_time{quantile="0.1"} 3.0', $metrics);
self::assertStringContainsString('react_parallel_pool_infinite_execution_time{quantile="0.5"} 3.0', $metrics);
self::assertStringContainsString('react_parallel_pool_infinite_execution_time{quantile="0.9"} 3.0', $metrics);
self::assertStringContainsString('react_parallel_pool_infinite_execution_time{quantile="0.99"} 3.0', $metrics);
self::assertSame([
Info::TOTAL => 0,
Info::BUSY => 0,
Info::CALLS => 0,
Info::IDLE => 0,
Info::SIZE => 0,
], [...$pool->info()]);
$pool->kill();
/** @phpstan-ignore staticMethod.alreadyNarrowedType */
self::assertSame(42, $asteriks);
}
#[Test]
public function withAnAlmostZeroTTLThreadsShouldNotBeKilledOffImmidetally(): void
{
$pool = new Infinite(new EventLoopBridge(), 5)->withMetrics(Metrics::create(MetricsFactory::create()));
Loop::addTimer(1, static function () use ($pool): void {
self::assertSame([
Info::TOTAL => 1,
Info::BUSY => 1,
Info::CALLS => 0,
Info::IDLE => 0,
Info::SIZE => 1,
], [...$pool->info()]);
});
self::assertSame([
Info::TOTAL => 0,
Info::BUSY => 0,
Info::CALLS => 0,
Info::IDLE => 0,
Info::SIZE => 0,
], [...$pool->info()]);
$asteriks = $pool->run(static function (): int {
sleep(3);
return 42;
});
self::assertSame([
Info::TOTAL => 1,
Info::BUSY => 0,
Info::CALLS => 0,
Info::IDLE => 1,
Info::SIZE => 1,
], [...$pool->info()]);
Loop::addTimer(0.5, static function () use ($pool): void {
self::assertSame([
Info::TOTAL => 1,
Info::BUSY => 1,
Info::CALLS => 0,
Info::IDLE => 0,
Info::SIZE => 1,
], [...$pool->info()]);
});
$asteriks = $pool->run(static function () use ($asteriks): int {
sleep(1);
return $asteriks;
});
self::assertSame([
Info::TOTAL => 1,
Info::BUSY => 0,
Info::CALLS => 0,
Info::IDLE => 1,
Info::SIZE => 1,
], [...$pool->info()]);
$pool->kill();
/** @phpstan-ignore staticMethod.alreadyNarrowedType */
self::assertSame(42, $asteriks);
}
protected function poolFactory(): PoolInfoInterface
{
return new Infinite(new EventLoopBridge(), 5)->withMetrics(Metrics::create(MetricsFactory::create()));
}
protected function createPool(): PoolInterface
{
return new Infinite(new EventLoopBridge(), 5)->withMetrics(Metrics::create(MetricsFactory::create()));
}
#[Test]
public function aquireLock(): void
{
$pool = new Infinite(new EventLoopBridge(), 5)->withMetrics(Metrics::create(MetricsFactory::create()));
$group = $pool->acquireGroup();
self::assertFalse($pool->close());
self::assertFalse($pool->kill());
$pool->releaseGroup($group);
self::assertTrue($pool->close());
self::assertTrue($pool->kill());
}
#[Test]
public function withMetrics(): void
{
$pool = new Infinite(new EventLoopBridge(), 5);
$poolWithmetrics = $pool->withMetrics(Metrics::create(MetricsFactory::create()));
self::assertNotSame($pool, $poolWithmetrics);
}
}