Skip to content

test: mock real I/O in unit tests to stop fail-slow flakes - #4455

Merged
Jan-Kazlouski-elastic merged 4 commits into
mainfrom
test/mock-fail-slow-flakes
Sep 9, 2026
Merged

test: mock real I/O in unit tests to stop fail-slow flakes#4455
Jan-Kazlouski-elastic merged 4 commits into
mainfrom
test/mock-fail-slow-flakes

Conversation

@Jan-Kazlouski-elastic

Copy link
Copy Markdown
Contributor

Several unit tests passed assertions but intermittently failed CI under the default 1s pytest-fail-slow threshold because they still hit real I/O:

  • Oracle/MSSQL tests patched Engine.connect but still called real create_engine
  • GraphQL close() accessed the cached session property, creating an aiohttp.ClientSession even when none was used
  • Some GitLab get_docs integration tests left get_work_items_group unmocked, triggering real HTTP for epic fetches

This PR mocks create_engine via a shared test helper, guards GraphQL session teardown, stubs epic fetches where not under test, and adds a regression test for GraphQL close behavior.

Checklists

Pre-Review Checklist

  • this PR does NOT contain credentials of any kind, such as API keys or username/passwords (double check config.yml.example)
  • this PR has a meaningful title
  • this PR links to all relevant github issues that it fixes or partially addresses
  • if there is no GH issue, please create it. Each PR should have a link to an issue
  • this PR has a thorough description
  • Covered the changes with automated tests
  • Tested the changes locally
  • Added a label for each target release version (example: v7.13.2, v7.14.0, v8.0.0)
  • For bugfixes: backport safely to all minor branches still receiving patch releases
  • Considered corresponding documentation changes
  • Contributed any configuration settings changes to the configuration reference
  • if you added or changed Rich Configurable Fields for a Native Connector, you made a corresponding PR in Kibana

Changes Requiring Extra Attention

Related Pull Requests

Release Note

Skip creating aiohttp sessions on GraphQL close when none was opened.
Mock SQLAlchemy create_engine in Oracle/MSSQL tests instead of only
patching Engine.connect. Stub GitLab epic fetches in get_docs tests that
do not exercise epics.
self._sleeps.cancel()
await self.session.close()
del self.session
session = self.__dict__.get("session")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That doesn't look right. Why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hi @artem-shelkovnikov

__dict__.get("session") approach was done to work around how @cached_property behaves. Reading self.session in close() always creates an aiohttp session, even when the test never made a request. Our tests always call close() on teardown, so that was triggering real session creation and flaky fail-slow failures.

The idea was to guard close() in production so it only shuts down a session if one was already opened. That works in practice, but I agree that it looks weird and we probably wouldn't want this in production code as part of test fix only PR.

I've switched to a different fix instead: create_graphql_source now mocks aiohttp.ClientSession, so teardown stays fast without changing connector code. The production close() is back to what it was on main.

We might want to clean up session handling in the GraphQL client itself (with explicit _session = None like Confluence/Jira) a as a follow up.

…close()

Revert the production close() guard and patch aiohttp.ClientSession in
create_graphql_source so teardown stays fast without touching connector code.
@Jan-Kazlouski-elastic

Copy link
Copy Markdown
Contributor Author

@artem-shelkovnikov
Your comment is addressed. Would you please take another look at this PR?

@Jan-Kazlouski-elastic
Jan-Kazlouski-elastic merged commit 5c0b297 into main Sep 9, 2026
2 checks passed
@Jan-Kazlouski-elastic
Jan-Kazlouski-elastic deleted the test/mock-fail-slow-flakes branch September 9, 2026 13:02
@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

💔 Failed to create backport PR(s)

Status Branch Result
9.4 #4466
8.19 Commit could not be cherrypicked due to conflicts
9.5 #4467

Successful backport PRs will be merged automatically after passing CI.

To backport manually run:
backport --pr 4455 --autoMerge --autoMergeMethod squash

Jan-Kazlouski-elastic added a commit that referenced this pull request Sep 9, 2026
…) (#4467)

Backports the following commits to 9.5:
 - test: mock real I/O in unit tests to stop fail-slow flakes (#4455)

Co-authored-by: Jan-Kazlouski-elastic <jan.kazlouski@elastic.co>
Jan-Kazlouski-elastic added a commit that referenced this pull request Sep 9, 2026
…) (#4466)

Backports the following commits to 9.4:
 - test: mock real I/O in unit tests to stop fail-slow flakes (#4455)

Co-authored-by: Jan-Kazlouski-elastic <jan.kazlouski@elastic.co>
Jan-Kazlouski-elastic added a commit that referenced this pull request Sep 9, 2026
) (#4468)

Backports #4455 to `8.19`.

## Summary
- Add shared `mock_sync_db_engine` helper and use it in Oracle/MSSQL
tests instead of only patching `Engine.connect`.
- Mock `aiohttp.ClientSession` in the GraphQL test helper so teardown
does not open a real session.
- Add regression test asserting GraphQL teardown does not instantiate a
real `ClientSession`.

## Backport notes
- The 8.19 branch uses monolithic
`connectors/sources/{graphql,mssql,oracle}.py` modules, so patch targets
differ from `main` (`connectors.sources.graphql.aiohttp.ClientSession`,
`connectors.sources.mssql.create_engine`, etc.).
- GitLab `get_work_items_group` stubs from the original PR are not
applicable on 8.19 (method does not exist there).
- The incidental `NOTICE.txt` version bump from `make install` is
intentionally excluded.

## Test plan
- [x] `make clean install autoformat lint test`
- [x] `pytest tests/sources/test_graphql.py tests/sources/test_mssql.py
tests/sources/test_oracle.py`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants