1
0
Fork 0
mirror of synced 2024-09-29 17:01:37 +13:00

Add tests for scheduled functions

This commit is contained in:
Khushboo Verma 2024-05-20 17:44:48 +05:30
parent fd7228984b
commit ccae83c8a8
2 changed files with 39 additions and 1 deletions

View file

@ -61,7 +61,7 @@ class FunctionsCustomClientTest extends Scope
'users.*.create', 'users.*.create',
'users.*.delete', 'users.*.delete',
], ],
'schedule' => '0 0 1 1 *', 'schedule' => '* * * * *', // execute every minute
'timeout' => 10, 'timeout' => 10,
]); ]);
@ -163,6 +163,24 @@ class FunctionsCustomClientTest extends Scope
$this->assertEquals(202, $execution['headers']['status-code']); $this->assertEquals(202, $execution['headers']['status-code']);
// Wait for the first scheduled execution to be created
sleep(65);
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $function['body']['$id'] . '/executions', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]);
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertCount(2, $executions['body']['executions']);
$this->assertIsArray($executions['body']['executions']);
$this->assertEquals($executions['body']['executions'][1]['trigger'], 'schedule');
$this->assertEquals($executions['body']['executions'][1]['status'], 'completed');
$this->assertEquals($executions['body']['executions'][1]['responseStatusCode'], 200);
$this->assertEquals($executions['body']['executions'][1]['responseBody'], '');
$this->assertEquals($executions['body']['executions'][1]['logs'], '');
// Cleanup : Delete function // Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $function['body']['$id'], [ $response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $function['body']['$id'], [
'content-type' => 'application/json', 'content-type' => 'application/json',

View file

@ -914,12 +914,14 @@ class FunctionsCustomServerTest extends Scope
'runtime' => $name, 'runtime' => $name,
'entrypoint' => $entrypoint, 'entrypoint' => $entrypoint,
'events' => [], 'events' => [],
'schedule' => '* * * * *', // execute every minute
'timeout' => $timeout, 'timeout' => $timeout,
]); ]);
$functionId = $function['body']['$id'] ?? ''; $functionId = $function['body']['$id'] ?? '';
$this->assertEquals(201, $function['headers']['status-code']); $this->assertEquals(201, $function['headers']['status-code']);
$this->assertEquals('* * * * *', $function['body']['schedule']);
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([ $deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'multipart/form-data', 'content-type' => 'multipart/form-data',
@ -986,6 +988,24 @@ class FunctionsCustomServerTest extends Scope
$this->assertEquals($executions['body']['executions'][0]['logs'], ''); $this->assertEquals($executions['body']['executions'][0]['logs'], '');
$this->assertStringContainsString('timed out', $executions['body']['executions'][0]['errors']); $this->assertStringContainsString('timed out', $executions['body']['executions'][0]['errors']);
sleep(70); //wait for scheduled execution to be created and time out
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
$this->assertEquals(200, $executions['headers']['status-code']);
$this->assertCount(2, $executions['body']['executions']);
$this->assertIsArray($executions['body']['executions']);
$this->assertEquals($executions['body']['executions'][1]['trigger'], 'schedule');
$this->assertEquals($executions['body']['executions'][1]['status'], 'failed');
$this->assertEquals($executions['body']['executions'][1]['responseStatusCode'], 500);
$this->assertLessThan(20, $executions['body']['executions'][1]['duration']);
$this->assertEquals($executions['body']['executions'][1]['responseBody'], '');
$this->assertEquals($executions['body']['executions'][1]['logs'], '');
$this->assertStringContainsString('timed out', $executions['body']['executions'][1]['errors']);
// Cleanup : Delete function // Cleanup : Delete function
$response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [ $response = $this->client->call(Client::METHOD_DELETE, '/functions/' . $functionId, [
'content-type' => 'application/json', 'content-type' => 'application/json',