Skip to content

Commit

Permalink
fix(rest-api) content import postman flakey test #31005 (#31015)
Browse files Browse the repository at this point in the history
### Proposed Changes
* Changed the scope of the variables `startTime` and `retryCount` from
`pm.environment` to `pm.collectionVariables` for better management and
scoping within Postman collections.
* Updated the test for canceling the content import job to use a larger
file to increase the chances of the job being in cancelable state when
the cancel request is made.

### Checklist
- [x] Tests
- [x] Translations
- [x] Security Implications Contemplated (add notes if applicable)

### Additional Info
These changes ensure that the retry logic and time tracking variables
are isolated to the collection level, avoiding potential conflicts with
environment-wide settings. Additionally, the larger file used for the
cancel job test makes it more likely to simulate a real-world scenario
where the job is still active when the cancel request is issued.
  • Loading branch information
valentinogiardino authored Jan 6, 2025
1 parent e0b5efc commit 9059c2b
Show file tree
Hide file tree
Showing 2 changed files with 3,024 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "13ba66c7-fa5d-4baf-882a-71de5536689d",
"_postman_id": "72d1e6bd-4ed6-450c-874d-c0a99bcc305d",
"name": "ContentImportResource",
"description": "Postman collection for testing the ContentImportResource API endpoints.",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
Expand Down Expand Up @@ -2217,8 +2217,8 @@
"exec": [
"const maxTimeout = 30000; // 10 seconds",
"const maxRetries = 10; // Maximum number of retry attempts",
"const startTime = parseInt(pm.environment.get(\"startTime\"));",
"const retryCount = parseInt(pm.environment.get(\"retryCount\"));",
"const startTime = parseInt(pm.collectionVariables.get(\"startTime\"));",
"const retryCount = parseInt(pm.collectionVariables.get(\"retryCount\"));",
"const elapsedTime = Date.now() - startTime;",
"",
"console.log(`Attempt ${retryCount + 1}, Elapsed time: ${elapsedTime}ms`);",
Expand All @@ -2229,11 +2229,11 @@
"// Check if job status is \"SUCCESS\"",
"if (response.entity.state === \"SUCCESS\") {",
" // Clear environment variables once done",
" pm.environment.unset(\"startTime\");",
" pm.environment.unset(\"retryCount\");",
" pm.collectionVariables.unset(\"startTime\");",
" pm.collectionVariables.unset(\"retryCount\");",
"} else if (elapsedTime < maxTimeout && retryCount < maxRetries) {",
" // Increment retry count",
" pm.environment.set(\"retryCount\", retryCount + 1);",
" pm.collectionVariables.set(\"retryCount\", retryCount + 1);",
" ",
" setTimeout(function(){",
" console.log(\"Sleeping for 3 seconds before next request.\");",
Expand All @@ -2243,8 +2243,8 @@
"} else {",
" // If we exceed the max timeout or max retries, fail the test",
" const timeoutReason = elapsedTime >= maxTimeout ? \"timeout\" : \"max retries\";",
" pm.environment.unset(\"startTime\");",
" pm.environment.unset(\"retryCount\");",
" pm.collectionVariables.unset(\"startTime\");",
" pm.collectionVariables.unset(\"retryCount\");",
" pm.test(`Job state check failed due to ${timeoutReason}`, function () {",
" pm.expect.fail(`${timeoutReason} reached after ${elapsedTime}ms. Job still in processing state after ${retryCount} attempts`);",
" });",
Expand All @@ -2270,12 +2270,12 @@
"listen": "prerequest",
"script": {
"exec": [
"if (!pm.environment.get(\"startTime\")) {",
" pm.environment.set(\"startTime\", Date.now());",
"if (!pm.collectionVariables.get(\"startTime\")) {",
" pm.collectionVariables.set(\"startTime\", Date.now());",
"}",
"",
"if (!pm.environment.get(\"retryCount\")) {",
" pm.environment.set(\"retryCount\", 0);",
"if (!pm.collectionVariables.get(\"retryCount\")) {",
" pm.collectionVariables.set(\"retryCount\", 0);",
"}"
],
"type": "text/javascript",
Expand Down Expand Up @@ -2342,7 +2342,7 @@
{
"key": "file",
"type": "file",
"src": "resources/ContentImportResource/test-import-content-job-final.csv"
"src": "resources/ContentImportResource/test-import-content-job-large-file.csv"
},
{
"key": "form",
Expand Down Expand Up @@ -2434,8 +2434,8 @@
"exec": [
"const maxTimeout = 30000; // 10 seconds",
"const maxRetries = 10; // Maximum number of retry attempts",
"const startTime = parseInt(pm.environment.get(\"startTime\"));",
"const retryCount = parseInt(pm.environment.get(\"retryCount\"));",
"const startTime = parseInt(pm.collectionVariables.get(\"startTime\"));",
"const retryCount = parseInt(pm.collectionVariables.get(\"retryCount\"));",
"const elapsedTime = Date.now() - startTime;",
"",
"console.log(`Attempt ${retryCount + 1}, Elapsed time: ${elapsedTime}ms`);",
Expand All @@ -2446,11 +2446,11 @@
"// Check if job status is \"CANCELED\"",
"if (response.entity.state === \"CANCELED\") {",
" // Clear environment variables once done",
" pm.environment.unset(\"startTime\");",
" pm.environment.unset(\"retryCount\");",
" pm.collectionVariables.unset(\"startTime\");",
" pm.collectionVariables.unset(\"retryCount\");",
"} else if (elapsedTime < maxTimeout && retryCount < maxRetries) {",
" // Increment retry count",
" pm.environment.set(\"retryCount\", retryCount + 1);",
" pm.collectionVariables.set(\"retryCount\", retryCount + 1);",
" ",
" setTimeout(function(){",
" console.log(\"Sleeping for 3 seconds before next request.\");",
Expand All @@ -2460,8 +2460,8 @@
"} else {",
" // If we exceed the max timeout or max retries, fail the test",
" const timeoutReason = elapsedTime >= maxTimeout ? \"timeout\" : \"max retries\";",
" pm.environment.unset(\"startTime\");",
" pm.environment.unset(\"retryCount\");",
" pm.collectionVariables.unset(\"startTime\");",
" pm.collectionVariables.unset(\"retryCount\");",
" pm.test(`Job state check failed due to ${timeoutReason}`, function () {",
" pm.expect.fail(`${timeoutReason} reached after ${elapsedTime}ms. Job still in processing state after ${retryCount} attempts`);",
" });",
Expand All @@ -2487,12 +2487,12 @@
"listen": "prerequest",
"script": {
"exec": [
"if (!pm.environment.get(\"startTime\")) {",
" pm.environment.set(\"startTime\", Date.now());",
"if (!pm.collectionVariables.get(\"startTime\")) {",
" pm.collectionVariables.set(\"startTime\", Date.now());",
"}",
"",
"if (!pm.environment.get(\"retryCount\")) {",
" pm.environment.set(\"retryCount\", 0);",
"if (!pm.collectionVariables.get(\"retryCount\")) {",
" pm.collectionVariables.set(\"retryCount\", 0);",
"}"
],
"type": "text/javascript",
Expand Down
Loading

0 comments on commit 9059c2b

Please sign in to comment.