diff --git a/.github/feature-test-timings.json b/.github/feature-test-timings.json index a1c2be173..0b6164c6b 100644 --- a/.github/feature-test-timings.json +++ b/.github/feature-test-timings.json @@ -17,6 +17,7 @@ "tests/Feature/ExceptionLoggingReplayTest.php": 7.228751, "tests/Feature/ExceptionWorkflowTest.php": 8.416919, "tests/Feature/ExplicitQueueWorkerTest.php": 4.186096, + "tests/Feature/FailedActivityRedispatchTest.php": 13.0, "tests/Feature/FailingWorkflowTest.php": 6.256735, "tests/Feature/HeartbeatWorkflowTest.php": 13.477668, "tests/Feature/MigrationTest.php": 55.40979, @@ -272,6 +273,7 @@ "tests/Feature/ExceptionLoggingReplayTest.php": 8.584815, "tests/Feature/ExceptionWorkflowTest.php": 4.583109, "tests/Feature/ExplicitQueueWorkerTest.php": 0.87937, + "tests/Feature/FailedActivityRedispatchTest.php": 12.0, "tests/Feature/FailingWorkflowTest.php": 3.523026, "tests/Feature/HeartbeatWorkflowTest.php": 13.476006, "tests/Feature/MigrationTest.php": 12.426278, diff --git a/CHANGELOG.md b/CHANGELOG.md index 9181dc8c1..bfe2f2264 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,25 @@ published package. `docs/business-state-machines.md` describes what it checks. The compensation-scope and state-machine journeys now share `scripts/ci/upgrade-journey-lib.sh`. +- A v1 activity whose job failed no longer runs a second time when the + workflow replays before the failure is recorded (#1241). The failure is + recorded by a separately queued `Workflow\Exception` job after the activity's + unique lock is released, so a wake in between (a signal, timer or sibling + result) dispatched the activity again and the copy executed it, adding an + exception row and repeating its side effects. The failed job now marks the + activity in the cache before its lock is released. A copy that finds the mark + completes without executing, recording a result or firing activity events, and + the Exception job removes the mark once it has recorded the failure or let it + go. Recorded history, job payloads and replay are unchanged. + - The mark expires after `workflows.activity_failure_mark_seconds` + (`DW_V1_ACTIVITY_FAILURE_MARK_SECONDS`, default 3600). If the Exception job + is lost (a flushed queue, an undecryptable payload), a later wake then runs + the activity again, as before this release, instead of the workflow waiting + forever. Keep it well above the time an Exception job can wait behind + earlier pending work. + - A job that times out, or exhausts its attempts after a worker crash, fails + without running the activity's catch block, so it writes no mark and the + redispatch race stays open on those paths, as before. ## 2.33.0 - 2026-11-19 diff --git a/src/Activity.php b/src/Activity.php index f22e488e7..b82d36cdc 100644 --- a/src/Activity.php +++ b/src/Activity.php @@ -44,6 +44,15 @@ class Activity implements ShouldBeEncrypted, ShouldBeUnique, ShouldQueue public $key = ''; + /** + * @internal Whether another job of this activity failed and its outcome + * is not yet recorded, once read for this handling. Unset typed + * properties are not serialized, so payloads are unchanged. + */ + public bool $awaitingRecordedFailure; + + private bool $ownsFailureMark; + private Container $container; public function __construct( @@ -111,12 +120,32 @@ public function handle() $this->container = App::make(Container::class); + if (self::awaitsRecordedFailure($this)) { + return; + } + if ($this->storedWorkflow->hasLogByIndex($this->index)) { return; } + // The Exception job forgets the failure only after recording it, so + // logs loaded with this job's payload may predate that record. + if ($this->storedWorkflow->relationLoaded('logs') && $this->storedWorkflow->logs() + ->whereIndex($this->index) + ->exists()) { + return; + } + try { - return $this->{'execute'}(...$this->resolveClassMethodDependencies($this->arguments, $this, 'execute')); + $result = $this->{'execute'}( + ...$this->resolveClassMethodDependencies($this->arguments, $this, 'execute') + ); + + if ($this->ownsFailureMark ?? false) { + Cache::forget(self::failureKey($this->storedWorkflow->id, $this->index)); + } + + return $result; } catch (\Throwable $throwable) { $this->storedWorkflow->exceptions() ->create([ @@ -124,6 +153,17 @@ public function handle() 'exception' => Serializer::serialize($throwable), ]); + // Recorded before the job can fail and release its unique lock. + // It expires so that a lost Exception job cannot strand the run. + $uuid = $this->queueJobUuid(); + if ($uuid !== null) { + Cache::put( + self::failureKey($this->storedWorkflow->id, $this->index), + $uuid, + self::failureMarkSeconds() + ); + } + if ($throwable instanceof NonRetryableExceptionContract) { $this->fail($throwable); } @@ -132,6 +172,39 @@ public function handle() } } + /** + * @internal A failed job's outcome is recorded later by a queued Exception + * job, after this activity's unique lock is gone. A replay in between + * dispatches the activity again; that copy must not run it again. Read + * once per handling, before the activity is reported as started. + */ + public static function awaitsRecordedFailure(self $activity): bool + { + if (! isset($activity->awaitingRecordedFailure)) { + $failedJob = Cache::get(self::failureKey($activity->storedWorkflow->id, $activity->index)); + $activity->ownsFailureMark = $failedJob !== null && $failedJob === $activity->queueJobUuid(); + $activity->awaitingRecordedFailure = $failedJob !== null && ! $activity->ownsFailureMark; + } + + return $activity->awaitingRecordedFailure; + } + + /** + * @internal + */ + public static function failureMarkSeconds(): int + { + return max(1, (int) config('workflows.activity_failure_mark_seconds', 3600)); + } + + /** + * @internal + */ + public static function failureKey(int|string $workflowId, int $index): string + { + return 'laravel-workflow-activity-failure:' . $workflowId . ':' . $index; + } + public function middleware() { return [ @@ -183,4 +256,11 @@ public function heartbeat(): void Cache::put($this->key, 1, $this->timeout); } } + + private function queueJobUuid(): ?string + { + $uuid = $this->job?->uuid(); + + return is_string($uuid) && $uuid !== '' ? $uuid : null; + } } diff --git a/src/Exception.php b/src/Exception.php index e5e2bea41..3f87b66cf 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -71,6 +71,8 @@ public function handle() return; } + $released = false; + try { $workflow = $this->storedWorkflow->toWorkflow(); @@ -84,13 +86,21 @@ public function handle() $workflow->next($this->index, $this->now, self::class, $this->exceptionPayload()); } elseif ($probeDecision === self::PROBE_RETRY) { $this->release(self::LOCK_RETRY_DELAY); + $released = true; } } } catch (TransitionNotFound) { if ($workflow->running()) { $this->release(); + $released = true; } } + + // Until here a redispatched copy of the failed activity waits for + // this outcome instead of running again (Activity::handle). + if (! $released) { + Cache::forget(Activity::failureKey($this->storedWorkflow->id, $this->index)); + } } finally { $lock->release(); } diff --git a/src/Middleware/ActivityMiddleware.php b/src/Middleware/ActivityMiddleware.php index 6e71f1f8f..95424b966 100644 --- a/src/Middleware/ActivityMiddleware.php +++ b/src/Middleware/ActivityMiddleware.php @@ -7,6 +7,7 @@ use Illuminate\Support\Str; use LimitIterator; use SplFileObject; +use Workflow\Activity; use Workflow\Events\ActivityCompleted; use Workflow\Events\ActivityFailed; use Workflow\Events\ActivityStarted; @@ -22,6 +23,12 @@ final class ActivityMiddleware public function handle($job, $next): void { + // A copy of an activity whose failed job is not yet recorded neither + // runs nor reports anything; the Exception job records the outcome. + if ($job instanceof Activity && Activity::awaitsRecordedFailure($job)) { + return; + } + $this->job = $job; $this->uuid = (string) Str::uuid(); diff --git a/src/config/workflows.php b/src/config/workflows.php index c35968c10..c1ebeecc1 100644 --- a/src/config/workflows.php +++ b/src/config/workflows.php @@ -47,6 +47,15 @@ // below this release requires draining the runs that recorded v2 first. 'definition_fingerprint_version' => env('DW_DEFINITION_FINGERPRINT_VERSION', 'v1'), + // Seconds a failed v1 activity stays marked while its queued + // Workflow\Exception job records the failure. A copy of the activity + // dispatched by a replay in that window does not run it again. The mark + // expires so that a lost Exception job (a flushed queue, an undecryptable + // payload) cannot leave the workflow waiting forever; a later wake then + // runs the activity again. A top-level key, so an application whose + // published workflows.php predates it still receives the default. + 'activity_failure_mark_seconds' => (int) env('DW_V1_ACTIVITY_FAILURE_MARK_SECONDS', 3600), + 'v1' => [ // Existing installations keep their v1 drain. Disable only for a // fresh v2-only application or after all legacy work is retired. diff --git a/tests/Feature/FailedActivityRedispatchTest.php b/tests/Feature/FailedActivityRedispatchTest.php new file mode 100644 index 000000000..22959f34b --- /dev/null +++ b/tests/Feature/FailedActivityRedispatchTest.php @@ -0,0 +1,191 @@ +getConnection(); + $key = Queue::connection('redis')->getQueue($queue); + $exceptionLock = null; + + try { + $workflow = WorkflowStub::make(TestFailedActivityRedispatchWorkflow::class); + $workflow->start($directory, new WorkflowOptions(connection: 'redis', queue: $queue)); + $this->work($queue); + $this->assertSame(WorkflowWaitingStatus::class, $workflow->fresh()->status()); + $this->assertSame(1, $redis->llen($key), 'The workflow dispatches its activity.'); + + // Any wake (a signal, timer or sibling result) replays the workflow. + $workflow->resume(); + $this->assertSame(2, $redis->llen($key)); + + // The activity fails its job; the Exception job is queued behind + // the wake and the activity's unique lock is released. + $this->work($queue); + $this->assertSame(1, $this->executions($directory)); + $this->assertSame(1, $workflow->exceptions()->count()); + $this->assertSame(0, $workflow->logs()->count()); + + // The wake replays before the failure is recorded and dispatches + // the activity again behind the Exception job. + $this->work($queue); + $this->assertSame(0, $workflow->logs()->count()); + $this->assertSame(2, $redis->llen($key), 'The replay dispatches the unrecorded activity again.'); + + // The Exception job is still waiting to record the failure. + $exceptionLock = Cache::lock('laravel-workflow-exception:' . $workflow->id(), 15); + $this->assertTrue($exceptionLock->get()); + $this->work($queue); + $this->assertSame(1, $redis->llen($key)); + $this->assertSame(1, $redis->zcard($key . ':delayed')); + + // The redispatched copy runs while the failure is unrecorded. + $this->work($queue); + $this->assertSame(1, $this->executions($directory), 'The failed activity ran again.'); + $this->assertSame(1, $workflow->exceptions()->count()); + + $exceptionLock->release(); + $exceptionLock = null; + $this->drain($queue, $workflow); + + $this->assertSame(WorkflowCompletedStatus::class, $workflow->status()); + $this->assertSame('caught: activity failed', $workflow->output()); + $this->assertSame(1, $this->executions($directory)); + $this->assertSame(1, $workflow->exceptions()->count()); + $this->assertSame([Exception::class], $workflow->logs()->pluck('class')->all()); + $this->assertSame( + [TestFailedActivityRedispatchActivity::class], + $workflow->exceptions() + ->pluck('class') + ->all() + ); + } finally { + $exceptionLock?->release(); + foreach (glob($directory . '/*') ?: [] as $file) { + unlink($file); + } + rmdir($directory); + } + } + + public function testLostExceptionJobLeavesTheActivityRunnableAfterTheMarkExpires(): void + { + $queue = 'lost-exception-job-' . Str::uuid(); + $directory = sys_get_temp_dir() . '/' . $queue; + mkdir($directory, 0700); + $redis = Queue::connection('redis')->getConnection(); + $key = Queue::connection('redis')->getQueue($queue); + $environment = [ + 'DW_V1_ACTIVITY_FAILURE_MARK_SECONDS' => (string) self::SHORT_MARK_SECONDS, + ]; + + try { + $workflow = WorkflowStub::make(TestFailedActivityRedispatchWorkflow::class); + $workflow->start($directory, new WorkflowOptions(connection: 'redis', queue: $queue)); + $this->work($queue, $environment); + $this->work($queue, $environment); + $this->assertSame(1, $this->executions($directory)); + $this->assertSame(1, $redis->llen($key), 'The Exception job is queued.'); + $mark = Activity::failureKey($workflow->id(), 0); + $this->assertNotNull(Cache::get($mark)); + + // The Exception job is lost (a flushed queue, an undecryptable payload). + $redis->del($key); + + // While the mark lasts, a wake's copy of the activity does not run. + $workflow->resume(); + $this->work($queue, $environment); + $this->assertSame(1, $redis->llen($key)); + $this->work($queue, $environment); + $this->assertSame(1, $this->executions($directory)); + $this->assertSame(WorkflowWaitingStatus::class, $workflow->fresh()->status()); + + // Once it expires, the next wake runs the activity again. + $deadline = microtime(true) + self::SHORT_MARK_SECONDS + 5; + while (Cache::get($mark) !== null) { + if (microtime(true) > $deadline) { + $this->fail('The failure mark did not expire.'); + } + usleep(100000); + } + $workflow->resume(); + $this->drain($queue, $workflow, $environment); + + $this->assertSame(WorkflowCompletedStatus::class, $workflow->status()); + $this->assertSame('caught: activity failed', $workflow->output()); + $this->assertSame(2, $this->executions($directory)); + $this->assertSame([Exception::class], $workflow->logs()->pluck('class')->all()); + } finally { + foreach (glob($directory . '/*') ?: [] as $file) { + unlink($file); + } + rmdir($directory); + } + } + + /** + * @param array $environment + */ + private function work(string $queue, array $environment = []): void + { + $process = new Process(['php', __DIR__ . '/../../vendor/bin/testbench', 'queue:work', 'redis', + '--queue=' . $queue, '--once', '--sleep=0'], env: [ + 'WORKFLOW_WATCHDOG_ENABLED' => 'false', + ...$environment, + ]); + $process->setTimeout(30); + $process->mustRun(); + } + + /** + * @param array $environment + */ + private function drain(string $queue, WorkflowStub $workflow, array $environment = []): void + { + $deadline = microtime(true) + 30; + + while (! $workflow->fresh()->completed() && ! $workflow->failed()) { + if (microtime(true) > $deadline) { + $this->fail('The workflow did not finish after its failure was recorded.'); + } + + $this->work($queue, $environment); + usleep(100000); + } + } + + private function executions(string $directory): int + { + $path = $directory . '/executions'; + + return is_file($path) ? substr_count((string) file_get_contents($path), "\n") : 0; + } +} diff --git a/tests/Fixtures/TestFailedActivityRedispatchActivity.php b/tests/Fixtures/TestFailedActivityRedispatchActivity.php new file mode 100644 index 000000000..8b5dbf760 --- /dev/null +++ b/tests/Fixtures/TestFailedActivityRedispatchActivity.php @@ -0,0 +1,22 @@ +getMessage(); + } + } +} diff --git a/tests/Unit/ActivityTest.php b/tests/Unit/ActivityTest.php index 4b11504fa..a87785106 100644 --- a/tests/Unit/ActivityTest.php +++ b/tests/Unit/ActivityTest.php @@ -6,6 +6,9 @@ use BadMethodCallException; use Exception; +use Illuminate\Contracts\Queue\Job as JobContract; +use Illuminate\Support\Facades\Cache; +use Mockery; use Tests\Fixtures\TestExceptionActivity; use Tests\Fixtures\TestInvalidActivity; use Tests\Fixtures\TestNonRetryableExceptionActivity; @@ -13,6 +16,7 @@ use Tests\Fixtures\TestOtherActivity; use Tests\Fixtures\TestWorkflow; use Tests\TestCase; +use Workflow\Activity; use Workflow\Exceptions\NonRetryableException; use Workflow\Models\StoredWorkflow; use Workflow\Serializers\Serializer; @@ -120,6 +124,67 @@ public function testFailedActivity(): void $this->assertSame(WorkflowCreatedStatus::class, $workflow->status()); } + public function testFailedJobMarksItsActivityBeforeReleasingIt(): void + { + $workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); + $activity = new TestExceptionActivity(0, now()->toDateTimeString(), StoredWorkflow::findOrFail( + $workflow->id() + )); + $activity->setJob($this->queueJob('failed-job')); + + try { + $activity->handle(); + $this->fail('The activity should fail.'); + } catch (Exception $exception) { + $this->assertSame('failed', $exception->getMessage()); + } + + $this->assertSame('failed-job', Cache::get(Activity::failureKey($workflow->id(), 0))); + } + + public function testCopyDoesNotRunWhileAnotherJobsFailureIsUnrecorded(): void + { + $workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); + Cache::forever(Activity::failureKey($workflow->id(), 0), 'failed-job'); + $activity = new TestExceptionActivity(0, now()->toDateTimeString(), StoredWorkflow::findOrFail( + $workflow->id() + )); + $activity->setJob($this->queueJob('copy-job')); + + $this->assertNull($activity->handle()); + $this->assertTrue($activity->awaitingRecordedFailure); + $this->assertSame(0, $workflow->exceptions()->count()); + $this->assertSame('failed-job', Cache::get(Activity::failureKey($workflow->id(), 0))); + } + + public function testRetriedJobRunsAndClearsItsOwnMark(): void + { + $workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); + Cache::forever(Activity::failureKey($workflow->id(), 0), 'retried-job'); + $activity = new TestOtherActivity(0, now()->toDateTimeString(), StoredWorkflow::findOrFail($workflow->id()), [ + 'other', + ]); + $activity->setJob($this->queueJob('retried-job')); + + $this->assertSame(['other'], $activity->handle()); + $this->assertNull(Cache::get(Activity::failureKey($workflow->id(), 0))); + } + + public function testFailureMarkExpiresAfterTheConfiguredSeconds(): void + { + $this->assertSame(3600, Activity::failureMarkSeconds()); + + config([ + 'workflows.activity_failure_mark_seconds' => 120, + ]); + $this->assertSame(120, Activity::failureMarkSeconds()); + + config([ + 'workflows.activity_failure_mark_seconds' => 0, + ]); + $this->assertSame(1, Activity::failureMarkSeconds()); + } + public function testActivityAlreadyComplete(): void { $workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); @@ -179,4 +244,15 @@ public function testNullArgumentsDoNotShift(): void 'comparing' => 'hours', ], $result); } + + private function queueJob(string $uuid): JobContract + { + $job = Mockery::mock(JobContract::class); + $job->shouldReceive('uuid') + ->andReturn($uuid); + $job->shouldReceive('attempts') + ->andReturn(1); + + return $job; + } } diff --git a/tests/Unit/Config/WorkflowsConfigTest.php b/tests/Unit/Config/WorkflowsConfigTest.php index 9b472842e..06d53a870 100644 --- a/tests/Unit/Config/WorkflowsConfigTest.php +++ b/tests/Unit/Config/WorkflowsConfigTest.php @@ -27,6 +27,7 @@ final class WorkflowsConfigTest extends NonDatabaseTestCase 'DW_V2_COMPATIBILITY_HEARTBEAT_TTL', 'WORKFLOW_V2_COMPATIBILITY_HEARTBEAT_TTL', 'DW_V2_PIN_TO_RECORDED_FINGERPRINT', 'WORKFLOW_V2_PIN_TO_RECORDED_FINGERPRINT', 'DW_DEFINITION_FINGERPRINT_VERSION', + 'DW_V1_ACTIVITY_FAILURE_MARK_SECONDS', 'DW_V2_CONTINUE_AS_NEW_EVENT_THRESHOLD', 'WORKFLOW_V2_CONTINUE_AS_NEW_EVENT_THRESHOLD', 'DW_V2_CONTINUE_AS_NEW_SIZE_BYTES_THRESHOLD', 'WORKFLOW_V2_CONTINUE_AS_NEW_SIZE_BYTES_THRESHOLD', 'DW_V2_HISTORY_EXPORT_SIGNING_KEY', 'WORKFLOW_V2_HISTORY_EXPORT_SIGNING_KEY', diff --git a/tests/Unit/ExceptionTest.php b/tests/Unit/ExceptionTest.php index 9df287de2..7d1e0f801 100644 --- a/tests/Unit/ExceptionTest.php +++ b/tests/Unit/ExceptionTest.php @@ -7,6 +7,7 @@ use Exception as BaseException; use Illuminate\Contracts\Queue\Job as JobContract; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Queue; use InvalidArgumentException; use Mockery; use ReflectionMethod; @@ -22,6 +23,7 @@ use Tests\Fixtures\TestSagaParallelActivityWorkflow; use Tests\Fixtures\TestWorkflow; use Tests\TestCase; +use Workflow\Activity; use Workflow\Exception; use Workflow\Middleware\WithoutOverlappingMiddleware; use Workflow\Models\StoredWorkflow; @@ -76,6 +78,9 @@ public function testHandleResumesWorkflowWhenLogAlreadyExists(): void ->once() ->with('laravel-workflow-exception:123', 15) ->andReturn($lock); + Cache::shouldReceive('forget') + ->once() + ->with(Activity::failureKey(123, 0)); $workflow = Mockery::mock(); $workflow->shouldReceive('resume') @@ -104,6 +109,32 @@ public function testHandleResumesWorkflowWhenLogAlreadyExists(): void Mockery::close(); } + public function testRecordedFailureRemovesTheActivityMark(): void + { + Queue::fake(); + + $workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); + $storedWorkflow = StoredWorkflow::findOrFail($workflow->id()); + $storedWorkflow->logs() + ->create([ + 'index' => 0, + 'now' => now() + ->toDateTimeString(), + 'class' => Exception::class, + 'result' => Serializer::serialize([ + 'class' => BaseException::class, + 'message' => 'recorded', + 'code' => 0, + ]), + ]); + Cache::forever(Activity::failureKey($storedWorkflow->id, 0), 'failed-job'); + + $exception = new Exception(0, now()->toDateTimeString(), $storedWorkflow, new BaseException('recorded')); + $exception->handle(); + + $this->assertNull(Cache::get(Activity::failureKey($storedWorkflow->id, 0))); + } + public function testHandleReleasesWhenExceptionLockUnavailable(): void { $workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id()); @@ -341,10 +372,13 @@ public function testRetriesWriteWhenProbeMatchesAfterEarlierPendingWork(): void 'message' => 'child failed: child-2', 'code' => 0, ], sourceClass: TestProbeChildFailureWorkflow::class); + Cache::forever(Activity::failureKey($storedWorkflow->id, 1), 'failed-job'); + $exception->setJob($job); $exception->handle(); $this->assertFalse($storedWorkflow->fresh()->hasLogByIndex(1)); + $this->assertSame('failed-job', Cache::get(Activity::failureKey($storedWorkflow->id, 1))); } public function testPersistsWriteWhenProbeReachesCandidateException(): void diff --git a/tests/Unit/Middleware/ActivityMiddlewareTest.php b/tests/Unit/Middleware/ActivityMiddlewareTest.php index b16ba7300..fd698db00 100644 --- a/tests/Unit/Middleware/ActivityMiddlewareTest.php +++ b/tests/Unit/Middleware/ActivityMiddlewareTest.php @@ -14,6 +14,7 @@ use Tests\Fixtures\TestModelNotFoundWorkflow; use Tests\Fixtures\TestWorkflow; use Tests\TestCase; +use Workflow\Activity; use Workflow\Events\ActivityCompleted; use Workflow\Events\ActivityFailed; use Workflow\Events\ActivityStarted; @@ -68,6 +69,33 @@ public function testMiddleware(): void Queue::assertPushed(TestWorkflow::class, 2); } + public function testCopyAwaitingAnotherJobsRecordedFailureNeitherRunsNorReports(): void + { + Event::fake(); + Queue::fake(); + + $workflow = WorkflowStub::make(TestWorkflow::class); + $workflow->start(); + $storedWorkflow = StoredWorkflow::findOrFail($workflow->id()); + Cache::forever(Activity::failureKey($storedWorkflow->id, 0), 'failed-job'); + + $activity = $this->mock(TestActivity::class); + $activity->index = 0; + $activity->now = now() + ->toDateTimeString(); + $activity->storedWorkflow = $storedWorkflow; + + (new ActivityMiddleware())->handle($activity, function (): void { + $this->fail('The copy must not run.'); + }); + + $this->assertTrue($activity->awaitingRecordedFailure); + $this->assertFalse(isset($activity->onUnlock)); + Event::assertNotDispatched(ActivityStarted::class); + Event::assertNotDispatched(ActivityCompleted::class); + Event::assertNotDispatched(ActivityFailed::class); + } + public function testAlreadyCompleted(): void { Event::fake();