|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit\V2; |
| 6 | + |
| 7 | +use Illuminate\Database\Eloquent\Collection; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Workflow\V2\Enums\HistoryEventType; |
| 10 | +use Workflow\V2\Models\WorkflowHistoryEvent; |
| 11 | +use Workflow\V2\Models\WorkflowRun; |
| 12 | +use Workflow\V2\Support\ActivityCall; |
| 13 | +use Workflow\V2\Support\ActivityReplayIdentity; |
| 14 | +use Workflow\V2\Support\ParallelChildGroup; |
| 15 | + |
| 16 | +final class ReplayPayloadReadGuardTest extends TestCase |
| 17 | +{ |
| 18 | + public function testActivityIdentityDoesNotDecodeUnrelatedEventTypes(): void |
| 19 | + { |
| 20 | + $unrelated = self::event(HistoryEventType::WorkflowStarted, 1, 7); |
| 21 | + $scheduled = self::event(HistoryEventType::ActivityScheduled, 2, 7); |
| 22 | + $after = self::event(HistoryEventType::ActivityCompleted, 3, 7); |
| 23 | + $events = new Collection([$unrelated, $scheduled, $after]); |
| 24 | + $before = $events->map->getAttributes() |
| 25 | + ->all(); |
| 26 | + $run = new WorkflowRun(); |
| 27 | + $run->setRelation('historyEvents', $events); |
| 28 | + |
| 29 | + ActivityReplayIdentity::assertCall($run, new ActivityCall('ExampleActivity', [1]), 7); |
| 30 | + |
| 31 | + self::assertSame(0, $unrelated->payloadReads); |
| 32 | + self::assertGreaterThan(0, $scheduled->payloadReads); |
| 33 | + self::assertSame(0, $after->payloadReads); |
| 34 | + self::assertSame($before, $events->map->getAttributes()->all()); |
| 35 | + } |
| 36 | + |
| 37 | + public function testFailureLookupDoesNotDecodeUnrelatedEventTypes(): void |
| 38 | + { |
| 39 | + $events = new Collection(); |
| 40 | + foreach ([ |
| 41 | + HistoryEventType::WorkflowStarted, |
| 42 | + HistoryEventType::ActivityScheduled, |
| 43 | + HistoryEventType::ActivityStarted, |
| 44 | + HistoryEventType::ActivityCompleted, |
| 45 | + HistoryEventType::TimerScheduled, |
| 46 | + HistoryEventType::TimerFired, |
| 47 | + HistoryEventType::WorkflowCompleted, |
| 48 | + ] as $index => $type) { |
| 49 | + $events->push(self::event($type, $index + 1, 1)); |
| 50 | + } |
| 51 | + $before = $events->map->getAttributes() |
| 52 | + ->all(); |
| 53 | + $run = new WorkflowRun(); |
| 54 | + $run->setRelation('historyEvents', $events); |
| 55 | + |
| 56 | + self::assertNull(ParallelChildGroup::memberFailureResolution($run, 1, 3)); |
| 57 | + |
| 58 | + foreach ($events as $event) { |
| 59 | + self::assertSame(0, $event->payloadReads); |
| 60 | + } |
| 61 | + self::assertSame($before, $events->map->getAttributes()->all()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testFailureLookupKeepsOriginalMemberAndHistoryOrdering(): void |
| 65 | + { |
| 66 | + $unrelated = self::event(HistoryEventType::ActivityCompleted, 1, 1); |
| 67 | + $otherGroup = self::event(HistoryEventType::ActivityFailed, 2, 9); |
| 68 | + $later = self::event(HistoryEventType::ActivityFailed, 40, 2); |
| 69 | + $earlier = self::event(HistoryEventType::ActivityTimedOut, 30, 3); |
| 70 | + $events = new Collection([$unrelated, $otherGroup, $later, $earlier]); |
| 71 | + $before = $events->map->getAttributes() |
| 72 | + ->all(); |
| 73 | + $run = new WorkflowRun(); |
| 74 | + $run->setRelation('historyEvents', $events); |
| 75 | + |
| 76 | + self::assertSame($earlier, ParallelChildGroup::memberFailureResolution($run, 1, 3)); |
| 77 | + self::assertGreaterThan(0, $earlier->payloadReads); |
| 78 | + self::assertSame($before, $events->map->getAttributes()->all()); |
| 79 | + } |
| 80 | + |
| 81 | + private static function event( |
| 82 | + HistoryEventType $type, |
| 83 | + int $historySequence, |
| 84 | + int $commandSequence |
| 85 | + ): ReadCountHistoryEvent { |
| 86 | + return new ReadCountHistoryEvent([ |
| 87 | + 'id' => 'event-' . $historySequence, |
| 88 | + 'sequence' => $historySequence, |
| 89 | + 'event_type' => $type, |
| 90 | + 'payload' => [ |
| 91 | + 'sequence' => $commandSequence, |
| 92 | + ], |
| 93 | + ]); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +final class ReadCountHistoryEvent extends WorkflowHistoryEvent |
| 98 | +{ |
| 99 | + public int $payloadReads = 0; |
| 100 | + |
| 101 | + public function getAttribute($key): mixed |
| 102 | + { |
| 103 | + if ($key === 'payload') { |
| 104 | + ++$this->payloadReads; |
| 105 | + } |
| 106 | + |
| 107 | + return parent::getAttribute($key); |
| 108 | + } |
| 109 | +} |
0 commit comments