fix: flakyness

This commit is contained in:
loks0n
2024-09-19 14:17:38 +01:00
parent 496eb5bd12
commit af509f3479
3 changed files with 51 additions and 61 deletions
+29 -49
View File
@@ -21,27 +21,26 @@ trait FunctionsBase
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $params);
$functionId = $function['body']['$id'];
$this->assertEquals($function['headers']['status-code'], 201, 'Setup function failed with status code: ' . $function['headers']['status-code'] . ' and response: ' . json_encode($function['body']));
$this->assertEquals($function['headers']['status-code'], 201);
$functionId = $function['body']['$id'];
return $functionId;
}
protected function setupDeployment(string $functionId, mixed $params): string
{
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'application/json',
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $params);
$this->assertEquals($deployment['headers']['status-code'], 202, 'Setup deployment failed with status code: ' . $deployment['headers']['status-code'] . ' and response: ' . json_encode($deployment['body']));
$deploymentId = $deployment['body']['$id'];
$this->assertEquals($deployment['headers']['status-code'], 202);
$this->assertEventually(function () use ($functionId, $deploymentId) {
$deployment = $this->getDeployment($functionId, $deploymentId);
$this->assertEquals('completed', $deployment['body']['status']);
}, 200000, 500);
}, 50000, 500);
return $deploymentId;
}
@@ -57,40 +56,27 @@ trait FunctionsBase
$this->assertEquals($function['headers']['status-code'], 204);
}
protected function createFunction(mixed $params)
protected function createFunction(mixed $params): mixed
{
$function = $this->client->call(Client::METHOD_POST, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $params);
$functionId = $function['body']['$id'];
if (empty($variables)) {
return $function;
}
$function = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()));
return $function;
}
protected function createVariable($functionId, $key, $value)
protected function createVariable(string $functionId, mixed $params): mixed
{
$variable = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/variables', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), [
'key' => $key,
'value' => $value
]);
], $this->getHeaders()), $params);
return $variable;
}
protected function getFunction($functionId)
protected function getFunction(string $functionId): mixed
{
$function = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId, array_merge([
'content-type' => 'application/json',
@@ -100,7 +86,7 @@ trait FunctionsBase
return $function;
}
protected function getDeployment($functionId, $deploymentId)
protected function getDeployment(string $functionId, string $deploymentId): mixed
{
$deployment = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments/' . $deploymentId, array_merge([
'content-type' => 'application/json',
@@ -120,12 +106,22 @@ trait FunctionsBase
return $execution;
}
protected function listFunctions($params = [])
{
$functions = $this->client->call(Client::METHOD_GET, '/functions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $params);
return $functions;
}
protected function listDeployments($functionId, $params = [])
{
$deployments = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders(), $params));
], $this->getHeaders()), $params);
return $deployments;
}
@@ -135,19 +131,17 @@ trait FunctionsBase
$executions = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/executions', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders(), $params));
], $this->getHeaders()), $params);
return $executions;
}
protected function packageFunction(string $folder = 'php')
protected function packageFunction(string $folder = 'php'): CURLFile
{
$code = realpath(__DIR__ . '/../../../resources/functions') . "/$folder/code.tar.gz";
Console::execute('cd ' . realpath(__DIR__ . "/../../../resources/functions") . "/$folder && tar --exclude code.tar.gz -czf code.tar.gz .", '', $this->stdout, $this->stderr);
if (!file_exists($code)) {
throw new \Exception('Failed to create code package. ' . $code . ' does not exist.');
Console::execute('cd ' . realpath(__DIR__ . "/../../../resources/functions") . "/$folder && tar --exclude code.tar.gz -czf code.tar.gz .", '', $this->stdout, $this->stderr);
}
if (filesize($code) > 1024 * 1024 * 5) {
throw new \Exception('Code package is too large. Use the chunked upload method instead.');
@@ -158,36 +152,22 @@ trait FunctionsBase
protected function createDeployment(
string $functionId,
mixed $params,
bool $cli = false,
bool $admin = false
mixed $params
) {
$authHeaders = $this->getHeaders();
if ($admin) {
$authHeaders = [
'x-appwrite-key' => $this->getProject()['apiKey'],
];
}
if ($cli) {
$authHeaders[] = 'x-sdk-language: cli';
}
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', [
$deployment = $this->client->call(Client::METHOD_POST, '/functions/' . $functionId . '/deployments', array_merge([
'content-type' => 'multipart/form-data',
'x-appwrite-project' => $this->getProject()['$id'],
], $params);
], $this->getHeaders()), $params);
return $deployment;
}
protected function getFunctionUsage($functionId, $params)
protected function getFunctionUsage(string $functionId, mixed $params): mixed
{
$usage = $this->client->call(Client::METHOD_GET, '/functions/' . $functionId . '/usage', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders(), $params));
], $this->getHeaders()), $params);
return $usage;
}
@@ -105,8 +105,10 @@ class FunctionsConsoleClientTest extends Scope
*/
$variable = $this->createVariable(
$data['functionId'],
'APP_TEST',
'TESTINGVALUE'
[
'key' => 'APP_TEST',
'value' => 'TESTINGVALUE'
]
);
$variableId = $variable['body']['$id'];
$this->assertEquals(201, $variable['headers']['status-code']);
@@ -117,24 +119,30 @@ class FunctionsConsoleClientTest extends Scope
// Test for duplicate key
$variable = $this->createVariable(
$data['functionId'],
'APP_TEST',
'ANOTHER_TESTINGVALUE'
[
'key' => 'APP_TEST',
'value' => 'ANOTHERTESTINGVALUE'
]
);
$this->assertEquals(409, $variable['headers']['status-code']);
// Test for invalid key
$variable = $this->createVariable(
$data['functionId'],
str_repeat("A", 256),
'TESTINGVALUE'
[
'key' => str_repeat("A", 256),
'value' => 'TESTINGVALUE'
]
);
$this->assertEquals(400, $variable['headers']['status-code']);
// Test for invalid value
$variable = $this->createVariable(
$data['functionId'],
'LONGKEY',
str_repeat("#", 8193)
[
'key' => 'LONGKEY',
'value' => str_repeat("#", 8193),
]
);
$this->assertEquals(400, $variable['headers']['status-code']);
@@ -15,7 +15,7 @@ class FunctionsCustomClientTest extends Scope
use ProjectCustom;
use SideClient;
public function testCreate()
public function testCreateFunction()
{
/**
* Test for FAILURE
@@ -80,7 +80,7 @@ class FunctionsCustomClientTest extends Scope
$this->assertNotEmpty($asyncExecution['logs']);
$this->assertNotEmpty($asyncExecution['errors']);
$this->assertGreaterThan(0, $asyncExecution['duration']);
}, 100000, 250);
}, 10000, 250);
$this->cleanupFunction($functionId);
}
@@ -106,7 +106,7 @@ class FunctionsCustomClientTest extends Scope
// Schedule execution for the future
\date_default_timezone_set('UTC');
$futureTime = (new \DateTime())->add(new \DateInterval('PT30S')); // 30 seconds from now
$futureTime = (new \DateTime())->add(new \DateInterval('PT15S')); // 15 seconds from now
$futureTime->setTime($futureTime->format('H'), $futureTime->format('i'), 0, 0);
$execution = $this->createExecution($functionId, [
@@ -126,6 +126,8 @@ class FunctionsCustomClientTest extends Scope
$this->assertEquals('/custom-path', $execution['body']['requestPath']);
$this->assertCount(0, $execution['body']['requestHeaders']);
\sleep(15);
$this->assertEventually(function () use ($functionId, $executionId) {
$execution = $this->getExecution($functionId, $executionId);
$this->assertEquals('completed', $execution['body']['status']);
@@ -141,7 +143,7 @@ class FunctionsCustomClientTest extends Scope
$this->assertStringContainsString('user-is-' . $this->getUser()['$id'], $execution['body']['logs']);
$this->assertStringContainsString('jwt-is-valid', $execution['body']['logs']);
$this->assertGreaterThan(0, $execution['body']['duration']);
}, 40000, 2500);
}, 10000, 250);
/* Test for FAILURE */
// Schedule synchronous execution