-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSwooleTest.php
More file actions
356 lines (295 loc) · 13.4 KB
/
SwooleTest.php
File metadata and controls
356 lines (295 loc) · 13.4 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
namespace Utopia\Tests\Adapter;
use Utopia\Pools\Adapter\Swoole;
use Utopia\Tests\Base;
use Swoole\Coroutine;
use Utopia\Pools\Pool;
use Utopia\Pools\Connection;
class SwooleTest extends Base
{
protected function getAdapter(): Swoole
{
return new Swoole();
}
protected function execute(callable $callback): mixed
{
$result = null;
$exception = null;
/** @phpstan-ignore-next-line */
Coroutine\run(function () use ($callback, &$result, &$exception): void {
try {
$result = $callback();
} catch (\Throwable $e) {
$exception = $e;
}
});
if ($exception !== null) {
throw $exception;
}
return $result;
}
public function testSwooleCoroutineRaceCondition(): void
{
$errors = [];
$successCount = 0;
/** @phpstan-ignore-next-line */
\Swoole\Coroutine\run(function () use (&$errors, &$successCount) {
// Create a pool with 5 connections inside coroutine context
$connectionCounter = 0;
$pool = new Pool(new Swoole(), 'swoole-test', 5, function () use (&$connectionCounter) {
$connectionCounter++;
return "connection-{$connectionCounter}";
});
// Set retry attempts to allow waiting for connections to be released
$pool->setRetryAttempts(3);
$pool->setRetrySleep(0);
// Spawn 10 coroutines trying to get connections from a pool of 5
// First 5 should get connections immediately
// Next 5 should wait and reuse connections after they're returned
$channels = [];
for ($i = 0; $i < 10; $i++) {
$channels[$i] = new \Swoole\Coroutine\Channel(1);
}
for ($i = 0; $i < 10; $i++) {
\Swoole\Coroutine::create(function () use ($pool, $i, &$errors, &$successCount, $channels) {
try {
// Each coroutine tries to get a connection
$connection = $pool->pop();
// Verify we got a valid connection
if (!$connection instanceof Connection) {
$errors[] = "Coroutine {$i}: Did not receive a valid Connection object";
$channels[$i]->push(false);
return;
}
if (empty($connection->getID())) {
$errors[] = "Coroutine {$i}: Connection has no ID";
$channels[$i]->push(false);
return;
}
// Simulate some work
\Swoole\Coroutine::sleep(0.01);
// Return connection to pool
$pool->reclaim($connection);
$successCount++;
$channels[$i]->push(true);
} catch (\Exception $e) {
$errors[] = "Coroutine {$i}: " . $e->getMessage();
$channels[$i]->push(false);
}
});
}
// Wait for all coroutines to complete
foreach ($channels as $channel) {
$channel->pop();
}
// Assertions inside coroutine context
$this->assertEmpty($errors, 'Errors occurred: ' . implode(', ', $errors));
$this->assertSame(10, $successCount, 'All 10 coroutines should successfully complete');
// Pool should be full again after all connections are reclaimed
$this->assertSame(5, $pool->count(), 'Pool should have all 5 connections back');
// Should only create exactly pool size connections (no race conditions with new implementation)
$this->assertSame(5, $connectionCounter, 'Should create exactly 5 connections (pool size)');
});
}
public function testSwooleCoroutineHighConcurrency(): void
{
if (!\extension_loaded('swoole')) {
$this->markTestSkipped('Swoole extension is not loaded');
}
$totalRequests = 20;
$successCount = 0;
$errorCount = 0;
/** @phpstan-ignore-next-line */
\Swoole\Coroutine\run(function () use ($totalRequests, &$successCount, &$errorCount) {
// Create a pool with 3 connections inside coroutine context
$connectionCounter = 0;
$pool = new Pool(new Swoole(), 'swoole-concurrent', 3, function () use (&$connectionCounter) {
$connectionCounter++;
return "connection-{$connectionCounter}";
});
$pool->setRetryAttempts(3);
$pool->setRetrySleep(0);
$channels = [];
for ($i = 0; $i < $totalRequests; $i++) {
$channels[$i] = new \Swoole\Coroutine\Channel(1);
}
for ($i = 0; $i < $totalRequests; $i++) {
\Swoole\Coroutine::create(function () use ($pool, $i, &$successCount, &$errorCount, $channels) {
try {
$pool->use(function ($resource) use ($i) {
// Simulate work
\Swoole\Coroutine::sleep(0.01);
return "processed-{$i}";
});
$successCount++;
$channels[$i]->push(true);
} catch (\Exception $e) {
$errorCount++;
$channels[$i]->push(false);
}
});
}
// Wait for all coroutines to complete
foreach ($channels as $channel) {
$channel->pop();
}
// All requests should succeed with proper retry logic
$this->assertSame($totalRequests, $successCount, "All {$totalRequests} requests should succeed");
$this->assertSame(0, $errorCount, 'No errors should occur with proper concurrency handling');
// Pool should be full again
$this->assertSame(3, $pool->count(), 'Pool should have all 3 connections back');
// Should only create 3 connections (pool size)
$this->assertSame(3, $connectionCounter, 'Should only create 3 connections (pool size)');
});
}
public function testSwooleCoroutineConnectionUniqueness(): void
{
if (!\extension_loaded('swoole')) {
$this->markTestSkipped('Swoole extension is not loaded');
}
$seenResources = [];
$duplicateResources = [];
/** @phpstan-ignore-next-line */
\Swoole\Coroutine\run(function () use (&$seenResources, &$duplicateResources) {
// Create a pool with 5 connections inside coroutine context
$connectionCounter = 0;
$pool = new Pool(new Swoole(), 'swoole-uniqueness', 5, function () use (&$connectionCounter) {
$connectionCounter++;
return "connection-{$connectionCounter}";
});
$pool->setRetryAttempts(1);
$pool->setRetrySleep(0);
$channels = [];
for ($i = 0; $i < 5; $i++) {
$channels[$i] = new \Swoole\Coroutine\Channel(1);
}
// Get all 5 connections simultaneously
for ($i = 0; $i < 5; $i++) {
\Swoole\Coroutine::create(function () use ($pool, $i, &$seenResources, &$duplicateResources, $channels) {
try {
$connection = $pool->pop();
$resource = $connection->getResource();
// Check if we've seen this resource before (indicates race condition)
if (isset($seenResources[$resource])) {
$duplicateResources[] = $resource;
} else {
$seenResources[$resource] = $connection;
}
// Hold the connection briefly
\Swoole\Coroutine::sleep(0.01);
$channels[$i]->push(true);
} catch (\Exception $e) {
$channels[$i]->push(false);
}
});
}
// Wait for all coroutines to complete
foreach ($channels as $channel) {
$channel->pop();
}
// Assertions inside coroutine context
$this->assertEmpty($duplicateResources, 'Duplicate resources detected: ' . implode(', ', $duplicateResources));
$this->assertCount(5, $seenResources, 'Should have exactly 5 unique connections');
// Verify each connection has a unique resource
$resources = array_keys($seenResources);
$this->assertCount(5, array_unique($resources), 'All connection resources should be unique');
});
}
public function testSwooleCoroutineIdleConnectionReuse(): void
{
if (!\extension_loaded('swoole')) {
$this->markTestSkipped('Swoole extension is not loaded');
}
$connectionIds = [];
$connectionCounter = 0;
/** @phpstan-ignore-next-line */
\Swoole\Coroutine\run(function () use (&$connectionIds, &$connectionCounter) {
// Create a pool with 3 connections inside coroutine context
$pool = new Pool(new Swoole(), 'swoole-reuse', 3, function () use (&$connectionCounter) {
$connectionCounter++;
return "connection-{$connectionCounter}";
});
$pool->setRetryAttempts(1);
$pool->setRetrySleep(0);
// First wave: Create 3 connections
$firstWave = [];
for ($i = 0; $i < 3; $i++) {
$conn = $pool->pop();
$firstWave[] = $conn;
$connectionIds['first'][] = $conn->getID();
}
// Return all connections
foreach ($firstWave as $conn) {
$pool->reclaim($conn);
}
// Second wave: Should reuse the same 3 connections
$secondWave = [];
for ($i = 0; $i < 3; $i++) {
$conn = $pool->pop();
$secondWave[] = $conn;
$connectionIds['second'][] = $conn->getID();
}
// Return all connections
foreach ($secondWave as $conn) {
$pool->reclaim($conn);
}
// Assertions inside coroutine context
$this->assertSame(3, $connectionCounter, 'Should only create 3 connections total');
$this->assertCount(3, $connectionIds['first'], 'First wave should have 3 connections');
$this->assertCount(3, $connectionIds['second'], 'Second wave should have 3 connections');
// Second wave should reuse connections from first wave
sort($connectionIds['first']);
sort($connectionIds['second']);
$this->assertSame($connectionIds['first'], $connectionIds['second'], 'Second wave should reuse same connection IDs');
});
}
public function testSwooleCoroutineStressTest(): void
{
if (!\extension_loaded('swoole')) {
$this->markTestSkipped('Swoole extension is not loaded');
}
$totalRequests = 100;
$successCount = 0;
$errorCount = 0;
$connectionCounter = 0;
/** @phpstan-ignore-next-line */
\Swoole\Coroutine\run(function () use ($totalRequests, &$successCount, &$errorCount, &$connectionCounter) {
// Create a pool with 10 connections inside coroutine context
$pool = new Pool(new Swoole(), 'swoole-stress', 10, function () use (&$connectionCounter) {
$connectionCounter++;
return "connection-{$connectionCounter}";
});
$pool->setRetryAttempts(10);
$pool->setRetrySleep(0);
$channels = [];
for ($i = 0; $i < $totalRequests; $i++) {
$channels[$i] = new \Swoole\Coroutine\Channel(1);
}
for ($i = 0; $i < $totalRequests; $i++) {
\Swoole\Coroutine::create(function () use ($pool, $i, &$successCount, &$errorCount, $channels) {
try {
$pool->use(function ($resource) {
// Simulate variable work duration
\Swoole\Coroutine::sleep(0.001 * rand(1, 5));
return $resource;
});
$successCount++;
$channels[$i]->push(true);
} catch (\Exception $e) {
$errorCount++;
$channels[$i]->push(false);
}
});
}
// Wait for all coroutines to complete
foreach ($channels as $channel) {
$channel->pop();
}
// Assertions inside coroutine context
$this->assertSame($totalRequests, $successCount, "All {$totalRequests} requests should succeed");
$this->assertSame(0, $errorCount, 'No errors should occur');
$this->assertSame(10, $connectionCounter, 'Should create exactly 10 connections (pool size)');
$this->assertSame(10, $pool->count(), 'Pool should have all connections back');
});
}
}