-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add in-memory state expiration to StateManagerMemory #6201
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
Open
FarhanAliRaza
wants to merge
5
commits into
reflex-dev:main
Choose a base branch
from
FarhanAliRaza:mem-expiration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+533
−13
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ab71d84
Add in-memory state expiration to StateManagerMemory
FarhanAliRaza c0b8742
Simplify StateManagerMemory expiration internals
FarhanAliRaza 2b0e097
Fix StateManager.create to respect explicit memory mode when Redis UR…
FarhanAliRaza cda08bb
feat: add in-memory state expiration to StateManagerMemory
FarhanAliRaza 9877312
feat: Addded expire extension
FarhanAliRaza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
tests/integration/test_memory_state_manager_expiration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| """Integration tests for in-memory state expiration.""" | ||
|
|
||
| import time | ||
| from collections.abc import Generator | ||
|
|
||
| import pytest | ||
| from selenium.webdriver.common.by import By | ||
| from selenium.webdriver.remote.webdriver import WebDriver | ||
|
|
||
| from reflex.istate.manager.memory import StateManagerMemory | ||
| from reflex.testing import AppHarness | ||
|
|
||
|
|
||
| def MemoryExpirationApp(): | ||
| """Reflex app that exposes state expiration through a simple counter UI.""" | ||
| import reflex as rx | ||
|
|
||
| class State(rx.State): | ||
| counter: int = 0 | ||
|
|
||
| @rx.event | ||
| def increment(self): | ||
| self.counter += 1 | ||
|
|
||
| app = rx.App() | ||
|
|
||
| @app.add_page | ||
| def index(): | ||
| return rx.vstack( | ||
| rx.input( | ||
| id="token", | ||
| value=State.router.session.client_token, | ||
| is_read_only=True, | ||
| ), | ||
| rx.text(State.counter, id="counter"), | ||
| rx.button("Increment", id="increment", on_click=State.increment), | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def memory_expiration_app( | ||
| app_harness_env: type[AppHarness], | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| tmp_path_factory: pytest.TempPathFactory, | ||
| ) -> Generator[AppHarness, None, None]: | ||
| """Start a memory-backed app with a short expiration window. | ||
|
|
||
| Yields: | ||
| A running app harness configured to use StateManagerMemory. | ||
| """ | ||
| monkeypatch.setenv("REFLEX_STATE_MANAGER_MODE", "memory") | ||
| # Memory expiration reuses the shared token_expiration config field. | ||
| monkeypatch.setenv("REFLEX_REDIS_TOKEN_EXPIRATION", "1") | ||
FarhanAliRaza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| with app_harness_env.create( | ||
| root=tmp_path_factory.mktemp("memory_expiration_app"), | ||
| app_name=f"memory_expiration_{app_harness_env.__name__.lower()}", | ||
| app_source=MemoryExpirationApp, | ||
| ) as harness: | ||
| assert isinstance(harness.state_manager, StateManagerMemory) | ||
| yield harness | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def driver(memory_expiration_app: AppHarness) -> Generator[WebDriver, None, None]: | ||
| """Open the memory expiration app in a browser. | ||
|
|
||
| Yields: | ||
| A webdriver instance pointed at the running app. | ||
| """ | ||
| assert memory_expiration_app.app_instance is not None, "app is not running" | ||
| driver = memory_expiration_app.frontend() | ||
| try: | ||
| yield driver | ||
| finally: | ||
| driver.quit() | ||
|
|
||
|
|
||
| def test_memory_state_manager_expires_state_end_to_end( | ||
| memory_expiration_app: AppHarness, | ||
| driver: WebDriver, | ||
| ): | ||
| """An idle in-memory state should expire and reset on the next event.""" | ||
| app_instance = memory_expiration_app.app_instance | ||
| assert app_instance is not None | ||
|
|
||
| token_input = AppHarness.poll_for_or_raise_timeout( | ||
| lambda: driver.find_element(By.ID, "token") | ||
| ) | ||
| token = memory_expiration_app.poll_for_value(token_input) | ||
| assert token is not None | ||
|
|
||
| counter = driver.find_element(By.ID, "counter") | ||
| increment = driver.find_element(By.ID, "increment") | ||
| app_state_manager = app_instance.state_manager | ||
| assert isinstance(app_state_manager, StateManagerMemory) | ||
|
|
||
| AppHarness.expect(lambda: counter.text == "0") | ||
|
|
||
| increment.click() | ||
| AppHarness.expect(lambda: counter.text == "1") | ||
|
|
||
| increment.click() | ||
| AppHarness.expect(lambda: counter.text == "2") | ||
|
|
||
| AppHarness.expect(lambda: token in app_state_manager.states) | ||
| AppHarness.expect(lambda: token not in app_state_manager.states, timeout=5) | ||
|
|
||
| increment.click() | ||
| AppHarness.expect(lambda: counter.text == "1") | ||
| assert token_input.get_attribute("value") == token | ||
|
|
||
|
|
||
| def test_memory_state_manager_delays_expiration_after_use_end_to_end( | ||
| memory_expiration_app: AppHarness, | ||
| driver: WebDriver, | ||
| ): | ||
| """Using a token should start a fresh expiration window from the last use.""" | ||
| app_instance = memory_expiration_app.app_instance | ||
| assert app_instance is not None | ||
|
|
||
| token_input = AppHarness.poll_for_or_raise_timeout( | ||
| lambda: driver.find_element(By.ID, "token") | ||
| ) | ||
| token = memory_expiration_app.poll_for_value(token_input) | ||
| assert token is not None | ||
|
|
||
| counter = driver.find_element(By.ID, "counter") | ||
| increment = driver.find_element(By.ID, "increment") | ||
| app_state_manager = app_instance.state_manager | ||
| assert isinstance(app_state_manager, StateManagerMemory) | ||
|
|
||
| AppHarness.expect(lambda: counter.text == "0") | ||
|
|
||
| increment.click() | ||
| AppHarness.expect(lambda: counter.text == "1") | ||
| AppHarness.expect(lambda: token in app_state_manager.states) | ||
|
|
||
| time.sleep(0.6) | ||
| increment.click() | ||
| AppHarness.expect(lambda: counter.text == "2") | ||
| AppHarness.expect(lambda: token in app_state_manager.states) | ||
|
|
||
| time.sleep(0.6) | ||
| increment.click() | ||
| AppHarness.expect(lambda: counter.text == "3") | ||
| AppHarness.expect(lambda: token in app_state_manager.states) | ||
|
|
||
| time.sleep(0.6) | ||
| assert token in app_state_manager.states | ||
| assert counter.text == "3" | ||
|
|
||
| AppHarness.expect(lambda: token not in app_state_manager.states, timeout=5) | ||
|
|
||
| increment.click() | ||
| AppHarness.expect(lambda: counter.text == "1") | ||
| assert token_input.get_attribute("value") == token | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.