Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpserver: Clear state in function-scoped fixtures before the test #352

Merged
merged 1 commit into from
Aug 11, 2024

Conversation

netaneld122
Copy link
Contributor

I believe it makes sense to clear the server state before performing a test rather than after, starting each test in a fresh state.

Consider the following case:

@fixture
def my_fixture() -> DoesSomeHTTPRequests:
    start_thread()
    yield obj
    stop_thread()

def test_function_1(my_fixture: DoesSomeHTTPRequests, httpserver: HTTPServer) -> None:
    my_fixture.make_thread_communicate_with(httpserver.url_for('/'))

def test_function_2(httpserver: HTTPServer) -> None:
    assert len(httpserver.log) == 0

In that case - test_function_2 may receive leaked logs from test_function_1 since my_fixture is destructed only after httpserver and therefore the fixture would be able to send requests to the server after the state is cleared.

One could argue that swapping the fixtures order would solve this, but in real world scenarios fixtures can be nested and far too complex to have a well defined order for cases like that. I think that clearing the state before the test is a simple and less error prone solution.

Copy link

codecov bot commented Aug 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.90%. Comparing base (0eb610b) to head (965818b).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #352   +/-   ##
=======================================
  Coverage   95.90%   95.90%           
=======================================
  Files           5        5           
  Lines         660      660           
=======================================
  Hits          633      633           
  Misses         27       27           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@csernazs
Copy link
Owner

csernazs commented Aug 9, 2024

hi @netaneld122 ,

I think your MR makes sense.
I understand the issue you descibed.

I could reproduce it without using threads:

import pytest

from pytest_httpserver import HTTPServer
import requests

class Client:
    def __init__(self):
        self.url: str | None = None

    def get(self):
        if self.url:
            requests.get(self.url)

@pytest.fixture()
def my_fixture():
    client = Client()
    yield client
    client.get()


def test_1(my_fixture: Client, httpserver: HTTPServer):
    httpserver.expect_request("/foo").respond_with_data("OK")
    my_fixture.url = httpserver.url_for("/foo")

def test_2(httpserver: HTTPServer):
    print(httpserver.log)

Basically, the issue is the order of destructing the fixtures, if httpserver gets destructed first, then it will clear its state and the remaining still-running fixtures can access it by the URL.

If I change my_fixture like this:

def my_fixture(httpserver):

Then it will define the order of destructuring (and dependency), so httpserver will be destructed later than my_fixture.

While your PR makes totally sense, I'm thinking about what could go wrong with it (assuming the existing 3rd party codes which use pytest-httpserver). I see no issues at the moment but I want to sleep on it.

Thanks,
Zsolt

@netaneld122
Copy link
Contributor Author

Thanks for your reply @csernazs.

I only want to point out that my_fixture could be a generic fixture or similarly even a 3rd party fixture such that making it depend on httpserver would make less sense.

I understand your concerns though, perhaps bumping the minor version (rather than the patch version) could be less prone to breakage (for those who depend on ~1.0.12)?

I can also suggest clearing the state both before and after the test function, does that feel safer?

@csernazs
Copy link
Owner

Yeah, I was also thinking about clearing the state both side.

But tbh your approach seems also safe. There's no way to access the httpserver fixture without clearing it before, so I'd release your PR. Maybe 1.1.0 as you suggested.

Zsolt

@csernazs csernazs merged commit 862d4af into csernazs:master Aug 11, 2024
10 checks passed
csernazs added a commit that referenced this pull request Aug 11, 2024
This is fixed in #352, and here is the test for it.
csernazs added a commit that referenced this pull request Aug 11, 2024
This is fixed in #352, and here is the test for it.
csernazs added a commit that referenced this pull request Aug 11, 2024
This is fixed in #352, and here is the test for it.
csernazs added a commit that referenced this pull request Aug 11, 2024
This is fixed in #352, and here is the test for it.
csernazs added a commit that referenced this pull request Aug 11, 2024
csernazs added a commit that referenced this pull request Aug 11, 2024
@csernazs
Copy link
Owner

@netaneld122 version 1.1.0 has just been released!

Thanks for your fix!

Zsolt

