-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path16-retry.php
More file actions
78 lines (56 loc) · 2.25 KB
/
Copy path16-retry.php
File metadata and controls
78 lines (56 loc) · 2.25 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
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use FerryAI\Core\PlatformDetector;
use FerryAI\Core\RetryHandler;
use FerryAI\NativeBinaryManager;
echo "=== 16 — RetryHandler, PlatformDetector, NativeBinaryManager ===\n\n";
echo "--- PlatformDetector ---\n\n";
printf("os: %s\n", PlatformDetector::os());
printf("arch: %s\n", PlatformDetector::arch());
printf("libExtension: %s\n", PlatformDetector::libExtension());
printf("platformKey: %s\n\n", PlatformDetector::platformKey());
echo "--- RetryHandler ---\n\n";
$handler = new RetryHandler();
$calls = 0;
$result = $handler->retry(function () use (&$calls): string {
$calls++;
if ($calls < 2) {
throw new \RuntimeException("attempt $calls failed");
}
return 'success on attempt ' . $calls;
}, maxAttempts: 3, delayMs: 10, backoff: 'linear');
printf("retry(linear): %s (after %d calls)\n", $result, $calls);
$calls = 0;
try {
$handler->retry(function () use (&$calls): never {
$calls++;
throw new \RuntimeException('always fails');
}, maxAttempts: 3, delayMs: 10);
} catch (\RuntimeException $e) {
printf("retry(exhaust): %s (after %d calls)\n\n", $e->getMessage(), $calls);
}
echo "--- shouldRetry ---\n\n";
printf("RuntimeException: %s\n", RetryHandler::shouldRetry(new \RuntimeException()) ? 'retry' : 'skip');
printf("ModelLoadException: %s\n", RetryHandler::shouldRetry(
new \FerryAI\Core\Exception\ModelLoadException('/p', 'bad'),
)
? 'retry' : 'skip');
printf("ShapeMismatchException: %s\n", RetryHandler::shouldRetry(
new \FerryAI\Core\Exception\ShapeMismatchException(
new \FerryAI\Core\ValueObjects\Shape([3]),
new \FerryAI\Core\ValueObjects\Shape([4]),
),
)
? 'retry' : 'skip');
printf("ModelNotFoundException: %s\n\n", RetryHandler::shouldRetry(
new \FerryAI\Core\Exception\ModelNotFoundException('/x'),
)
? 'retry' : 'skip');
echo "--- NativeBinaryManager ---\n\n";
$manager = new NativeBinaryManager();
$resolved = $manager->resolve('nonexistent_lib_xyz');
printf("resolve(unknown): %s\n", $resolved ?? 'null');
printf("verify(missing): %s\n\n", $manager->verify('/nonexistent', 'abc') ? 'PASS' : 'FAIL');
echo "=== OK ===\n";