Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid crash if testdata file is empty #1403

Merged
merged 2 commits into from
Nov 6, 2024

Conversation

dagfinno
Copy link
Collaborator

@dagfinno dagfinno commented Nov 6, 2024

return an empty array if testdatafile not found, check for empty testdata in k6 tests

Description

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)

Documentation

  • Documentation is updated (either in docs-directory, Altinnpedia or a separate linked PR in altinn-studio-docs., if applicable)

Summary by CodeRabbit

  • New Features

    • Introduced error handling in various performance testing scripts to ensure required data arrays are populated before execution.
    • Added a new function for reading and parsing CSV files, improving error handling during file operations.
  • Bug Fixes

    • Enhanced robustness of performance tests by preventing execution with empty datasets, which could lead to runtime errors.

@dagfinno dagfinno requested a review from a team as a code owner November 6, 2024 07:17
Copy link
Contributor

coderabbitai bot commented Nov 6, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

This pull request introduces various enhancements to error handling across multiple performance testing scripts. Specifically, it adds checks to ensure that critical arrays, such as endUsersWithTokens, serviceOwners, and endUsers, are not empty or undefined before proceeding with function executions. This prevents runtime errors due to insufficient data. Additionally, a new function for reading CSV files is introduced, centralizing the CSV parsing logic and incorporating error handling for file reading issues.

Changes

File Path Change Summary
tests/k6/tests/enduser/performance/enduser-search.js Added error handling to check if endUsersWithTokens is undefined or empty, throwing an error if true.
tests/k6/tests/performancetest_common/readTestdata.js Introduced readCsv function for reading CSV files, refactored existing code to use this function, and added error handling for file reading.
tests/k6/tests/scenarios/performance/create-dialog-and-search.js Added error handling in createDialogs and enduserSearches functions to check if endUsersWithTokens and serviceOwners are empty before proceeding.
tests/k6/tests/serviceowner/performance/create-dialog.js Added error handling to ensure endUsers and serviceOwners are not empty before dialog creation.
tests/k6/tests/serviceowner/performance/create-remove-dialog.js Added error handling to check if endUsers and serviceOwners are populated before executing the dialog creation and removal logic.

Possibly related PRs

Suggested reviewers

  • oskogstad

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 5a9cb09 and 0385d11.

