Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions app/services/demo_service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
def run_k6_test(test_type, duration=60, vus=5):
"""Run K6 test in Docker container"""
try:
# Ensure numeric values for calculations
duration = int(duration)
vus = int(vus)
if test_type == 'load-testing':
cmd = [
'docker', 'exec', 'k6-load-tester',
Expand Down Expand Up @@ -82,8 +85,8 @@ def start_load_testing():
# If JSON parsing fails, use defaults
pass

duration = data.get('duration', 60)
vus = data.get('vus', 5)
duration = int(data.get('duration', 60))
vus = int(data.get('vus', 5))
Comment on lines +88 to +89

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance error handling for user input conversion.

The integer conversion is correctly applied to user input from the JSON payload. However, the current error handling might not provide clear feedback when invalid values are provided.

Consider more specific error handling for user input:

-        duration = int(data.get('duration', 60))
-        vus = int(data.get('vus', 5))
+        try:
+            duration = int(data.get('duration', 60))
+            vus = int(data.get('vus', 5))
+            
+            if duration <= 0 or vus <= 0:
+                return jsonify({
+                    'scriptId': 'load-testing',
+                    'success': False,
+                    'message': 'Duration and VUs must be positive integers'
+                }), 400
+                
+        except (ValueError, TypeError) as e:
+            return jsonify({
+                'scriptId': 'load-testing',
+                'success': False,
+                'message': 'Duration and VUs must be valid integers'
+            }), 400
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
duration = int(data.get('duration', 60))
vus = int(data.get('vus', 5))
try:
duration = int(data.get('duration', 60))
vus = int(data.get('vus', 5))
if duration <= 0 or vus <= 0:
return jsonify({
'scriptId': 'load-testing',
'success': False,
'message': 'Duration and VUs must be positive integers'
}), 400
except (ValueError, TypeError):
return jsonify({
'scriptId': 'load-testing',
'success': False,
'message': 'Duration and VUs must be valid integers'
}), 400
🤖 Prompt for AI Agents
In app/services/demo_service/app.py around lines 88 to 89, the code converts
user input to integers without specific error handling, which can cause unclear
feedback on invalid inputs. Add try-except blocks around the integer conversions
for 'duration' and 'vus' to catch ValueError exceptions, and provide clear error
messages or responses indicating which input was invalid and why, improving user
feedback on input errors.


if 'load-testing' in running_tests and running_tests['load-testing']['status'] == 'running':
return jsonify({
Expand Down