-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_streaming.php
More file actions
158 lines (126 loc) · 4.62 KB
/
test_streaming.php
File metadata and controls
158 lines (126 loc) · 4.62 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/vendor/autoload.php';
use ModelsLab\ModelQ\ModelQ;
echo "=== ModelQ Streaming Test ===\n\n";
// Clean Redis
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->flushDb();
echo "Redis DB flushed.\n\n";
$isWorker = isset($argv[1]) && $argv[1] === '--worker';
if ($isWorker) {
// ============================================
// WORKER MODE
// ============================================
echo "Starting as WORKER...\n";
$modelq = new ModelQ(host: 'localhost', port: 6379);
// Register streaming task
$modelq->task('stream_words', function (array $data): \Generator {
$sentence = $data['sentence'] ?? 'Hello World';
$words = explode(' ', $sentence);
foreach ($words as $word) {
usleep(100000); // 100ms delay
yield $word;
}
}, ['stream' => true]);
$modelq->task('stream_numbers', function (array $data): \Generator {
$count = $data['count'] ?? 5;
for ($i = 1; $i <= $count; $i++) {
usleep(50000); // 50ms delay
yield ['number' => $i, 'square' => $i * $i];
}
}, ['stream' => true]);
$modelq->startWorkers(1);
} else {
// ============================================
// TEST RUNNER MODE
// ============================================
echo "Starting as TEST RUNNER...\n";
// Start worker in background
$workerCmd = "php " . escapeshellarg(__FILE__) . " --worker > /tmp/modelq_stream_worker.log 2>&1 &";
exec($workerCmd);
echo "Worker started in background.\n";
sleep(2);
$passed = 0;
$failed = 0;
function test(string $name, callable $test): void
{
global $passed, $failed;
echo "Testing: {$name}... ";
try {
$result = $test();
if ($result === true) {
echo "PASSED\n";
$passed++;
} else {
echo "FAILED - " . (is_string($result) ? $result : "returned false") . "\n";
$failed++;
}
} catch (Throwable $e) {
echo "FAILED - Exception: {$e->getMessage()}\n";
$failed++;
}
}
$modelq = new ModelQ(host: 'localhost', port: 6379);
$modelq->task('stream_words', fn($data) => null, ['stream' => true]);
$modelq->task('stream_numbers', fn($data) => null, ['stream' => true]);
// ============================================
// Test 1: Stream words
// ============================================
test("Stream words", function () use ($modelq) {
$task = $modelq->enqueue('stream_words', ['sentence' => 'The quick brown fox']);
$words = [];
foreach ($task->getStream($modelq->getRedisClient()) as $word) {
$words[] = $word;
echo "."; // Progress indicator
}
echo " ";
$expected = ['The', 'quick', 'brown', 'fox'];
return $words === $expected;
});
// ============================================
// Test 2: Stream numbers with objects
// ============================================
test("Stream numbers", function () use ($modelq) {
$task = $modelq->enqueue('stream_numbers', ['count' => 3]);
$numbers = [];
foreach ($task->getStream($modelq->getRedisClient()) as $item) {
$numbers[] = $item;
echo ".";
}
echo " ";
$expected = [
['number' => 1, 'square' => 1],
['number' => 2, 'square' => 4],
['number' => 3, 'square' => 9],
];
return $numbers === $expected;
});
// ============================================
// Test 3: Stream with combined result
// ============================================
test("Stream combined result", function () use ($modelq) {
$task = $modelq->enqueue('stream_words', ['sentence' => 'Hello World']);
$words = [];
foreach ($task->getStream($modelq->getRedisClient()) as $word) {
$words[] = $word;
}
// Check task status after stream completion
return $task->status === 'completed' && count($words) === 2;
});
// Cleanup
echo "\nStopping worker...\n";
exec("pkill -f 'php.*test_streaming.php.*--worker' 2>/dev/null");
// Summary
echo "\n=== Test Results ===\n";
echo "Passed: {$passed}\n";
echo "Failed: {$failed}\n";
echo "Total: " . ($passed + $failed) . "\n";
if ($failed > 0) {
echo "\n=== Worker Log ===\n";
echo file_get_contents('/tmp/modelq_stream_worker.log') ?: "(empty)";
exit(1);
}
echo "\nAll streaming tests passed!\n";
}