Skip to content

fix: improve flaky CI test resilience for channel type limits#238

Open
aliev wants to merge 1 commit intomainfrom
fix/flaky-test-retry-decorator
Open

fix: improve flaky CI test resilience for channel type limits#238
aliev wants to merge 1 commit intomainfrom
fix/flaky-test-retry-decorator

Conversation

@aliev
Copy link
Copy Markdown
Member

@aliev aliev commented Apr 14, 2026

Why

CI runs 4 parallel runners (Python 3.10-3.13) that all share one Stream app with a hard limit of 50 custom channel types. Each runner executes 4 tests that create a channel type and delete it in finally. In practice, types leak: tests fail before finally, or delete_channel_type itself fails due to eventual consistency. Over time the app accumulates 50 stale types and every create_channel_type call returns 400:

StreamAPIException: Stream error code 4: CreateChannelType failed with error:
"your application reached the maximum number of custom channel types (50)"

Retry alone does not help because the limit is permanently saturated, not temporarily.

Changes

@cleanup_channel_types decorator (tests/base.py), runs before each channel type test:

  1. Lists all channel types in the app
  2. Skips 5 builtins (messaging, livestream, team, gaming, commerce)
  3. Deletes only types older than 2 minutes, stale leftovers from previous runs
  4. Waits 2s for eventual consistency
  5. Runs the actual test

The 2-minute threshold avoids deleting types that a parallel runner just created in the current run.

_is_transient_error improvements (tests/base.py):

  • Catch 429 (rate limit) status code
  • Use str(exc) instead of http_response.text for reliable phrase matching
  • Add "maximum number of", "rate limit", "too many" to retryable phrases

New decorator applications (tests/test_chat_misc.py):

  • @cleanup_channel_types + @retry_on_transient_error() on all 4 channel type CRUD tests
  • @retry_on_transient_error() on test_event_hooks_sqs_sns

Summary by CodeRabbit

  • Tests

    • Enhanced transient error detection for improved reliability.
    • Added channel type cleanup utilities to improve test stability.
    • Applied retry decorators to critical test functions.
  • Bug Fixes

    • Improved resilience in handling transient API errors with broader status code and error message pattern matching.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 14, 2026

Warning

Rate limit exceeded

@aliev has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 53 minutes and 14 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 53 minutes and 14 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6c3cc1cd-5559-4ffc-9202-30230a2420c0

📥 Commits

Reviewing files that changed from the base of the PR and between 4d49318 and 3a29e71.

📒 Files selected for processing (2)
  • tests/base.py
  • tests/test_chat_misc.py
📝 Walkthrough

Walkthrough

Test infrastructure updated to improve transient error detection for stream API exceptions and introduce a decorator utility for cleaning up stale channel types before test execution.

Changes

Cohort / File(s) Summary
Error Handling & Test Utilities
tests/base.py
Refined _is_transient_error for StreamAPIException by replacing fragile text parsing with broader status code checks (429, 502, 503, 504) and substring matching against lowercased exception string. Added cleanup_channel_types decorator with module constants _BUILTIN_CHANNEL_TYPES and _STALE_THRESHOLD to list, filter, and delete non-builtin channel types older than 2 minutes before tests run.
Decorator Application
tests/test_chat_misc.py
Applied cleanup_channel_types and retry_on_transient_error() decorators to channel-type-related tests (test_create_channel_type, test_update_channel_type_mark_messages_pending, test_update_channel_type_push_notifications, test_delete_channel_type). Added retry_on_transient_error() to test_event_hooks_sqs_sns. Imported new decorators from tests.base.

Sequence Diagram

sequenceDiagram
    participant Test as Test Function
    participant Decorator as cleanup_channel_types
    participant Client as Client Fixture
    participant API as Chat API
    
    Test->>Decorator: Execute with decorator
    Decorator->>Decorator: Resolve client from kwargs/signature
    Decorator->>Client: Request list_channel_types()
    Client->>API: GET /channel_types
    API-->>Client: Return channel types list
    Client-->>Decorator: Channel types data
    Decorator->>Decorator: Filter non-builtin types older than 2min
    Decorator->>API: DELETE stale channel types
    API-->>Decorator: Deletion complete (suppress errors)
    Decorator->>Decorator: Sleep 2 seconds
    Decorator->>Test: Invoke original test function
    Test-->>Decorator: Test execution complete
Loading

Estimated Code Review Effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A tidy warren, fresh and clean,
With stale channel-types removed from the scene,
Transient errors caught with wisdom anew,
The tests now run smoother, tried and true! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately and clearly summarizes the main change: improving CI test resilience for channel type limits through retry and cleanup mechanisms.
Docstring Coverage ✅ Passed Docstring coverage is 84.62% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/flaky-test-retry-decorator

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@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

🧹 Nitpick comments (1)
tests/base.py (1)

123-129: Scope stale cleanup to test-owned channel types.

This loop deletes every non-builtin type older than two minutes. That is broader than the resources these tests create (testtype* / testdeltype*) and can remove legitimate custom types from the shared app. Prefer filtering on an explicit test prefix or another ownership marker before deleting.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/base.py` around lines 123 - 129, The cleanup loop iterates
resp.data.channel_types and deletes any non-builtin type older than
_STALE_THRESHOLD, which can remove shared custom types; restrict deletion to
only test-owned types by checking the channel type name for the test prefixes
used in this test suite (e.g., names starting with "testtype" or "testdeltype")
before calling client.chat.delete_channel_type(name=name), leaving the existing
_BUILTIN_CHANNEL_TYPES and timestamp check intact and retaining the try/except
around the delete call.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@tests/test_chat_misc.py`:
- Around line 393-394: The decorators on the channel-type CRUD tests are in the
wrong order: swap the decorator order so `@retry_on_transient_error`() wraps the
test and `@cleanup_channel_types` is inside it (i.e., place
`@retry_on_transient_error`() above `@cleanup_channel_types`) so cleanup runs on
every retry attempt; make the same swap for the other channel-type CRUD tests
that use these two decorators (ensure tests referencing cleanup_channel_types
and retry_on_transient_error use the retry decorator outermost).

---

Nitpick comments:
In `@tests/base.py`:
- Around line 123-129: The cleanup loop iterates resp.data.channel_types and
deletes any non-builtin type older than _STALE_THRESHOLD, which can remove
shared custom types; restrict deletion to only test-owned types by checking the
channel type name for the test prefixes used in this test suite (e.g., names
starting with "testtype" or "testdeltype") before calling
client.chat.delete_channel_type(name=name), leaving the existing
_BUILTIN_CHANNEL_TYPES and timestamp check intact and retaining the try/except
around the delete call.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 1abd8b6c-af72-44f0-b27b-8c3d0a312894

📥 Commits

Reviewing files that changed from the base of the PR and between 7c4c123 and 4d49318.

📒 Files selected for processing (2)
  • tests/base.py
  • tests/test_chat_misc.py

- Broaden _is_transient_error to catch 429, rate limits, and resource
  limit errors (e.g. "maximum number of custom channel types")
- Use str(exc) instead of http_response.text for reliable phrase matching
- Add @cleanup_channel_types decorator that deletes stale (>2min)
  non-builtin channel types before tests to free slots in shared CI env
- Apply @retry_on_transient_error and @cleanup_channel_types to channel
  type CRUD tests and event_hooks test
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.

1 participant