Skip to content

Commit

Permalink
Add acceptance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Sep 16, 2024
1 parent 7a29763 commit 82f158d
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/Acceptance/App/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Temporal\Tests\Acceptance\App\Runtime\RRStarter;
use Temporal\Tests\Acceptance\App\Runtime\State;

abstract class TestCase extends \PHPUnit\Framework\TestCase
abstract class TestCase extends \Temporal\Tests\TestCase
{
protected function setUp(): void
{
Expand Down
174 changes: 174 additions & 0 deletions tests/Acceptance/Extra/Schedule/ScheduleUpdateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

declare(strict_types=1);

namespace Temporal\Tests\Acceptance\Extra\Update\DynamicUpdate;

use PHPUnit\Framework\Attributes\Test;
use Temporal\Client\Schedule\Action\StartWorkflowAction;
use Temporal\Client\Schedule\Schedule;
use Temporal\Client\Schedule\ScheduleOptions;
use Temporal\Client\Schedule\Spec\ScheduleSpec;
use Temporal\Client\Schedule\Update\ScheduleUpdate;
use Temporal\Client\Schedule\Update\ScheduleUpdateInput;
use Temporal\Client\ScheduleClientInterface;
use Temporal\DataConverter\EncodedCollection;
use Temporal\Tests\Acceptance\App\TestCase;

class ScheduleUpdateTest extends TestCase
{
#[Test]
public function searchAttributesClearViaUpdate(
ScheduleClientInterface $client,
): void
{
// Create a new schedule
$handle = $client->createSchedule(
Schedule::new()
->withAction(
StartWorkflowAction::new('TestWorkflow')
)->withSpec(
ScheduleSpec::new()
->withStartTime('+1 hour')
),
ScheduleOptions::new()
->withMemo(['memokey2' => 'memoval2'])
->withSearchAttributes(
EncodedCollection::fromValues([
'foo' => 'bar',
'bar' => 42,
])
)
);

try {
$description = $handle->describe();
self::assertEquals(2, $description->searchAttributes->count());

// Update the schedule search attribute by clearing them
$handle->update(function (ScheduleUpdateInput $input): ScheduleUpdate {
$schedule = $input->description->schedule;
return ScheduleUpdate::new($schedule)
->withSearchAttributes(EncodedCollection::empty());
});

sleep(1);
self::assertEquals(0, $handle->describe()->searchAttributes->count());
} finally {
$handle->delete();
}
}

#[Test]
public function searchAttributesAddViaUpdate(
ScheduleClientInterface $client,
): void
{
// Create a new schedule
$handle = $client->createSchedule(
Schedule::new()
->withAction(
StartWorkflowAction::new('TestWorkflow')
)->withSpec(
ScheduleSpec::new()
->withStartTime('+1 hour')
),
ScheduleOptions::new()
->withMemo(['memokey2' => 'memoval2'])
->withSearchAttributes(
EncodedCollection::fromValues([
'foo' => 'bar',
])
)
);

try {
$description = $handle->describe();
self::assertEquals(1, $description->searchAttributes->count());

// Update the schedule search attribute by clearing them
$handle->update(function (ScheduleUpdateInput $input): ScheduleUpdate {
$schedule = $input->description->schedule;
return ScheduleUpdate::new($schedule)
->withSearchAttributes($input->description->searchAttributes->withValue('bar', 69));
});

sleep(1);
self::assertEquals(2, $handle->describe()->searchAttributes->count());
self::assertSame(69, $handle->describe()->searchAttributes->getValue('bar'));
} finally {
$handle->delete();
}
}

#[Test]
public function update(
ScheduleClientInterface $client,
): void {
// Create a new schedule
$handle = $client->createSchedule(
Schedule::new()
->withAction(
StartWorkflowAction::new('TestWorkflow')
->withMemo(['memokey1' => 'memoval1'])
)->withSpec(
ScheduleSpec::new()
->withStartTime('+1 hour')
),
ScheduleOptions::new()
->withMemo(['memokey2' => 'memoval2'])
->withSearchAttributes(EncodedCollection::fromValues([
'foo' => 'bar',
'bar' => 42,
]))
);

try {
// Describe the schedule
$description = $handle->describe();
self::assertSame("memoval2", $description->memo->getValue("memokey2"));
self::assertEquals(2, $description->searchAttributes->count());

/** @var StartWorkflowAction $startWfAction */
$startWfAction = $description->schedule->action;
self::assertSame('memoval1', $startWfAction->memo->getValue("memokey1"));

// Add memo and update task timeout
$handle->update(function (ScheduleUpdateInput $input): ScheduleUpdate {
$schedule = $input->description->schedule;
/** @var StartWorkflowAction $action */
$action = $schedule->action;
$action = $action->withWorkflowTaskTimeout('7 minutes')
->withMemo(['memokey3' => 'memoval3']);
return ScheduleUpdate::new($schedule->withAction($action));
});

$description = $handle->describe();
self::assertInstanceOf(StartWorkflowAction::class, $description->schedule->action);
self::assertSame("memoval2", $description->memo->getValue("memokey2"));
$startWfAction = $description->schedule->action;
self::assertSame("memoval3", $startWfAction->memo->getValue("memokey3"));
$this->assertEqualIntervals(new \DateInterval('PT7M'), $startWfAction->workflowTaskTimeout);

// Update the schedule state
$expectedUpdateTime = $description->info->lastUpdateAt;
$handle->update(function (ScheduleUpdateInput $input): ScheduleUpdate {
$schedule = $input->description->schedule;
$schedule = $schedule->withState($schedule->state->withPaused(true));
return ScheduleUpdate::new($schedule);
});
$description = $handle->describe();
//
self::assertSame("memoval2", $description->memo->getValue("memokey2"));
$startWfAction = $description->schedule->action;
self::assertSame("memoval3", $startWfAction->memo->getValue("memokey3"));
//
self::assertNotEquals($expectedUpdateTime, $description->info->lastUpdateAt);
self::assertTrue($description->schedule->state->paused);
self::assertEquals(2, $description->searchAttributes->count());
self::assertSame('bar', $description->searchAttributes->getValue('foo'));
} finally {
$handle->delete();
}
}
}

0 comments on commit 82f158d

Please sign in to comment.