Junior
What do you know about timeout testing?
sobes.tech AI
Answer from AI
Timeout testing involves verifying that the system correctly handles exceeding the specified wait time for an operation, response, or action.
Main aspects:
- Purpose: Ensure the system does not hang indefinitely, correctly handles timeout errors (e.g., disconnects, returns error messages), and does not block other operations.
- Application areas:
- Network requests (HTTP, TCP/IP).
- Waiting for responses from external services (API, databases).
- File system operations.
- Hardware interactions.
- User input waiting (in some systems).
- Types of timeouts:
- Connection timeout: Time to establish a connection.
- Read/Idle timeout: Waiting time for data after connection is established.
- Write timeout: Time to complete data sending.
- Execution timeout: Total time for operation completion.
- Testing strategies:
- Positive testing: Verify correct operation within normal execution time (less than timeout).
- Negative testing:
- Initiate a situation where the operation exceeds the timeout.
- Check system behavior on timeout (logging, user message, resource release).
- Check system state after timeout.
- Boundary values: Testing with execution time close to timeout value.
- Tools and techniques:
- Setting network delays (latency).
- Using mock services that controllably delay responses.
- Changing timeout settings in app or service configuration.
- Using network engineering tools (e.g.,
tcin Linux,PktRdr). - Automated tests emulating long operations or delays.
# Example of an automatic timeout test
import requests
import time
def test_api_timeout():
url = "http://example.com/slow_api" # API that intentionally delays response
timeout_seconds = 5 # Set timeout limit
start_time = time.time()
try:
response = requests.get(url, timeout=timeout_seconds)
# If no exception, this is an error if timeout was expected
assert False, f"API unexpectedly responded within {timeout_seconds} seconds."
except requests.exceptions.Timeout:
end_time = time.time()
duration = end_time - start_time
# Check that exception was raised and time is approximately equal to timeout
print(f"Timeout occurred as expected after {duration:.2f} seconds.")
assert duration >= timeout_seconds * 0.9, "Timeout occurred too early"
assert duration <= timeout_seconds * 1.5, "Timeout occurred too late or not at all"
except Exception as e:
assert False, f"An unexpected error occurred: {e}"
# Run the test
# test_api_timeout()
It is important to test how the system recovers after a timeout, cleans up resources (connections, memory threads), and does not enter a non-operational state.
Timeout examples:
| Operation Type | Example | Expected behavior on timeout |
|---|---|---|
| HTTP GET request | Fetching API data | Error message "Request timed out", disconnect |
| Database connection | Opening a connection | Connection error, logging event |
| File write operation | Saving a large file | Write termination, possible partial file |
| User input waiting | Dialog window | Window closing, returning to previous state |