Skip to content

✅ Add unit tests for Lambda worker handler locustfile processing #357

Description

@sodre

Description

The Lambda worker handler (src/zae_limiter/loadtest/lambda/worker.py) has existing unit tests in tests/unit/loadtest/test_worker.py covering handler dispatch (TestHandler) and basic _load_user_classes loading. However, several locustfile processing paths and runtime behaviors lack coverage.

Acceptance Criteria

_load_user_classes locustfile resolution

  • Test that config["locustfile"] takes precedence over LOCUSTFILE env var for module path resolution
  • Test that the default module path is locustfile when neither config["locustfile"] nor LOCUSTFILE env var is set
  • Test that abstract User subclasses (abstract = True) are excluded from auto-discovery results
  • Test that non-User classes in the module are excluded from auto-discovery results
  • Test that whitespace in comma-separated user_classes is trimmed (e.g., "ClassA, ClassB")

_configure_boto3_pool

  • Test that _configure_boto3_pool is idempotent (calling twice does not re-patch)
  • Test that the patched boto3.Session.client applies max_pool_connections config to dynamodb service calls
  • Test that non-dynamodb service calls are not affected by the patch

_run_headless

  • Test that _run_headless loads user classes from config, creates a LocalRunner, starts users, and returns stats dict with expected keys (total_requests, total_failures, avg_response_time, p50, p95, p99, requests_per_second, failure_rate)
  • Test that _run_headless stops early when context.get_remaining_time_in_millis() returns a value below the shutdown buffer
  • Test that _run_headless calculates shutdown buffer as shutdown_buffer_pct of initial remaining time

_run_as_worker

  • Test that _run_as_worker generates a worker ID from context.aws_request_id when available
  • Test that _run_as_worker generates a UUID-based worker ID when context has no aws_request_id
  • Test that _run_as_worker sets LOCUST_UNIQUE_ID environment variable before creating the runner
  • Test that _run_as_worker quits gracefully when Lambda timeout approaches (remaining time < shutdown buffer)
  • Test that _run_as_worker returns {"status": "worker_completed", "worker_id": ...}

Notes

  • All tests should use mocks for locust imports (Environment, User, etc.) to avoid gevent monkey-patching in the test process, following the existing _FakeUserBase pattern in test_worker.py
  • Tests belong in tests/unit/loadtest/test_worker.py, extending the existing test file
  • Use pytest.mark.gevent marker consistent with existing tests in the file

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/loadtestLoad testing infrastructure and toolingtestingTest coverage

    Type

    No fields configured for Task.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions