fix: flaky cli test (#12038)

This commit is contained in:
Michal Klos
2026-02-23 12:31:12 +01:00
committed by GitHub
parent 0d690c4b8b
commit 4f4fb4a3fb
2 changed files with 48 additions and 2 deletions

View File

@@ -562,6 +562,51 @@ class CliContext implements Context {
}
}
/**
* @Given the administrator has waited until the file :filename has finished processing
*
* @param string $filename
*
* @return void
* @throws JsonException
*/
public function theAdministratorHasWaitedUntilFileHasFinishedProcessing(string $filename): void {
$timeout = 30; // seconds
$interval = 1; // seconds
$startTime = \time();
// Poll until file is no longer in --processing list (allows time for state transitions).
// Replaces fixed wait that assumed deterministic timing.
while (true) {
$this->theAdministratorListsAllTheUploadSessions("processing");
$this->featureContext->theHTTPStatusCodeShouldBe(200);
$responseArray = $this->getJSONDecodedCliMessage($this->featureContext->getResponse());
$found = false;
foreach ($responseArray as $item) {
if (isset($item->filename) && $item->filename === $filename) {
$found = true;
break;
}
}
if (!$found) {
// File is no longer in processing list - abort completed and state updated
return;
}
$elapsed = \time() - $startTime;
if ($elapsed >= $timeout) {
Assert::fail(
"Timeout after {$timeout}s: file '{$filename}' is still in processing upload sessions list. " .
"Virus scan + abort may not have completed within timeout period.",
);
}
\sleep($interval);
}
}
/**
* @param ResponseInterface $response
*
@@ -575,7 +620,7 @@ class CliContext implements Context {
// Example Output: "INFO memory is not limited, skipping package=github.com/KimMachineGun/automemlimit/memlimit [{<output-json>}]"
// So, only extracting the array of output json from the message
\preg_match('/(\[.*\])/', $responseBody["message"], $matches);
return \json_decode($matches[1], null, 512, JSON_THROW_ON_ERROR);
return isset($matches[1]) ? \json_decode($matches[1], null, 512, JSON_THROW_ON_ERROR) : [];
}
/**

View File

@@ -29,7 +29,8 @@ Feature: List upload sessions via CLI command
| antivirus | ANTIVIRUS_INFECTED_FILE_HANDLING | abort |
And user "Alice" has uploaded file "filesForUpload/filesWithVirus/eicar.com" to "/virusFile.txt"
And the config "POSTPROCESSING_DELAY" has been set to "10s" for "postprocessing" service
And the administrator has waited for "2" seconds
# Polls --processing list until virusFile.txt is absent (30s timeout). Replaces fixed 2s wait.
And the administrator has waited until the file "virusFile.txt" has finished processing
And user "Alice" has uploaded file with content "uploaded content" to "/file1.txt"
And user "Alice" has uploaded file with content "uploaded content" to "/file2.txt"
When the administrator lists all the upload sessions with flag "processing"