-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathWorkerCommandTest.php
More file actions
367 lines (278 loc) · 11 KB
/
WorkerCommandTest.php
File metadata and controls
367 lines (278 loc) · 11 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
357
358
359
360
361
362
363
364
365
366
367
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Tests\Core\Command\TaskProcessing;
use OC\Core\Command\TaskProcessing\WorkerCommand;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IAppConfig;
use OCP\TaskProcessing\Exception\Exception;
use OCP\TaskProcessing\Exception\NotFoundException;
use OCP\TaskProcessing\IManager;
use OCP\TaskProcessing\ISynchronousProvider;
use OCP\TaskProcessing\Task;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Test\TestCase;
class WorkerCommandTest extends TestCase {
private IManager&MockObject $manager;
private LoggerInterface&MockObject $logger;
private IAppConfig&MockObject $appConfig;
private ITimeFactory&MockObject $timeFactory;
private WorkerCommand $command;
protected function setUp(): void {
parent::setUp();
$this->manager = $this->createMock(IManager::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->appConfig = $this->createMock(IAppConfig::class);
$this->timeFactory = $this->createMock(ITimeFactory::class);
$this->timeFactory->method('now')->willReturnCallback(fn () => new \DateTimeImmutable());
$this->command = new WorkerCommand($this->manager, $this->logger, $this->appConfig, $this->timeFactory);
}
/**
* Helper to create a minimal ISynchronousProvider mock.
*/
private function createProvider(string $id, string $taskTypeId): ISynchronousProvider&MockObject {
$provider = $this->createMock(ISynchronousProvider::class);
$provider->method('getId')->willReturn($id);
$provider->method('getTaskTypeId')->willReturn($taskTypeId);
return $provider;
}
/**
* Helper to create a real Task with a given id and optional task type.
*/
private function createTask(int $id, string $type = 'test_task_type'): Task {
$task = new Task($type, [], 'testapp', null);
$task->setId($id);
return $task;
}
public function testOnceExitsAfterNoTask(): void {
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([]);
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testOnceProcessesOneTask(): void {
$taskTypeId = 'test_task_type';
$provider = $this->createProvider('test_provider', $taskTypeId);
$task = $this->createTask(42);
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider]);
$this->manager->expects($this->once())
->method('getPreferredProvider')
->with($taskTypeId)
->willReturn($provider);
$this->manager->expects($this->once())
->method('getNextScheduledTask')
->with([$taskTypeId])
->willReturn($task);
$this->manager->expects($this->once())
->method('processTask')
->with($task, $provider)
->willReturn(true);
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testSkipsNonSynchronousProviders(): void {
// A provider that is NOT an ISynchronousProvider
$nonSyncProvider = $this->createMock(\OCP\TaskProcessing\IProvider::class);
$nonSyncProvider->method('getId')->willReturn('non_sync_provider');
$nonSyncProvider->method('getTaskTypeId')->willReturn('some_type');
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$nonSyncProvider]);
$this->manager->expects($this->never())
->method('getPreferredProvider');
$this->manager->expects($this->never())
->method('getNextScheduledTask');
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testSkipsNonPreferredProviders(): void {
$taskTypeId = 'test_task_type';
$provider = $this->createProvider('provider_a', $taskTypeId);
$preferredProvider = $this->createProvider('provider_b', $taskTypeId);
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider]);
$this->manager->expects($this->once())
->method('getPreferredProvider')
->with($taskTypeId)
->willReturn($preferredProvider);
// provider_a is not preferred (provider_b is), so getNextScheduledTask is never called
$this->manager->expects($this->never())
->method('getNextScheduledTask');
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testContinuesWhenNoTaskFound(): void {
$taskTypeId = 'test_task_type';
$provider = $this->createProvider('test_provider', $taskTypeId);
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider]);
$this->manager->expects($this->once())
->method('getPreferredProvider')
->with($taskTypeId)
->willReturn($provider);
$this->manager->expects($this->once())
->method('getNextScheduledTask')
->with([$taskTypeId])
->willThrowException(new NotFoundException());
$this->manager->expects($this->never())
->method('processTask');
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testLogsErrorAndContinuesOnException(): void {
$taskTypeId = 'test_task_type';
$provider = $this->createProvider('test_provider', $taskTypeId);
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider]);
$this->manager->expects($this->once())
->method('getPreferredProvider')
->with($taskTypeId)
->willReturn($provider);
$exception = new Exception('DB error');
$this->manager->expects($this->once())
->method('getNextScheduledTask')
->with([$taskTypeId])
->willThrowException($exception);
$this->logger->expects($this->once())
->method('error')
->with('Unknown error while retrieving scheduled TaskProcessing tasks', ['exception' => $exception]);
$this->manager->expects($this->never())
->method('processTask');
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testTimeoutExitsLoop(): void {
// Arrange: no providers so each iteration does nothing, but timeout=1 should exit quickly
$this->manager->method('getProviders')->willReturn([]);
$input = new ArrayInput(['--timeout' => '1', '--interval' => '0'], $this->command->getDefinition());
$output = new NullOutput();
$start = time();
$result = $this->command->run($input, $output);
$elapsed = time() - $start;
$this->assertSame(0, $result);
// Should have exited within a few seconds
$this->assertLessThanOrEqual(5, $elapsed);
}
public function testProcessesCorrectProviderForReturnedTaskType(): void {
$taskTypeId1 = 'type_a';
$taskTypeId2 = 'type_b';
$provider1 = $this->createProvider('provider_a', $taskTypeId1);
$provider2 = $this->createProvider('provider_b', $taskTypeId2);
// Task has type_a, so provider1 must be chosen to process it
$task = $this->createTask(7, $taskTypeId1);
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider1, $provider2]);
// Both providers are eligible, so getPreferredProvider is called for each
$this->manager->expects($this->exactly(2))
->method('getPreferredProvider')
->willReturnMap([
[$taskTypeId1, $provider1],
[$taskTypeId2, $provider2],
]);
// All eligible task types are passed in a single query
$this->manager->expects($this->once())
->method('getNextScheduledTask')
->with($this->equalTo([$taskTypeId1, $taskTypeId2]))
->willReturn($task);
$this->manager->expects($this->once())
->method('processTask')
->with($task, $provider1)
->willReturn(true);
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testTaskTypesWhitelistFiltersProviders(): void {
$taskTypeId1 = 'type_a';
$taskTypeId2 = 'type_b';
$provider1 = $this->createProvider('provider_a', $taskTypeId1);
$provider2 = $this->createProvider('provider_b', $taskTypeId2);
$task = $this->createTask(99, $taskTypeId2);
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider1, $provider2]);
// Only type_b is whitelisted, so provider_a (type_a) must be skipped entirely
$this->manager->expects($this->once())
->method('getPreferredProvider')
->with($taskTypeId2)
->willReturn($provider2);
$this->manager->expects($this->once())
->method('getNextScheduledTask')
->with([$taskTypeId2])
->willReturn($task);
$this->manager->expects($this->once())
->method('processTask')
->with($task, $provider2)
->willReturn(true);
$input = new ArrayInput(['--once' => true, '--taskTypes' => [$taskTypeId2]], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testTaskTypesWhitelistWithNoMatchingProviders(): void {
$provider = $this->createProvider('provider_a', 'type_a');
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider]);
// Whitelist does not include type_a so nothing should be processed
$this->manager->expects($this->never())
->method('getPreferredProvider');
$this->manager->expects($this->never())
->method('getNextScheduledTask');
$input = new ArrayInput(['--once' => true, '--taskTypes' => ['type_b']], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
public function testEmptyTaskTypesAllowsAllProviders(): void {
$taskTypeId = 'type_a';
$provider = $this->createProvider('provider_a', $taskTypeId);
$task = $this->createTask(5, $taskTypeId);
$this->manager->expects($this->once())
->method('getProviders')
->willReturn([$provider]);
$this->manager->expects($this->once())
->method('getPreferredProvider')
->with($taskTypeId)
->willReturn($provider);
$this->manager->expects($this->once())
->method('getNextScheduledTask')
->with([$taskTypeId])
->willReturn($task);
$this->manager->expects($this->once())
->method('processTask')
->with($task, $provider)
->willReturn(true);
// No --taskTypes option provided
$input = new ArrayInput(['--once' => true], $this->command->getDefinition());
$output = new NullOutput();
$result = $this->command->run($input, $output);
$this->assertSame(0, $result);
}
}