Skip to content

Commit

Permalink
Deprecate namespace in ScheduleOptions; use ClientOptions's namespace…
Browse files Browse the repository at this point in the history
… as fallback when a Schedule is created
  • Loading branch information
roxblnfk committed Jan 8, 2025
1 parent bf33938 commit 1099083
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 6 deletions.
11 changes: 11 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,23 @@
<code><![CDATA[$workflowType]]></code>
</PropertyNotSetInConstructor>
</file>
<file src="src/Client/Schedule/ScheduleOptions.php">
<DeprecatedProperty>
<code><![CDATA[$this->namespace]]></code>
</DeprecatedProperty>
</file>
<file src="src/Client/Schedule/Spec/ScheduleSpec.php">
<DeprecatedProperty>
<code><![CDATA[$this->excludeCalendarList]]></code>
<code><![CDATA[$this->excludeCalendarList]]></code>
</DeprecatedProperty>
</file>
<file src="src/Client/ScheduleClient.php">
<DeprecatedProperty>
<code><![CDATA[$options->namespace]]></code>
<code><![CDATA[$options->namespace]]></code>
</DeprecatedProperty>
</file>
<file src="src/Client/Update/UpdateHandle.php">
<PossiblyNullArgument>
<code><![CDATA[$result->getSuccess()]]></code>
Expand Down
13 changes: 10 additions & 3 deletions src/Client/Schedule/ScheduleOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,23 @@ final class ScheduleOptions
{
use CloneWith;

public readonly string $namespace;
public readonly bool $triggerImmediately;
/**
* @deprecated will be removed in the next major version.
*/
public readonly ?string $namespace;

/**
* @var list<BackfillPeriod>
*/
public readonly array $backfills;

public readonly bool $triggerImmediately;
public readonly EncodedCollection $memo;
public readonly EncodedCollection $searchAttributes;

private function __construct()
{
$this->namespace = 'default';
$this->namespace = null;
$this->triggerImmediately = false;
$this->backfills = [];
$this->memo = EncodedCollection::empty();
Expand All @@ -41,6 +44,10 @@ public static function new(): self
return new self();
}

/**
* @deprecated Configure the namespace on the {@see \Temporal\Client\ClientOptions} instead
* when creating the {@see \Temporal\Client\ScheduleClient}.
*/
public function withNamespace(string $namespace): self
{
/** @see self::$namespace */
Expand Down
4 changes: 2 additions & 2 deletions src/Client/ScheduleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function createSchedule(
$request = new CreateScheduleRequest();
$request
->setRequestId(Uuid::v4())
->setNamespace($options->namespace)
->setNamespace($options->namespace ?? $this->clientOptions->namespace)
->setScheduleId($scheduleId)
->setIdentity($this->clientOptions->identity);

Expand Down Expand Up @@ -123,7 +123,7 @@ public function createSchedule(
$this->converter,
$this->marshaller,
$this->protoConverter,
$options->namespace,
$options->namespace ?? $this->clientOptions->namespace,
$scheduleId,
);
}
Expand Down
54 changes: 54 additions & 0 deletions tests/Unit/Schedule/ScheduleClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Temporal\Client\Schedule\Policy\ScheduleOverlapPolicy;
use Temporal\Client\Schedule\Policy\SchedulePolicies;
use Temporal\Client\Schedule\Schedule;
use Temporal\Client\Schedule\ScheduleOptions;
use Temporal\Client\Schedule\Spec\CalendarSpec;
use Temporal\Client\Schedule\Spec\Range;
use Temporal\Client\Schedule\Spec\ScheduleSpec;
Expand Down Expand Up @@ -63,6 +64,59 @@ public function testCreateSchedule(): void
);
}

public function testCreateScheduleNamespaceFromOptions(): void
{
$testContext = new class {
public CreateScheduleRequest $request;
};
// Prepare mocks
$clientMock = $this->createMock(ServiceClientInterface::class);
$clientMock->expects($this->once())
->method('CreateSchedule')
->with($this->callback(fn(CreateScheduleRequest $request) => $testContext->request = $request or true))
->willReturn((new CreateScheduleResponse())->setConflictToken('test-conflict-token'));
$scheduleClient = $this->createScheduleClient(
client: $clientMock,
);
$scheduleDto = $this->getScheduleDto();

$scheduleClient->createSchedule(
$scheduleDto,
options: ScheduleOptions::new()->withNamespace('test-namespace'),
scheduleId: 'test-id',
);

$this->assertTrue(isset($testContext->request));
$this->assertSame('test-namespace', $testContext->request->getNamespace());
}

public function testCreateScheduleNamespaceFromServiceClient(): void
{
$testContext = new class {
public CreateScheduleRequest $request;
};
// Prepare mocks
$clientMock = $this->createMock(ServiceClientInterface::class);
$clientMock->expects($this->once())
->method('CreateSchedule')
->with($this->callback(fn(CreateScheduleRequest $request) => $testContext->request = $request or true))
->willReturn((new CreateScheduleResponse())->setConflictToken('test-conflict-token'));
$scheduleClient = $this->createScheduleClient(
client: $clientMock,
clientOptions: (new ClientOptions())->withNamespace('test-namespace'),
);
$scheduleDto = $this->getScheduleDto();

$scheduleClient->createSchedule(
$scheduleDto,
options: ScheduleOptions::new(),
scheduleId: 'test-id',
);

$this->assertTrue(isset($testContext->request));
$this->assertSame('test-namespace', $testContext->request->getNamespace());
}

public function testCreateScheduleWithoutWorkflowId(): void
{
$testContext = new class {
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Schedule/ScheduleOptionsTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testWithNamespace(): void
$new = $init->withNamespace('foo');

$this->assertNotSame($init, $new, 'immutable method clones object');
$this->assertSame('default', $init->namespace, 'default value was not changed');
$this->assertNull($init->namespace, 'default value was not changed');
$this->assertSame('foo', $new->namespace);
}

Expand Down

0 comments on commit 1099083

Please sign in to comment.