📒 Files selected for processing (1)
  • tests/k6/tests/performancetest_common/readTestdata.js (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/k6/tests/performancetest_common/readTestdata.js

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (6)
tests/k6/tests/enduser/performance/enduser-search.js (1)

20-22: Enhance error message for better debugging.

The guard clause is a good addition that prevents test execution with invalid data. However, the error message could be more descriptive to help users diagnose and fix the issue.

Consider enhancing the error handling:

     if (!endUsersWithTokens || endUsersWithTokens.length === 0) {
-        throw new Error('No end users loaded for testing');
+        console.error('Failed to load end users from test data file. Please ensure the test data file exists and contains valid user data.');
+        throw new Error('No end users loaded for testing. Check the test data file and ensure it contains valid user data.');
     }
tests/k6/tests/serviceowner/performance/create-dialog.js (2)

16-18: Consider using optional chaining for null check

The null check can be simplified using optional chaining.

-    if (!endUsers || endUsers.length === 0) {
+    if (!endUsers?.length) {
         throw new Error('No end users loaded for testing');
     }

19-21: Consider using optional chaining for null check

Similar to the above, this null check can also be simplified.

-    if (!serviceOwners || serviceOwners.length === 0) {
+    if (!serviceOwners?.length) {
         throw new Error('No service owners loaded for testing');
     }
tests/k6/tests/performancetest_common/readTestdata.js (2)

12-19: LGTM! Consider enhancing error logging and input validation.

The implementation successfully addresses the PR objective by gracefully handling file read errors and returning an empty array instead of crashing.

Consider these enhancements:

 function readCsv(filename) {
+  if (!filename || typeof filename !== 'string') {
+    console.log('Invalid filename provided');
+    return [];
+  }
   try {
     return papaparse.parse(open(filename), { header: true, skipEmptyLines: true }).data;
   } catch (error) {
-    console.log(`Error reading CSV file: ${error}`);
+    console.log(`Error reading CSV file ${filename}: ${error.message}`);
+    console.log(`Stack trace: ${error.stack}`);
     return [];
   } 
 }

Line range hint 1-60: Consider centralizing test data validation and configuration.

The current implementation handles test data well, but consider these architectural improvements:

  1. Move file paths to a separate configuration file
  2. Create a TestDataManager class to handle all data loading and validation
  3. Add data schema validation using a library like Joi or Zod

This would make the code more maintainable and easier to test. Would you like me to provide an example implementation?

tests/k6/tests/scenarios/performance/create-dialog-and-search.js (1)

70-72: Consider extracting common validation logic

The validation for endUsersWithTokens is duplicated between createDialogs() and enduserSearches(). Consider extracting this into a shared validation function.

+function validateEndUsers() {
+  if (!endUsersWithTokens || endUsersWithTokens.length === 0) {
+    throw new Error('No end users loaded for testing');
+  }
+}

 export function createDialogs() {
-  if (!endUsersWithTokens || endUsersWithTokens.length === 0) {
-    throw new Error('No end users loaded for testing');
-  }
+  validateEndUsers();
   if (!serviceOwners || serviceOwners.length === 0) {
     throw new Error('No service owners loaded for testing');
   }
   createDialog(randomItem(serviceOwners), randomItem(endUsersWithTokens));
 }

 export function enduserSearches() {
-  if (!endUsersWithTokens || endUsersWithTokens.length === 0) {
-    throw new Error('No end users loaded for testing');
-  }
+  validateEndUsers();
   enduserSearch(randomItem(endUsersWithTokens));
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 70a63cd and 5a9cb09.

📒 Files selected for processing (5)
  • tests/k6/tests/enduser/performance/enduser-search.js (1 hunks)
  • tests/k6/tests/performancetest_common/readTestdata.js (4 hunks)
  • tests/k6/tests/scenarios/performance/create-dialog-and-search.js (1 hunks)
  • tests/k6/tests/serviceowner/performance/create-dialog.js (1 hunks)
  • tests/k6/tests/serviceowner/performance/create-remove-dialog.js (1 hunks)
🧰 Additional context used
📓 Learnings (3)
tests/k6/tests/scenarios/performance/create-dialog-and-search.js (1)
Learnt from: dagfinno
PR: digdir/dialogporten#1331
File: tests/k6/tests/serviceowner/performance/create-dialog.js:28-33
Timestamp: 2024-10-23T11:32:22.074Z
Learning: In k6 performance tests, when data arrays such as `serviceOwners` and `endUsers` might be empty, prefer to let the test crash rather than adding checks to log error messages continuously.
tests/k6/tests/serviceowner/performance/create-dialog.js (1)
Learnt from: dagfinno
PR: digdir/dialogporten#1331
File: tests/k6/tests/serviceowner/performance/create-dialog.js:28-33
Timestamp: 2024-10-23T11:32:22.074Z
Learning: In k6 performance tests, when data arrays such as `serviceOwners` and `endUsers` might be empty, prefer to let the test crash rather than adding checks to log error messages continuously.
tests/k6/tests/serviceowner/performance/create-remove-dialog.js (1)
Learnt from: dagfinno
PR: digdir/dialogporten#1331
File: tests/k6/tests/serviceowner/performance/create-dialog.js:28-33
Timestamp: 2024-10-23T11:32:22.074Z
Learning: In k6 performance tests, when data arrays such as `serviceOwners` and `endUsers` might be empty, prefer to let the test crash rather than adding checks to log error messages continuously.
🔇 Additional comments (6)
tests/k6/tests/enduser/performance/enduser-search.js (1)

20-22: Verify consistent error handling across test files.

Let's ensure this error handling pattern is consistently implemented across related test files.

✅ Verification successful

Error handling is consistently implemented across test files

The verification shows consistent error handling patterns:

  • enduser-search.js and create-dialog-and-search.js both check for !endUsersWithTokens || endUsersWithTokens.length === 0
  • create-dialog.js implements the same pattern for endUsers
  • All files throw similar error messages when test data is missing
  • The test data loading is centralized in readTestdata.js and properly imported by all test files
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for consistent error handling in k6 test files
# Expected: Similar guard clauses in other test files using endUsersWithTokens

# Test 1: Find all k6 test files using endUsersWithTokens
echo "=== K6 test files using endUsersWithTokens ==="
rg -l "endUsersWithTokens" --type js "tests/k6"

# Test 2: Check error handling patterns in these files
echo -e "\n=== Error handling patterns ==="
rg "(!|\s+undefined.*|\.length\s*===\s*0).*endUsersWithTokens" --type js "tests/k6"

# Test 3: Verify the test data loading implementation
echo -e "\n=== Test data loading implementation ==="
rg -A 10 "readTestdata" --type js "tests/k6"

Length of output: 7513

tests/k6/tests/serviceowner/performance/create-dialog.js (1)

16-21: Consider previous feedback about handling empty arrays in k6 tests

Based on previous feedback in PR #1331, it was suggested to let k6 performance tests crash naturally rather than adding explicit checks for empty arrays. The current implementation contradicts this guidance.

However, I notice that these error messages are more descriptive than a natural crash would be, which could help with debugging. We have two options here:

  1. Remove these checks to align with previous guidance
  2. Keep them if the improved error messages provide significant debugging value

Let's check if similar checks exist in other k6 test files:

tests/k6/tests/serviceowner/performance/create-remove-dialog.js (3)

19-21: LGTM: Clear error message for missing end users.

The error message clearly indicates when end users data is missing, which aligns with the PR's objective of better handling empty test data files.


22-24: LGTM: Clear error message for missing service owners.

The error message clearly indicates when service owners data is missing, which aligns with the PR's objective of better handling empty test data files.


19-24: Consider previous feedback about handling empty arrays in k6 tests.

Based on previous feedback in PR #1331, it was suggested to let k6 tests crash naturally rather than adding explicit checks for empty arrays. The current implementation contradicts this guidance.

However, if the explicit checks are intentional to provide more descriptive error messages, then the implementation is consistent with similar changes in other test files.

Let's verify the consistency of error handling across other k6 test files:

✅ Verification successful

Empty array checks are consistently implemented across k6 test files

The explicit checks for empty arrays and descriptive error messages are consistently implemented across multiple k6 test files:

  • tests/k6/tests/scenarios/performance/create-dialog-and-search.js
  • tests/k6/tests/enduser/performance/enduser-search.js
  • tests/k6/tests/graphql/performance/graphql-search.js
  • tests/k6/tests/serviceowner/performance/create-dialog.js

This appears to be an established pattern in the codebase, suggesting that the previous feedback about letting tests crash naturally has been superseded by a more explicit error handling approach that provides better debugging information.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for similar error handling patterns in other k6 test files
# Expected: Find similar array emptiness checks in other test files

# Search for similar error handling patterns in k6 test files
rg -A 2 "(!.*Users.*\.length === 0|!.*Owners.*\.length === 0)" tests/k6/tests/

Length of output: 2822

tests/k6/tests/scenarios/performance/create-dialog-and-search.js (1)

60-65: Clarification needed: Change contradicts previous approach

This validation logic contradicts your previous feedback in PR #1331 where you mentioned preferring to let tests crash rather than adding empty checks. Could you clarify if this is an intentional change in approach?

If this is a new direction, consider adding a comment explaining why explicit validation is now preferred over letting the test fail naturally.

Copy link

sonarqubecloud bot commented Nov 6, 2024

@dagfinno dagfinno merged commit e0ea0af into main Nov 6, 2024
4 checks passed
@dagfinno dagfinno deleted the fix/read-performance-testdata branch November 6, 2024 09:09
arealmaas pushed a commit that referenced this pull request Nov 6, 2024
🤖 I have created a release *beep* *boop*
---


##
[1.28.3](v1.28.2...v1.28.3)
(2024-11-06)


### Bug Fixes

* avoid crash if testdata file is empty
([#1403](#1403))
([e0ea0af](e0ea0af))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
@coderabbitai coderabbitai bot mentioned this pull request Dec 12, 2024
4 tasks
@coderabbitai coderabbitai bot mentioned this pull request Jan 8, 2025
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants