Testing and integration - kristianrpo/mom-grpc-microservices GitHub Wiki

1. Testing Scope

  • Integration Testing:
    • API Gateway ↔ Microservices communication
    • API Gateway ↔ MOM (Redis) interaction
    • Client ↔ MOM ↔ Microservices
  • Functional Testing:
    • Request processing workflows
    • Failover mechanisms
    • Task management operations
    • Inmediate response
  • Resilience Test:
    • Handle timeout when service is unavailable

2. Prerequisites

  • Python 3.8+
  • Pytest 8.3.5
  • Redis server running locally (port 6379)
  • MOM server running locally (port 50051)
  • API gateway running

2.1. Microservices Requirements

  • For API Gateway tests (test_api_gateway.py):
    All microservices must be running or mocked externally.

  • For Client tests (test_multiplication.py, test_sum.py, test_subtraction.py):
    Microservices are launched and stopped automatically inside the tests.

3. Tests execution

From project root (mom-grpc-microservices/):

3.1. Install dependencies

cd api_gateway
pip install -r requirements.txt

3.2. Run API Gateway tests

python -m pytest tests/test_api_gateway.py -v

3.3. Run Client ↔ Microservices tests

3.3.1. Multiplication service tests

python -m pytest tests/test_multiplication.py -s

3.3.2. Sum service tests

python -m pytest tests/test_sum.py -s

3.3.3. Subtraction service tests

python -m pytest tests/test_subtraction.py -s

4. Implemented Tests

4.1. Test: Request Processing (test_request_processing)

  • Purpose: Verifies correct routing to microservices
  • Success Criteria:
    • HTTP status code 200
    • processed_immediately = True
    • Response contains correct sum (15.0)
  • Execution:
    pytest tests/test_api_gateway.py::test_request_processing -v
    

4.2. Test: MOM Failover (test_failover_to_mom)

  • Purpose: Validates queueing mechanism when microservice fails
  • Success Criteria:
    • HTTP status code 200
    • "queued" status in response
    • Presence of task_id
  • Execution:
    pytest tests/test_api_gateway.py::test_failover_to_mom -v
    

4.3. Test: Task Retrieval (test_task_retrieval)

  • Purpose: Verifies task status query functionality
  • Success Criteria:
    • HTTP status code 200
    • Response contains valid status
  • Execution:
    pytest tests/test_api_gateway.py::test_task_retrieval -v
    
    

4.4. Test: Immediate Response Test (test_inmediate_response)

  • Purpose: Verifies that the microservice returns an immediate response when already running.
  • Scenario:
    • The microservice is started manually by the test.
    • A client request is submitted.
    • The service responds instantly with a result.
  • Success Criteria:
    • Status: COMPLETED
    • Result: expected value (product, total, or difference)
  • Used in:
    • test_multiplication.py, test_sum.py, test_subtraction.py

4.5. Delayed Queue Response Test (test_delayed_response_from_queue)

  • Purpose: Tests the system behavior when the microservice is initially down.
  • Scenario:
    • A request is submitted before the service is running.
    • The request is enqueued via MOM.
    • The microservice is started after a delay.
    • The client receives the result after re-polling.
  • Success Criteria:
    • Status: COMPLETED
    • Correct result delivered after delay
  • Used in:
    • test_multiplication.py, test_sum.py, test_subtraction.py

4.6. Timeout Handling Test (test_timeout_when_service_never_starts)

  • Purpose: Ensures the client times out gracefully when no microservice ever responds.
  • Scenario:
    • A request is submitted.
    • The microservice is never started.
    • The client times out after a fixed duration.
    • The test captures and inspects the client’s output.
  • Success Criteria:
    • Output contains "timeout reached"
  • Used in:
    • test_multiplication.py, test_sum.py, test_subtraction.py

πŸ“ Note: Tests from 4 to 6 automatically launch the actual microservice processes during execution and clean them up afterward.
You do not need to start any microservice manually for these tests.

5. Test Cases Results

5.1. API Gateway Tests

Test Case Status Description
test_request_processing βœ… PASSED Validates successful request processing
test_failover_to_mom βœ… PASSED Verifies MOM failover mechanism
test_task_retrieval βœ… PASSED Tests task status retrieval functionality

Total Execution Time (API Gateway): 5.51 seconds

5.2. Sum Service Tests

Test Case Status Description
test_inmediate_response βœ… PASSED Returns result immediately if service is running
test_delayed_response_from_queue βœ… PASSED Enqueues task, returns result after service starts
test_timeout_when_service_never_starts βœ… PASSED Handles timeout when service never responds

Total Execution Time (Sum): 51.51 seconds

5.3. Subtraction Service Tests

Test Case Status Description
test_inmediate_response βœ… PASSED Returns result immediately if service is running
test_delayed_response_from_queue βœ… PASSED Enqueues task, returns result after service starts
test_timeout_when_service_never_starts βœ… PASSED Handles timeout when service never responds

Total Execution Time (Subtraction): 53.56 seconds

5.4. Multiplication Service Tests

Test Case Status Description
test_inmediate_response βœ… PASSED Returns result immediately if service is running
test_delayed_response_from_queue βœ… PASSED Enqueues task, returns result after service starts
test_timeout_when_service_never_starts βœ… PASSED Handles timeout when service never responds

Total Execution Time (Multiplication): 51.47 seconds

5.5. Grand Total

Overall Execution Time (All Tests): 162.05 seconds