Skip to content

Add ReuseServerTestCase for class-scoped server reuse - #13

Open
Fniakate8 wants to merge 3 commits into
valkey-io:unstablefrom
Fniakate8:server-reuse-upstream
Open

Add ReuseServerTestCase for class-scoped server reuse#13
Fniakate8 wants to merge 3 commits into
valkey-io:unstablefrom
Fniakate8:server-reuse-upstream

Conversation

@Fniakate8

@Fniakate8 Fniakate8 commented Jul 29, 2026

Copy link
Copy Markdown

This PR implements server reuse for test classes, reducing per-test overhead from (FLUSHALL + CONFIG RESETSTAT).

Whats Added

ReuseServerTestCase now inherits ValkeyTestCase and overrides create_server() to cache the server on first call instead of spawning a new one per test
Configs are snapshotted on server creation and restored between tests to prevent pollution
FLUSHALL + CONFIG RESETSTAT runs between each test for data isolation
Bumps pytest to 7.4.3
How to use

To adopt server reuse in your module, you need to:

  1. Import ReuseServerTestCase from the framework

  2. Change your test base class to inherit from ReuseServerTestCase instead of ValkeyTestCase

  3. your existingsetup_test, create_server()calls, and self.server/self.client assignments all work unchanged.

How I tested with other modules

Bloom module — Changed ValkeyBloomTestCaseBaseto inherit ReuseServerTestCase. Ran test_bloom_basic.py (34 tests). All 34 passed. Tests that use CONFIG SET
(like bf.bloom-memory-usage-limit, maxmemory) no longer pollute later tests thanks to the automatic config restore.
JSON module — Changed JsonTestCase to inherit ReuseServerTestCase. Ran test_json_basic.py (171 tests). 170 passed, 1 failed due to a pre-existing missing SOURCE_DIR env var (unrelated to server reuse).

Server reuse helps most when module loading is expensive (like JSON) and tests are lightweight data operations. Bloom sees less improvement because its tests are compute-heavy (filling filters, sleeping for expiration).

Test plan

  • Bloom: all 34 tests pass with only base class change
  • JSON: 170/171 pass with only base class change
  • Config-heavy tests no longer pollute subsequent tests
  • Tests that need a fresh server can stay on ValkeyTestCase

@Fniakate8
Fniakate8 force-pushed the server-reuse-upstream branch 2 times, most recently from e94d1d4 to 5006368 Compare July 29, 2026 01:14
Fanta Niakate added 2 commits July 30, 2026 17:45
Instead of spawning a fresh valkey-server per test (~42ms), one server
is started per class and FLUSHALL + CONFIG RESETSTAT (~5ms) run between
tests to reset state. This gives ~5-7x speedup for test classes.

Changes:
- src/conftest.py: add class_port_tracker fixture (scope="class")
- src/valkey_test_case.py: add ReuseServerTestCase class
- tests/test_reuse_server.py: demo/test proving reuse and isolation

Signed-off-by: Fanta Niakate <niakatf@amazon.com>
ReuseServerTestCase now inherits ValkeyTestCase and overrides
create_server() to cache the server on first call. Modules only need to
change their base class — no other code modifications required.

Key changes:
- Inherit ValkeyTestCase instead of ValkeyTestCaseBase so all fixtures
  (setup, port_tracker_fixture) work automatically
- Override create_server() to return cached server on subsequent calls
- Snapshot all configs on first creation and restore between tests to
  prevent config pollution across tests
- FLUSHALL + CONFIG RESETSTAT between tests for data isolation
- Bump pytest to 7.4.3

Tested against bloom (34/34 pass) and JSON (170/171 pass, 1 unrelated
env var issue). JSON sees ~17x speedup (8s vs 138s).

Signed-off-by: Fanta Niakate <niakatf@amazon.com>
@Fniakate8
Fniakate8 force-pushed the server-reuse-upstream branch from de8c642 to 88d0f96 Compare July 30, 2026 17:50

@chinguyen21 chinguyen21 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Overall looks better a lot after the 2nd change. Just a few things to improve. Please check the comments



class TestReuseServer(ReuseServerTestCase):
"""Verifies that server reuse works and tests are isolated."""

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Lack of setup_test to call create_server() after redesigning the ReuseServerTestCase in the 2nd commit

@Fniakate8 Fniakate8 Jul 30, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

you're correct. After the redesign of the server reuse, the server is only created when create_server() is first called. I'll add a setup_test fixture taht calls create_server().

Comment thread requirements.txt Outdated
@@ -1,4 +1,4 @@
valkey
pytest==6
pytest==7.4.3

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do we need this change? Doesn't pytest6 work?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The pytest 6 works but it throws deprecation warnings for the teardown(self) method and pytest 7 handles fixture scoping more reliably (like our class-scoped class_teardown fixture.) but for simplicity I'll remove it.

Comment thread src/valkey_test_case.py Outdated
def teardown(self):
if hasattr(self.__class__, '_shared_server') and self.__class__._shared_server:
client = self.__class__._shared_client
client.flushall()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the server somehow crashes during a test, this will raise an exception. Can we handle it? And make sure if exception happens, we should discard the shared server, so other tests can still run properly

@Fniakate8 Fniakate8 Jul 30, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed. I wrapped the teardown in a try/except. If the server crashes, we can just discard _shared_server and _shared_client by setting them to None. The next test's create_server will see None and spin up a new server for concurrent tests.

Comment thread src/valkey_test_case.py

def create_server(
self,
testdir=None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is there any benefit to not require testdir?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added a fallback: if testdir isn't passed, it defaults to self.testdir (same approach as server_path). This way None never reaches the parent, and modules that already pass testdir=self.testdir still work the same.

@Fniakate8
Fniakate8 force-pushed the server-reuse-upstream branch from 482ecaa to 53af29f Compare July 30, 2026 23:59
@zackcam

zackcam commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

I let workflows run seems like there are some formatting issues can run black . to fix formatting issues.

Comment thread src/conftest.py Outdated


@pytest.fixture(scope="class")
def class_port_tracker(request):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Do we need this still? I saw we had an import for this but removed it

- Handle server crash in teardown: if flushall/config reset fails,
  discard the shared server so next test creates a fresh one
- Default testdir to self.testdir when not passed (same pattern as
  server_path) so None never reaches the parent
- Add setup_test fixture to test_reuse_server.py to call create_server()
  matching the pattern modules use
- Remove unused class_port_tracker import from test file
- Revert pytest version bump — not needed for server reuse

Signed-off-by: Fanta Niakate <niakatf@amazon.com>
@Fniakate8
Fniakate8 force-pushed the server-reuse-upstream branch from 53af29f to c3685d4 Compare July 31, 2026 21:06
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.

3 participants