edgarrmondragon pushed a commit to MeltanoLabs/tap-stackexchange that referenced this pull request Aug 12, 2024
…evelopment-dependencies group (#454)

Bumps the development-dependencies group with 1 update:
[pytest-httpserver](https://github.com/csernazs/pytest-httpserver).

Updates `pytest-httpserver` from 1.0.12 to 1.1.0
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/csernazs/pytest-httpserver/releases">pytest-httpserver's
releases</a>.</em></p>
<blockquote>
<h2>1.1.0</h2>
<h2>What's Changed</h2>
<ul>
<li>httpserver: Clear state in function-scoped fixtures before the test
by <a
href="https://github.com/netaneld122"><code>@​netaneld122</code></a> in
<a
href="https://redirect.github.com/csernazs/pytest-httpserver/pull/352">csernazs/pytest-httpserver#352</a></li>
</ul>
<p><strong>Full Changelog</strong>: <a
href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0</a></p>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/csernazs/pytest-httpserver/blob/master/CHANGES.rst">pytest-httpserver's
changelog</a>.</em></p>
<blockquote>
<h1>1.1.0</h1>
<p>.. _Release Notes_1.1.0_Bug Fixes:</p>
<h2>Bug Fixes</h2>
<ul>
<li>Fixed an issue related to the leak of httpserver state between the
tests
when httpserver is destructed before the other fixtures.
<code>[#352](csernazs/pytest-httpserver#352)
&lt;https://github.com/csernazs/pytest-httpserver/issues/352&gt;</code>_</li>
</ul>
<p>.. _Release Notes_1.0.12:</p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/4f19faf89bb1fc6d2b637d6824bc4df90be027be"><code>4f19faf</code></a>
CHANGES.rst: add release notes for 1.1.0</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/1b01241affebad9d806646bcecbf9134e3d2877c"><code>1b01241</code></a>
Version bump to 1.1.0</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/86c96670220af73fdb2b146418275f50c6304225"><code>86c9667</code></a>
releasenotes: add release note for <a
href="https://redirect.github.com/csernazs/pytest-httpserver/issues/352">#352</a></li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/2fed2b6fd0b3fbeb47a47bf9721c4567eeb3785e"><code>2fed2b6</code></a>
tests: add test for log leak</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/862d4afe489d3ebf6e5e57585ec530bc9df1e3e0"><code>862d4af</code></a>
httpserver: Cleanup state in function scoped fixtures before the
test</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/0eb610bc87e3b6cd4f47dad574691a430b95282c"><code>0eb610b</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/a178b96dac5821ad47ca1722f8a6108261267b36"><code>a178b96</code></a>
build(deps-dev): bump the deps group with 3 updates</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/7df2d1cede92144e648049505695bc88251fa206"><code>7df2d1c</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li>See full diff in <a
href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=pytest-httpserver&package-manager=pip&previous-version=1.0.12&new-version=1.1.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore <dependency name> major version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's major version (unless you unignore this specific
dependency's major version or upgrade to it yourself)
- `@dependabot ignore <dependency name> minor version` will close this
group update PR and stop Dependabot creating any more for the specific
dependency's minor version (unless you unignore this specific
dependency's minor version or upgrade to it yourself)
- `@dependabot ignore <dependency name>` will close this group update PR
and stop Dependabot creating any more for the specific dependency
(unless you unignore this specific dependency or upgrade to it yourself)
- `@dependabot unignore <dependency name>` will remove all of the ignore
conditions of the specified dependency
- `@dependabot unignore <dependency name> <ignore condition>` will
remove the ignore condition of the specified dependency and ignore
conditions


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
zifeo pushed a commit to zifeo/dataconf that referenced this pull request Sep 1, 2024
Bumps [pytest-httpserver](https://github.com/csernazs/pytest-httpserver)
from 1.0.12 to 1.1.0.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a
href="https://github.com/csernazs/pytest-httpserver/releases">pytest-httpserver's
releases</a>.</em></p>
<blockquote>
<h2>1.1.0</h2>
<h2>What's Changed</h2>
<ul>
<li>httpserver: Clear state in function-scoped fixtures before the test
by <a
href="https://github.com/netaneld122"><code>@​netaneld122</code></a> in
<a
href="https://redirect.github.com/csernazs/pytest-httpserver/pull/352">csernazs/pytest-httpserver#352</a></li>
</ul>
<p><strong>Full Changelog</strong>: <a
href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0</a></p>
</blockquote>
</details>
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/csernazs/pytest-httpserver/blob/master/CHANGES.rst">pytest-httpserver's
changelog</a>.</em></p>
<blockquote>
<h1>1.1.0</h1>
<p>.. _Release Notes_1.1.0_Bug Fixes:</p>
<h2>Bug Fixes</h2>
<ul>
<li>Fixed an issue related to the leak of httpserver state between the
tests
when httpserver is destructed before the other fixtures.
<code>[#352](csernazs/pytest-httpserver#352)
&lt;https://github.com/csernazs/pytest-httpserver/issues/352&gt;</code>_</li>
</ul>
<p>.. _Release Notes_1.0.12:</p>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/4f19faf89bb1fc6d2b637d6824bc4df90be027be"><code>4f19faf</code></a>
CHANGES.rst: add release notes for 1.1.0</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/1b01241affebad9d806646bcecbf9134e3d2877c"><code>1b01241</code></a>
Version bump to 1.1.0</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/86c96670220af73fdb2b146418275f50c6304225"><code>86c9667</code></a>
releasenotes: add release note for <a
href="https://redirect.github.com/csernazs/pytest-httpserver/issues/352">#352</a></li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/2fed2b6fd0b3fbeb47a47bf9721c4567eeb3785e"><code>2fed2b6</code></a>
tests: add test for log leak</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/862d4afe489d3ebf6e5e57585ec530bc9df1e3e0"><code>862d4af</code></a>
httpserver: Cleanup state in function scoped fixtures before the
test</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/0eb610bc87e3b6cd4f47dad574691a430b95282c"><code>0eb610b</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/a178b96dac5821ad47ca1722f8a6108261267b36"><code>a178b96</code></a>
build(deps-dev): bump the deps group with 3 updates</li>
<li><a
href="https://github.com/csernazs/pytest-httpserver/commit/7df2d1cede92144e648049505695bc88251fa206"><code>7df2d1c</code></a>
[pre-commit.ci] pre-commit autoupdate</li>
<li>See full diff in <a
href="https://github.com/csernazs/pytest-httpserver/compare/1.0.12...1.1.0">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=pytest-httpserver&package-manager=pip&previous-version=1.0.12&new-version=1.1.0)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
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.

2 participants