From f02bf6ac5a2e5d3be03f13f128e5ca70ec9a513d Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:03:24 +0100 Subject: [PATCH 01/10] feat: use coroutines delay system --- .../Platform/Tasks/ScheduleExecutions.php | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index 2fdbd98da3..e7364c7a4b 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -26,6 +26,7 @@ class ScheduleExecutions extends ScheduleBase $queue = $pools->get('queue')->pop(); $connection = $queue->getResource(); $queueForFunctions = new Func($connection); + $intervalEnd = (new \DateTime())->modify('+' . self::ENQUEUE_TIMER . ' seconds'); foreach ($this->schedules as $schedule) { if (!$schedule['active']) { @@ -38,25 +39,29 @@ class ScheduleExecutions extends ScheduleBase continue; } - $now = new \DateTime(); $scheduledAt = new \DateTime($schedule['schedule']); - - if ($scheduledAt > $now) { + if ($scheduledAt <= $intervalEnd) { continue; } - $queueForFunctions - ->setType('schedule') - // Set functionId instead of function as we don't have $dbForProject - // TODO: Refactor to use function instead of functionId - ->setFunctionId($schedule['resource']['functionId']) - ->setExecution($schedule['resource']) - ->setMethod($schedule['data']['method'] ?? 'POST') - ->setPath($schedule['data']['path'] ?? '/') - ->setHeaders($schedule['data']['headers'] ?? []) - ->setBody($schedule['data']['body'] ?? '') - ->setProject($schedule['project']) - ->trigger(); + $delay = $scheduledAt->getTimestamp() - (new \DateTime())->getTimestamp(); + + \go(function () use ($queueForFunctions, $schedule, $delay) { + \sleep($delay); + + $queueForFunctions + ->setType('schedule') + // Set functionId instead of function as we don't have $dbForProject + // TODO: Refactor to use function instead of functionId + ->setFunctionId($schedule['resource']['functionId']) + ->setExecution($schedule['resource']) + ->setMethod($schedule['data']['method'] ?? 'POST') + ->setPath($schedule['data']['path'] ?? '/') + ->setHeaders($schedule['data']['headers'] ?? []) + ->setBody($schedule['data']['body'] ?? '') + ->setProject($schedule['project']) + ->trigger(); + }); $dbForConsole->deleteDocument( 'schedules', From d0a0329cc8595684fe89db008b3fe4f8a48d334b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Thu, 18 Jul 2024 13:12:39 +0100 Subject: [PATCH 02/10] test: smaller sleep --- tests/e2e/Services/Functions/FunctionsCustomClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index bf969d388a..e3817c1b46 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -289,7 +289,7 @@ class FunctionsCustomClientTest extends Scope $executionId = $execution['body']['$id']; - sleep(20); + sleep(15); $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions/' . $executionId, [ 'content-type' => 'application/json', From 5a56131efa480d1eff8ad43f686ef0fe3e06d1cf Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:22:50 +0100 Subject: [PATCH 03/10] chore: binyamin review --- src/Appwrite/Platform/Tasks/ScheduleExecutions.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php index e7364c7a4b..c5f9b40d15 100644 --- a/src/Appwrite/Platform/Tasks/ScheduleExecutions.php +++ b/src/Appwrite/Platform/Tasks/ScheduleExecutions.php @@ -3,6 +3,7 @@ namespace Appwrite\Platform\Tasks; use Appwrite\Event\Func; +use Swoole\Coroutine as Co; use Utopia\Database\Database; use Utopia\Pools\Group; @@ -46,8 +47,9 @@ class ScheduleExecutions extends ScheduleBase $delay = $scheduledAt->getTimestamp() - (new \DateTime())->getTimestamp(); + \go(function () use ($queueForFunctions, $schedule, $delay) { - \sleep($delay); + Co::sleep($delay); $queueForFunctions ->setType('schedule') From 76aaa50ad6f251d18339eea72ef7be33e8721a9b Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 19 Jul 2024 10:54:20 +0100 Subject: [PATCH 04/10] chore: matej suggested test --- .../Functions/FunctionsCustomClientTest.php | 17 +++++++++++++---- tests/resources/functions/php-time/index.php | 7 +++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 tests/resources/functions/php-time/index.php diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index e3817c1b46..3b5adf1abd 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -220,7 +220,7 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals(201, $function['headers']['status-code']); - $folder = 'php'; + $folder = 'php-time'; $code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz"; $this->packageCode($folder); @@ -268,14 +268,15 @@ class FunctionsCustomClientTest extends Scope // Schedule execution for the future \date_default_timezone_set('UTC'); - $futureTime = (new \DateTime())->add(new \DateInterval('PT10S'))->format('Y-m-d H:i:s'); + $futureTime = (new \DateTime())->add(new \DateInterval('PT10S')); + $futureTimeString = $futureTime->format('Y-m-d H:i:s'); $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'async' => true, - 'scheduledAt' => $futureTime, + 'scheduledAt' => $futureTimeString, 'path' => '/custom', 'method' => 'GET', 'body' => 'hello', @@ -289,7 +290,7 @@ class FunctionsCustomClientTest extends Scope $executionId = $execution['body']['$id']; - sleep(15); + sleep(13); $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions/' . $executionId, [ 'content-type' => 'application/json', @@ -304,6 +305,14 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals('GET', $execution['body']['requestMethod']); $this->assertGreaterThan(0, $execution['body']['duration']); + // Assert that the execution was completed within 3 seconds of the scheduled time + + $output = json_decode($execution['body']['responseBody'], true); + $this->assertArrayHasKey('time', $output); + $this->assertGreaterThan($output['time'], $futureTime->getTimestamp()); + $this->assertLessThan($output['time'], $futureTime->getTimestamp() + 3); + + /* Test for FAILURE */ // Schedule synchronous execution diff --git a/tests/resources/functions/php-time/index.php b/tests/resources/functions/php-time/index.php new file mode 100644 index 0000000000..9a3433d55b --- /dev/null +++ b/tests/resources/functions/php-time/index.php @@ -0,0 +1,7 @@ +res->json([ + 'time' => \time(), + ]); +}; From de1d205341e47d3cc3161b1f569f5c68129a3208 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 31 Jul 2024 20:43:28 +0000 Subject: [PATCH 05/10] chore: update changes from merge --- .../Functions/FunctionsCustomClientTest.php | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 85630c7fd8..676f93b113 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -234,8 +234,8 @@ class FunctionsCustomClientTest extends Scope // Schedule execution for the future \date_default_timezone_set('UTC'); - $futureTime = (new \DateTime())->add(new \DateInterval('PT10S'))->format('Y-m-d H:i:s'); - $futureTimeIso = DateTime::formatTz($futureTime); + $futureTime = (new \DateTime())->add(new \DateInterval('PT10S')); + $futureTimeString = $futureTime->format('Y-m-d H:i:s'); $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([ 'content-type' => 'application/json', @@ -257,21 +257,12 @@ class FunctionsCustomClientTest extends Scope $executionId = $execution['body']['$id']; - // List executions and ensure it has schedule date - $response = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions', [ - ]); - - $this->assertEquals(200, $response['headers']['status-code']); - $this->assertGreaterThan(0, \count($response['body']['executions'])); - $recentExecution = $response['body']['executions'][0]; - $this->assertEquals($executionId, $recentExecution['$id']); - $this->assertEquals($futureTimeIso, $recentExecution['scheduledAt']); - - sleep(20); + sleep(13); $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions/' . $executionId, [ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], ]); $this->assertEquals(200, $execution['headers']['status-code']); From cf4030095f1d29271745212cf30124ce39a7d2a8 Mon Sep 17 00:00:00 2001 From: Christy Jacob Date: Wed, 31 Jul 2024 20:48:59 +0000 Subject: [PATCH 06/10] chore: linter --- composer.lock | 34 +++++++++---------- .../Functions/FunctionsCustomClientTest.php | 1 - 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/composer.lock b/composer.lock index 68f109ed72..e058bfdb33 100644 --- a/composer.lock +++ b/composer.lock @@ -1721,16 +1721,16 @@ }, { "name": "utopia-php/database", - "version": "0.50.0", + "version": "0.50.2", "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "ce3eaccb2f3bbd34b2b97419836fec633b26b8f7" + "reference": "c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/ce3eaccb2f3bbd34b2b97419836fec633b26b8f7", - "reference": "ce3eaccb2f3bbd34b2b97419836fec633b26b8f7", + "url": "https://api.github.com/repos/utopia-php/database/zipball/c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa", + "reference": "c712d1f6c8ec37886a7a1ad4d60a8cd75dec00aa", "shasum": "" }, "require": { @@ -1771,9 +1771,9 @@ ], "support": { "issues": "https://github.com/utopia-php/database/issues", - "source": "https://github.com/utopia-php/database/tree/0.50.0" + "source": "https://github.com/utopia-php/database/tree/0.50.2" }, - "time": "2024-06-21T03:21:42+00:00" + "time": "2024-07-31T10:12:19+00:00" }, { "name": "utopia-php/domains", @@ -2990,16 +2990,16 @@ "packages-dev": [ { "name": "appwrite/sdk-generator", - "version": "0.39.3", + "version": "0.39.4", "source": { "type": "git", "url": "https://github.com/appwrite/sdk-generator.git", - "reference": "16142d88270e368030d7956cadf2d7816413f8c4" + "reference": "501b92d73ae55e0f880ed00f57bc64a54d0ce137" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/16142d88270e368030d7956cadf2d7816413f8c4", - "reference": "16142d88270e368030d7956cadf2d7816413f8c4", + "url": "https://api.github.com/repos/appwrite/sdk-generator/zipball/501b92d73ae55e0f880ed00f57bc64a54d0ce137", + "reference": "501b92d73ae55e0f880ed00f57bc64a54d0ce137", "shasum": "" }, "require": { @@ -3035,9 +3035,9 @@ "description": "Appwrite PHP library for generating API SDKs for multiple programming languages and platforms", "support": { "issues": "https://github.com/appwrite/sdk-generator/issues", - "source": "https://github.com/appwrite/sdk-generator/tree/0.39.3" + "source": "https://github.com/appwrite/sdk-generator/tree/0.39.4" }, - "time": "2024-07-12T15:29:48+00:00" + "time": "2024-07-26T22:34:10+00:00" }, { "name": "doctrine/deprecations", @@ -3158,16 +3158,16 @@ }, { "name": "laravel/pint", - "version": "v1.16.2", + "version": "v1.17.0", "source": { "type": "git", "url": "https://github.com/laravel/pint.git", - "reference": "51f1ba679a6afe0315621ad143d788bd7ded0eca" + "reference": "4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/laravel/pint/zipball/51f1ba679a6afe0315621ad143d788bd7ded0eca", - "reference": "51f1ba679a6afe0315621ad143d788bd7ded0eca", + "url": "https://api.github.com/repos/laravel/pint/zipball/4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5", + "reference": "4dba80c1de4b81dc4c4fb10ea6f4781495eb29f5", "shasum": "" }, "require": { @@ -3220,7 +3220,7 @@ "issues": "https://github.com/laravel/pint/issues", "source": "https://github.com/laravel/pint" }, - "time": "2024-07-09T15:58:08+00:00" + "time": "2024-07-23T16:40:20+00:00" }, { "name": "matthiasmullie/minify", diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 676f93b113..5b59094409 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -9,7 +9,6 @@ use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideClient; use Utopia\Config\Config; -use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Role; From b87cabfc4949490e86881d8d8af12c027189b9c2 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:49:03 +0100 Subject: [PATCH 07/10] test: fix unused var --- tests/e2e/Services/Functions/FunctionsCustomClientTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 5b59094409..870c1b0bff 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -252,10 +252,10 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals(202, $execution['headers']['status-code']); $this->assertEquals('scheduled', $execution['body']['status']); - $this->assertEquals($futureTimeIso, $execution['body']['scheduledAt']); $executionId = $execution['body']['$id']; + // 10 seconds delay, then allow 3 seconds for cold start sleep(13); $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions/' . $executionId, [ @@ -270,10 +270,8 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals('/custom', $execution['body']['requestPath']); $this->assertEquals('GET', $execution['body']['requestMethod']); $this->assertGreaterThan(0, $execution['body']['duration']); - $this->assertEquals($futureTimeIso, $execution['body']['scheduledAt']); - - // Assert that the execution was completed within 3 seconds of the scheduled time + // Assert execution completed within 3 seconds of schedule $output = json_decode($execution['body']['responseBody'], true); $this->assertArrayHasKey('time', $output); $this->assertGreaterThan($output['time'], $futureTime->getTimestamp()); From 30132e7e9346a2a8fb96d1e58a0bb5782c5298d6 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 9 Aug 2024 10:43:08 +0100 Subject: [PATCH 08/10] test: fix body check, not possible --- .../Functions/FunctionsCustomClientTest.php | 60 ++++++++----------- tests/resources/functions/php-time/index.php | 7 --- 2 files changed, 24 insertions(+), 43 deletions(-) delete mode 100644 tests/resources/functions/php-time/index.php diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 870c1b0bff..e3c5a8a19c 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -191,19 +191,15 @@ class FunctionsCustomClientTest extends Scope ], [ 'functionId' => ID::unique(), 'name' => 'Test', - 'execute' => [Role::user($this->getUser()['$id'])->toString()], + 'execute' => [Role::any()->toString()], 'runtime' => 'php-8.0', 'entrypoint' => 'index.php', - 'events' => [ - 'users.*.create', - 'users.*.delete', - ], 'timeout' => 10, ]); $this->assertEquals(201, $function['headers']['status-code']); - $folder = 'php-time'; + $folder = 'php'; $code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz"; $this->packageCode($folder); @@ -216,20 +212,10 @@ class FunctionsCustomClientTest extends Scope 'code' => new CURLFile($code, 'application/x-gzip', \basename($code)), 'activate' => true ]); - $deploymentId = $deployment['body']['$id'] ?? ''; - $this->assertEquals(202, $deployment['headers']['status-code']); - $this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId); - - $function = $this->client->call(Client::METHOD_PATCH, '/functions/' . $function['body']['$id'] . '/deployments/' . $deploymentId, [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ], []); - - $this->assertEquals(200, $function['headers']['status-code']); + $this->awaitDeploymentIsBuilt($function['body']['$id'], $deploymentId, true); // Schedule execution for the future \date_default_timezone_set('UTC'); @@ -243,11 +229,7 @@ class FunctionsCustomClientTest extends Scope 'async' => true, 'scheduledAt' => $futureTimeString, 'path' => '/custom', - 'method' => 'GET', - 'body' => 'hello', - 'headers' => [ - 'content-type' => 'application/plain', - ], + 'method' => 'GET' ]); $this->assertEquals(202, $execution['headers']['status-code']); @@ -255,14 +237,27 @@ class FunctionsCustomClientTest extends Scope $executionId = $execution['body']['$id']; - // 10 seconds delay, then allow 3 seconds for cold start - sleep(13); + // 10 seconds delay + sleep(10); - $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions/' . $executionId, [ - 'content-type' => 'application/json', - 'x-appwrite-project' => $this->getProject()['$id'], - 'x-appwrite-key' => $this->getProject()['apiKey'], - ]); + $start = \microtime(true); + while (true) { + $execution = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions/' . $executionId, [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]); + + if ($execution['body']['status'] === 'completed') { + break; + } + + if (\microtime(true) - $start > 4) { + $this->fail('Execution did not complete within 4 seconds of schedule'); + } + + usleep(500000); + } $this->assertEquals(200, $execution['headers']['status-code']); $this->assertEquals(200, $execution['body']['responseStatusCode']); @@ -271,13 +266,6 @@ class FunctionsCustomClientTest extends Scope $this->assertEquals('GET', $execution['body']['requestMethod']); $this->assertGreaterThan(0, $execution['body']['duration']); - // Assert execution completed within 3 seconds of schedule - $output = json_decode($execution['body']['responseBody'], true); - $this->assertArrayHasKey('time', $output); - $this->assertGreaterThan($output['time'], $futureTime->getTimestamp()); - $this->assertLessThan($output['time'], $futureTime->getTimestamp() + 3); - - /* Test for FAILURE */ // Schedule synchronous execution diff --git a/tests/resources/functions/php-time/index.php b/tests/resources/functions/php-time/index.php deleted file mode 100644 index 9a3433d55b..0000000000 --- a/tests/resources/functions/php-time/index.php +++ /dev/null @@ -1,7 +0,0 @@ -res->json([ - 'time' => \time(), - ]); -}; From 464e1e247c2dbeaa53c6b001d5c6d351957cc4fe Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 9 Aug 2024 11:30:01 +0100 Subject: [PATCH 09/10] test: fix expect 400 --- tests/e2e/Services/Functions/FunctionsCustomClientTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index e3c5a8a19c..05987f07a4 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -4,6 +4,7 @@ namespace Tests\E2E\Services\Functions; use Appwrite\Tests\Retry; use CURLFile; +use DateTime; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -220,14 +221,13 @@ class FunctionsCustomClientTest extends Scope // Schedule execution for the future \date_default_timezone_set('UTC'); $futureTime = (new \DateTime())->add(new \DateInterval('PT10S')); - $futureTimeString = $futureTime->format('Y-m-d H:i:s'); $execution = $this->client->call(Client::METHOD_POST, '/functions/' . $function['body']['$id'] . '/executions', array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'async' => true, - 'scheduledAt' => $futureTimeString, + 'scheduledAt' => $futureTime->format(DateTime::ATOM), 'path' => '/custom', 'method' => 'GET' ]); @@ -275,7 +275,7 @@ class FunctionsCustomClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'async' => false, - 'scheduledAt' => $futureTime, + 'scheduledAt' => $futureTime->format(DateTime::ATOM), ]); $this->assertEquals(400, $execution['headers']['status-code']); From 0e773fefdfaa7be110fe6360010cd76d3340ff23 Mon Sep 17 00:00:00 2001 From: loks0n <22452787+loks0n@users.noreply.github.com> Date: Fri, 9 Aug 2024 13:10:07 +0100 Subject: [PATCH 10/10] feat: improve test --- .../Functions/FunctionsCustomClientTest.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php index 592697e9ec..eb2a1a1df9 100644 --- a/tests/e2e/Services/Functions/FunctionsCustomClientTest.php +++ b/tests/e2e/Services/Functions/FunctionsCustomClientTest.php @@ -4,7 +4,6 @@ namespace Tests\E2E\Services\Functions; use Appwrite\Tests\Retry; use CURLFile; -use DateTime; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -192,7 +191,7 @@ class FunctionsCustomClientTest extends Scope ], [ 'functionId' => ID::unique(), 'name' => 'Test', - 'execute' => [Role::any()->toString()], + 'execute' => [Role::user($this->getUser()['$id'])->toString()], 'runtime' => 'php-8.0', 'entrypoint' => 'index.php', 'timeout' => 10, @@ -227,7 +226,7 @@ class FunctionsCustomClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'async' => true, - 'scheduledAt' => $futureTime->format(DateTime::ATOM), + 'scheduledAt' => $futureTime->format(\DateTime::ATOM), 'path' => '/custom', 'method' => 'GET' ]); @@ -237,7 +236,6 @@ class FunctionsCustomClientTest extends Scope $executionId = $execution['body']['$id']; - // 10 seconds delay sleep(10); $start = \microtime(true); @@ -252,11 +250,11 @@ class FunctionsCustomClientTest extends Scope break; } - if (\microtime(true) - $start > 4) { - $this->fail('Execution did not complete within 4 seconds of schedule'); + if (\microtime(true) - $start > 5) { + $this->fail('Execution did not complete within 5 seconds of schedule'); } - usleep(500000); + usleep(500000); // 0.5 seconds } $this->assertEquals(200, $execution['headers']['status-code']); @@ -275,7 +273,7 @@ class FunctionsCustomClientTest extends Scope 'x-appwrite-project' => $this->getProject()['$id'], ], $this->getHeaders()), [ 'async' => false, - 'scheduledAt' => $futureTime->format(DateTime::ATOM), + 'scheduledAt' => $futureTime->format(\DateTime::ATOM), ]); $this->assertEquals(400, $execution['headers']['status-code']);