Skip to content
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

Use Default task queue if there are no declarations #73

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 0 additions & 2 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ jobs:
extensions: sockets, grpc
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2']
stability: >-
['prefer-stable']
2 changes: 0 additions & 2 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,3 @@ jobs:
with:
os: >-
['ubuntu-latest']
php: >-
['8.1']
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"require-dev": {
"spiral/framework": "^3.0",
"spiral/testing": "^2.0",
"vimeo/psalm": "^4.9"
"vimeo/psalm": "^5.17"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
findUnusedBaselineEntry="true"
findUnusedCode="false"
>
<projectFiles>
<directory name="src" />
Expand Down
3 changes: 3 additions & 0 deletions src/Attribute/AssignWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Spiral\Attributes\NamedArgumentConstructor;

/**
* @psalm-suppress DeprecatedClass
*/
#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor]
final class AssignWorker
{
Expand Down
6 changes: 6 additions & 0 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function serve(): void
$factory = $this->container->get(WorkerFactoryInterface::class);
$registry = $this->container->get(WorkersRegistryInterface::class);

$hasDeclarations = false;
foreach ($declarations as $type => $declaration) {
// Worker that listens on a task queue and hosts both workflow and activity implementations.
$worker = $registry->get($this->resolveQueueName($declaration));
Expand All @@ -58,6 +59,11 @@ public function serve(): void
fn(ReflectionClass $class): object => $this->container->make($class->getName())
);
}
$hasDeclarations = true;
}

if (!$hasDeclarations) {
$registry->get(WorkerFactoryInterface::DEFAULT_TASK_QUEUE);
}

// start primary loop
Expand Down
4 changes: 4 additions & 0 deletions src/Generator/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function generateWorkflowSignalMethods(array $signalMethods, Class
foreach ($signalMethods as $method) {
$params = null;
if (\str_contains($method, ',')) {
/** @psalm-suppress PossiblyUndefinedArrayOffset */
[$method, $params] = \explode(',', $method, 2);
}

Expand Down Expand Up @@ -70,10 +71,12 @@ public static function parseMethods(array $methods): array
foreach ($methods as $method) {
$params = '';
if (\str_contains($method, ',')) {
/** @psalm-suppress PossiblyUndefinedArrayOffset */
[$method, $params] = \explode(',', $method, 2);
}

if (\str_contains($method, ':')) {
/** @psalm-suppress PossiblyUndefinedArrayOffset */
[$method, $type] = \explode(':', $method, 2);
}

Expand All @@ -100,6 +103,7 @@ public static function parseParameters(array $parameters): array
foreach ($parameters as $param) {
$type = null;
if (\str_contains($param, ':')) {
/** @psalm-suppress PossiblyUndefinedArrayOffset */
[$param, $type] = \explode(':', $param, 2);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/src/Commands/MakePresetCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class MakePresetCommandTest extends TestCase
public function testNotFoundPresetShouldShowErrorMessage(): void
{
$this->expectException(PresetNotFoundException::class);
$this->expectErrorMessage('Preset with given name [foo] is not defined.');
$this->expectExceptionMessage('Preset with given name [foo] is not defined.');

$this->runCommand('temporal:make-preset', ['preset' => 'foo', 'name' => ' bar']);
}
Expand Down
59 changes: 58 additions & 1 deletion tests/src/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
use Spiral\RoadRunnerBridge\RoadRunnerMode;
use Spiral\TemporalBridge\Attribute\AssignWorker;
use Spiral\TemporalBridge\Config\TemporalConfig;
use Spiral\TemporalBridge\DeclarationLocatorInterface;
use Spiral\TemporalBridge\Dispatcher;
use Spiral\TemporalBridge\Tests\App\SomeWorkflow;
use Spiral\TemporalBridge\WorkersRegistryInterface;
use Temporal\Worker\WorkerFactoryInterface;
use Temporal\Worker\WorkerInterface;
use Temporal\Workflow\WorkflowInterface;

final class DispatcherTest extends TestCase
{
Expand All @@ -23,7 +29,6 @@ public function testResolvingQueueName(): void

$ref = new \ReflectionClass($dispatcher);
$method = $ref->getMethod('resolveQueueName');
$method->setAccessible(true);

$queue = $method->invoke(
$dispatcher,
Expand All @@ -37,6 +42,58 @@ public function testResolvingQueueName(): void
);
$this->assertSame('foo', $queue);
}

public function testServeWithoutDeclarations(): void
{
$dispatcher = new Dispatcher(
RoadRunnerMode::Temporal,
new AttributeReader(),
new TemporalConfig(),
$this->getContainer(),
);

$locator = $this->mockContainer(DeclarationLocatorInterface::class);
$locator->shouldReceive('getDeclarations')->once()->andReturn([]);

$registry = $this->mockContainer(WorkersRegistryInterface::class);
$registry
->shouldReceive('get')
->once()
->with(WorkerFactoryInterface::DEFAULT_TASK_QUEUE)
->andReturn($this->createMock(WorkerInterface::class));

$factory = $this->mockContainer(WorkerFactoryInterface::class);
$factory->shouldReceive('run')->once();

$dispatcher->serve();
}

public function testServeWithDeclarations(): void
{
$dispatcher = new Dispatcher(
RoadRunnerMode::Temporal,
new AttributeReader(),
new TemporalConfig(),
$this->getContainer(),
);

$locator = $this->mockContainer(DeclarationLocatorInterface::class);
$locator->shouldReceive('getDeclarations')->once()->andReturn([
WorkflowInterface::class => new \ReflectionClass(SomeWorkflow::class),
]);

$registry = $this->mockContainer(WorkersRegistryInterface::class);
$registry
->shouldReceive('get')
->once()
->with('worker2')
->andReturn($this->createMock(WorkerInterface::class));

$factory = $this->mockContainer(WorkerFactoryInterface::class);
$factory->shouldReceive('run')->once();

$dispatcher->serve();
}
}

#[AssignWorker(name: 'worker1')]
Expand Down
8 changes: 7 additions & 1 deletion tests/src/Generator/PhpCodePrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ final class PhpCodePrinterTest extends TestCase
{
public function testPrint(): void
{
$namespace = new PhpNamespace('Foo\\Bar');
$namespace->addClass('Baz');

$printer = new PhpCodePrinter(
new PhpNamespace('Foo\\Bar'),
$namespace,
new Context('src/app/', 'App//Foo', 'Bar')
);

Expand All @@ -28,6 +31,9 @@ public function testPrint(): void

namespace Foo\Bar;

class Baz
{
}

CODE
, null, true);
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Preset/PresetRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testRegister(): void
public function testNotFoundPresetShouldThrowAnException(): void
{
$this->expectException(PresetNotFoundException::class);
$this->expectErrorMessage('Preset with given name [foo] is not defined.');
$this->expectExceptionMessage('Preset with given name [foo] is not defined.');

$registry = new PresetRegistry();

Expand Down
2 changes: 1 addition & 1 deletion tests/src/WorkersRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testRegisterWorker(): void
public function testRegisterWorkerWithExistsName(): void
{
$this->expectException(WorkersRegistryException::class);
$this->expectErrorMessage('Temporal worker with given name `foo` has already been registered.');
$this->expectExceptionMessage('Temporal worker with given name `foo` has already been registered.');

$registry = new WorkersRegistry(
$this->createMock(WorkerFactoryInterface::class),
Expand Down
Loading