Skip to content

Use laravel/serializable-closure #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@
"php": "^8.0.2",
"laravel/framework": "^9.19",
"spatie/laravel-model-states": "^2.1",
"react/promise": "^2.9",
"opis/closure": "^3.6"
"react/promise": "^2.9"
},
"require-dev": {
"orchestra/testbench": "^7.1",
Expand Down
4 changes: 4 additions & 0 deletions src/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public function handle()
throw new BadMethodCallException('Execute method not implemented.');
}

if ($this->storedWorkflow->logs()->whereIndex($this->index)->exists()) {
return;
}

try {
return $this->{'execute'}(...$this->arguments);
} catch (\Throwable $throwable) {
Expand Down
12 changes: 7 additions & 5 deletions src/Serializers/Y.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Workflow\Serializers;

use Opis\Closure\SerializableClosure;
use function Opis\Closure\serialize as s;
use function Opis\Closure\unserialize as u;
use Laravel\SerializableClosure\SerializableClosure;

final class Y implements SerializerInterface
{
Expand Down Expand Up @@ -42,12 +40,16 @@ public static function decode(string $data): string
public static function serialize($data): string
{
SerializableClosure::setSecretKey(config('app.key'));
return self::encode(s($data));
return self::encode(serialize(new SerializableClosure(static fn () => $data)));
}

public static function unserialize(string $data)
{
SerializableClosure::setSecretKey(config('app.key'));
return u(self::decode($data));
$unserialized = unserialize(self::decode($data));
if ($unserialized instanceof SerializableClosure) {
$unserialized = ($unserialized->getClosure())();
}
return $unserialized;
}
}
12 changes: 12 additions & 0 deletions tests/Fixtures/TestEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

enum TestEnum: string
{
case First = 'first';
case Second = 'second';
case Third = 'third';
}
2 changes: 1 addition & 1 deletion tests/Unit/ActivityStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function testStoresResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => TestActivity::class,
'result' => Y::serialize('activity'),
]);
$this->assertSame('activity', Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testLoadsStoredResult(): void
Expand Down
27 changes: 26 additions & 1 deletion tests/Unit/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Tests\Fixtures\TestWorkflow;
use Tests\TestCase;
use Workflow\Models\StoredWorkflow;
use Workflow\Serializers\Y;
use Workflow\States\WorkflowFailedStatus;
use Workflow\WorkflowStub;

Expand All @@ -26,8 +27,9 @@ public function testActivity(): void
$activity->timeout = 1;
$activity->heartbeat();

$activity->handle();
$result = $activity->handle();

$this->assertSame(['other'], $result);
$this->assertSame([1, 2, 5, 10, 15, 30, 60, 120], $activity->backoff());
$this->assertSame($workflow->id(), $activity->workflowId());
$this->assertSame($activity->timeout, pcntl_alarm(0));
Expand Down Expand Up @@ -78,4 +80,27 @@ public function testFailedActivity(): void
$this->assertSame(0, $workflow->logs()->count());
$this->assertSame(WorkflowFailedStatus::class, $workflow->status());
}

public function testActivityAlreadyComplete(): void
{
$workflow = WorkflowStub::load(WorkflowStub::make(TestWorkflow::class)->id());
StoredWorkflow::findOrFail($workflow->id())->logs()->create([
'index' => 0,
'now' => now(),
'class' => TestOtherActivity::class,
'result' => Y::serialize('other'),
]);
$activity = new TestOtherActivity(0, now()->toDateTimeString(), StoredWorkflow::findOrFail($workflow->id()), [
'other',
]);
$activity->timeout = 1;
$activity->heartbeat();

$result = $activity->handle();

$this->assertNull($result);
$this->assertSame([1, 2, 5, 10, 15, 30, 60, 120], $activity->backoff());
$this->assertSame($workflow->id(), $activity->workflowId());
$this->assertSame($activity->timeout, pcntl_alarm(0));
}
}
3 changes: 3 additions & 0 deletions tests/Unit/Serializers/SerializeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Tests\Unit\Serializers;

use Tests\Fixtures\TestEnum;
use Tests\TestCase;
use Workflow\Serializers\Y;

Expand Down Expand Up @@ -32,6 +33,8 @@ public function dataProvider(): array
],
'bool true' => [true],
'bool false' => [false],
'enum' => [TestEnum::First],
'enum[]' => [[TestEnum::First]],
'int(PHP_INT_MIN)' => [PHP_INT_MIN],
'int(PHP_INT_MAX)' => [PHP_INT_MAX],
'int(-1)' => [-1],
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Traits/AwaitWithTimeoutsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public function testStoresResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertTrue(Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testLoadsStoredResult(): void
Expand All @@ -82,8 +82,8 @@ public function testLoadsStoredResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertTrue(Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testResolvesConflictingResult(): void
Expand Down Expand Up @@ -111,7 +111,7 @@ public function testResolvesConflictingResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(false),
]);
$this->assertFalse(Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Traits/AwaitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public function testStoresResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertTrue(Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testLoadsStoredResult(): void
Expand All @@ -68,8 +68,8 @@ public function testLoadsStoredResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertTrue(Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testResolvesConflictingResult(): void
Expand Down Expand Up @@ -97,7 +97,7 @@ public function testResolvesConflictingResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(false),
]);
$this->assertFalse(Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Traits/SideEffectsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function testStoresResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => TestWorkflow::class,
'result' => Y::serialize('test'),
]);
$this->assertSame('test', Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testLoadsStoredResult(): void
Expand All @@ -54,8 +54,8 @@ public function testLoadsStoredResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => TestWorkflow::class,
'result' => Y::serialize('test'),
]);
$this->assertSame('test', Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testResolvesConflictingResult(): void
Expand Down Expand Up @@ -83,7 +83,7 @@ public function testResolvesConflictingResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => TestWorkflow::class,
'result' => Y::serialize('test'),
]);
$this->assertSame('test', Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}
}
4 changes: 2 additions & 2 deletions tests/Unit/Traits/TimersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public function testStoresResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertSame(true, Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}

public function testLoadsStoredResult(): void
Expand Down Expand Up @@ -135,7 +135,7 @@ public function testLoadsStoredResult(): void
'stored_workflow_id' => $workflow->id(),
'index' => 0,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertSame(true, Y::unserialize($workflow->logs()->firstWhere('index', 0)->result));
}
}
6 changes: 3 additions & 3 deletions tests/Unit/WorkflowStubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public function testAwait(): void
'stored_workflow_id' => $workflow->id(),
'index' => 1,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertTrue(Y::unserialize($workflow->logs()->firstWhere('index', 1)->result));

$workflow->fresh();
$context = WorkflowStub::getContext();
Expand Down Expand Up @@ -130,8 +130,8 @@ public function testAwaitWithTimeout(): void
'stored_workflow_id' => $workflow->id(),
'index' => 1,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertTrue(Y::unserialize($workflow->logs()->firstWhere('index', 1)->result));

$workflow->fresh();
$context = WorkflowStub::getContext();
Expand Down Expand Up @@ -178,7 +178,7 @@ public function testAwaitWithTimeoutTimedout(): void
'stored_workflow_id' => $workflow->id(),
'index' => 1,
'class' => Signal::class,
'result' => Y::serialize(true),
]);
$this->assertTrue(Y::unserialize($workflow->logs()->firstWhere('index', 1)->result));
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy