-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathScheduledTaskDuplicationTest.php
More file actions
418 lines (361 loc) · 14.5 KB
/
Copy pathScheduledTaskDuplicationTest.php
File metadata and controls
418 lines (361 loc) · 14.5 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
namespace Tests\Feature;
use Carbon\Carbon;
use DateTimeZone;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
use ProcessMaker\Facades\WorkflowManager;
use ProcessMaker\Managers\TaskSchedulerManager;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ScheduledTask;
use Tests\TestCase;
class ScheduledTaskDuplicationTest extends TestCase
{
/**
* Test that atomic claim prevents duplicate task execution.
* Simulates two processes trying to claim the same task.
*/
public function testAtomicClaimPreventsDuplicateExecution()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
// Create a scheduled task that should be executed
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_1',
]),
]);
$now = Carbon::now()->format('Y-m-d H:i:s');
// First claim should succeed
$claimed1 = DB::table('scheduled_tasks')
->where('id', $task->id)
->whereNull('claimed_by')
->update([
'claimed_by' => Str::uuid()->toString(),
'claimed_at' => $now,
]);
// Second claim should fail (task already claimed)
$claimed2 = DB::table('scheduled_tasks')
->where('id', $task->id)
->whereNull('claimed_by')
->update([
'claimed_by' => Str::uuid()->toString(),
'claimed_at' => $now,
]);
$this->assertEquals(1, $claimed1, 'First claim should succeed');
$this->assertEquals(0, $claimed2, 'Second claim should fail');
}
/**
* Test that stale claims are released after timeout.
*/
public function testStaleClaimsAreReleased()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
// Create a task with a stale claim (claimed 10 minutes ago)
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(15)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_1',
]),
'claimed_by' => 'stale-claim-id',
'claimed_at' => Carbon::now()->subMinutes(10),
]);
$manager = new TaskSchedulerManager();
// Use reflection to call the private method
$reflection = new \ReflectionClass($manager);
$method = $reflection->getMethod('releaseStaleClaimedTasks');
$method->setAccessible(true);
$method->invoke($manager);
// Verify the claim was released
$task->refresh();
$this->assertNull($task->claimed_by);
$this->assertNull($task->claimed_at);
}
/**
* Test that recent claims are NOT released.
*/
public function testRecentClaimsAreNotReleased()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
$claimId = Str::uuid()->toString();
// Create a task with a recent claim (1 minute ago)
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_1',
]),
'claimed_by' => $claimId,
'claimed_at' => Carbon::now()->subMinute(),
]);
$manager = new TaskSchedulerManager();
$reflection = new \ReflectionClass($manager);
$method = $reflection->getMethod('releaseStaleClaimedTasks');
$method->setAccessible(true);
$method->invoke($manager);
// Verify the claim was NOT released
$task->refresh();
$this->assertEquals($claimId, $task->claimed_by);
$this->assertNotNull($task->claimed_at);
}
/**
* Test that claimTask method works correctly.
*/
public function testClaimTaskMethod()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_1',
]),
]);
$manager = new TaskSchedulerManager();
$now = Carbon::now()->format('Y-m-d H:i:s');
// Use reflection to call the private method
$reflection = new \ReflectionClass($manager);
$method = $reflection->getMethod('claimTask');
$method->setAccessible(true);
// First claim should succeed
$result1 = $method->invoke($manager, $task->id, $now);
$this->assertTrue($result1, 'First claim should succeed');
// Second claim should fail
$result2 = $method->invoke($manager, $task->id, $now);
$this->assertFalse($result2, 'Second claim should fail');
}
/**
* Test race condition prevention with multiple concurrent claims.
*/
public function testRaceConditionPrevention()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_1',
]),
]);
$claimResults = [];
$now = Carbon::now()->format('Y-m-d H:i:s');
// Simulate 10 concurrent claims
for ($i = 0; $i < 10; $i++) {
$claimed = DB::table('scheduled_tasks')
->where('id', $task->id)
->whereNull('claimed_by')
->update([
'claimed_by' => Str::uuid()->toString(),
'claimed_at' => $now,
]);
$claimResults[] = $claimed;
}
// Only ONE claim should succeed
$successfulClaims = array_sum($claimResults);
$this->assertEquals(1, $successfulClaims, 'Only one claim should succeed out of 10 attempts');
}
/**
* Test that task is released after successful execution.
*/
public function testTaskReleasedAfterExecution()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_1',
]),
'claimed_by' => 'test-claim-id',
'claimed_at' => Carbon::now(),
]);
$manager = new TaskSchedulerManager();
$reflection = new \ReflectionClass($manager);
$method = $reflection->getMethod('releaseTask');
$method->setAccessible(true);
$method->invoke($manager, $task);
// Verify the task was released
$task->refresh();
$this->assertNull($task->claimed_by);
$this->assertNull($task->claimed_at);
}
/**
* Test that already claimed tasks are skipped during processing.
*/
public function testAlreadyClaimedTasksAreSkipped()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
// Task that is already claimed by another process
$claimedTask = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_1',
]),
'claimed_by' => 'other-process-claim',
'claimed_at' => Carbon::now(),
]);
// Unclaimed task
$unclaimedTask = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleInterval(),
'element_id' => 'node_2',
]),
]);
// Query for unclaimed tasks only (as the new implementation does)
$unclaimedTasks = ScheduledTask::whereNull('claimed_by')->get();
$this->assertCount(1, $unclaimedTasks);
$this->assertEquals($unclaimedTask->id, $unclaimedTasks->first()->id);
}
/**
* Test selection logic: task should NOT be claimed if nextDate is in the future.
*/
public function testTaskNotExecutedIfNextDateInFuture()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
// Set a fake "today" to control time precisely
$fakeToday = Carbon::create(2026, 1, 12, 12, 0, 0, 'UTC');
TaskSchedulerManager::fakeToday($fakeToday);
// Create a task that was executed 5 minutes ago with 60-minute interval
// Next execution should be at 12:55 (55 minutes in the future)
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::create(2026, 1, 12, 11, 55, 0, 'UTC')->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleIntervalFromDate(
Carbon::create(2026, 1, 12, 10, 55, 0, 'UTC'), // Start at 10:55
60 // Every 60 minutes
),
'element_id' => 'node_1',
]),
]);
$manager = new TaskSchedulerManager();
$today = $manager->today();
// Calculate nextDate using the manager
$config = json_decode($task->configuration);
$lastExecution = new \DateTime($task->last_execution, new \DateTimeZone('UTC'));
$nextDate = $manager->nextDate($today, $config, $lastExecution, null);
// nextDate should be 12:55 which is 55 minutes after "today" (12:00)
$this->assertNotNull($nextDate);
$this->assertGreaterThan($today->getTimestamp(), $nextDate->getTimestamp());
// Reset fake today
TaskSchedulerManager::fakeToday(null);
}
/**
* Test selection logic: task SHOULD be executed if nextDate has passed.
*/
public function testTaskExecutedIfNextDatePassed()
{
$process = Process::factory()->create(['status' => 'ACTIVE']);
// Set a fake "today" to control time precisely
$fakeToday = Carbon::create(2026, 1, 12, 12, 30, 0, 'UTC');
TaskSchedulerManager::fakeToday($fakeToday);
// Create a task with last execution at 12:00, 1-minute interval
// Next execution should be at 12:01, which is 29 minutes in the past
$task = ScheduledTask::create([
'process_id' => $process->id,
'type' => 'TIMER_START_EVENT',
'last_execution' => Carbon::create(2026, 1, 12, 12, 0, 0, 'UTC')->format('Y-m-d H:i:s'),
'configuration' => json_encode([
'type' => 'TimeCycle',
'interval' => $this->createCycleIntervalFromDate(
Carbon::create(2026, 1, 12, 11, 0, 0, 'UTC'), // Start at 11:00
1 // Every 1 minute
),
'element_id' => 'node_1',
]),
]);
$manager = new TaskSchedulerManager();
$today = $manager->today();
// Calculate nextDate
$config = json_decode($task->configuration);
$lastExecution = new \DateTime($task->last_execution, new \DateTimeZone('UTC'));
$nextDate = $manager->nextDate($today, $config, $lastExecution, null);
// nextDate should be 12:01 which is before "today" (12:30)
$this->assertNotNull($nextDate);
$this->assertLessThanOrEqual($today->getTimestamp(), $nextDate->getTimestamp());
// Reset fake today
TaskSchedulerManager::fakeToday(null);
}
/**
* Helper method to create a cycle interval configuration.
*
* @param int $minutes Interval in minutes (default 1)
*/
private function createCycleInterval(int $minutes = 1): object
{
return (object) [
'start' => (object) [
'date' => Carbon::now()->subHour()->format('Y-m-d H:i:s'),
'timezone' => 'UTC',
],
'interval' => (object) [
'y' => 0,
'm' => 0,
'd' => 0,
'h' => 0,
'i' => $minutes,
's' => 0,
'f' => 0,
],
'end' => null,
'recurrences' => 0,
];
}
/**
* Helper method to create a cycle interval configuration with a specific start date.
*
* @param Carbon $startDate The start date for the cycle
* @param int $minutes Interval in minutes
*/
private function createCycleIntervalFromDate(Carbon $startDate, int $minutes): object
{
return (object) [
'start' => (object) [
'date' => $startDate->format('Y-m-d H:i:s'),
'timezone' => 'UTC',
],
'interval' => (object) [
'y' => 0,
'm' => 0,
'd' => 0,
'h' => 0,
'i' => $minutes,
's' => 0,
'f' => 0,
],
'end' => null,
'recurrences' => 0,
];
}
}