-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(test): add performance tests to check it doesn't timeout in edge…
… cases
- Loading branch information
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[tool.poetry] | ||
name = "python-redux" | ||
version = "0.15.0" | ||
version = "0.15.1" | ||
description = "Redux implementation for Python" | ||
authors = ["Sassan Haradji <[email protected]>"] | ||
license = "Apache-2.0" | ||
|
This file contains 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,88 @@ | ||
# ruff: noqa: D100, D101, D102, D103, D104, D107 | ||
|
||
from __future__ import annotations | ||
|
||
from dataclasses import replace | ||
from typing import Generator | ||
|
||
import pytest | ||
from immutable import Immutable | ||
|
||
from redux.basic_types import ( | ||
BaseAction, | ||
CompleteReducerResult, | ||
CreateStoreOptions, | ||
FinishAction, | ||
FinishEvent, | ||
InitAction, | ||
InitializationActionError, | ||
) | ||
from redux.main import Store | ||
|
||
|
||
class StateType(Immutable): | ||
value: int | ||
|
||
|
||
class IncrementAction(BaseAction): ... | ||
|
||
|
||
Action = IncrementAction | InitAction | FinishAction | ||
|
||
|
||
def reducer( | ||
state: StateType | None, | ||
action: Action, | ||
) -> StateType | CompleteReducerResult[StateType, Action, FinishEvent]: | ||
if state is None: | ||
if isinstance(action, InitAction): | ||
return StateType(value=0) | ||
raise InitializationActionError(action) | ||
|
||
if isinstance(action, IncrementAction): | ||
return replace(state, value=state.value + 1) | ||
|
||
return state | ||
|
||
|
||
class StoreType(Store[StateType, Action, FinishEvent]): | ||
@property | ||
def state(self: StoreType) -> StateType | None: | ||
return self._state | ||
|
||
|
||
@pytest.fixture() | ||
def store() -> Generator[StoreType, None, None]: | ||
store = StoreType(reducer, options=CreateStoreOptions(auto_init=True)) | ||
yield store | ||
|
||
store.dispatch(FinishAction()) | ||
|
||
|
||
# These tests will timeout if they take a long time to run, indicating a performance | ||
# issue. | ||
|
||
|
||
def test_simple_dispatch(store: StoreType) -> None: | ||
count = 100000 | ||
for _ in range(count): | ||
store.dispatch(IncrementAction()) | ||
|
||
assert store.state is not None | ||
assert store.state.value == count | ||
|
||
|
||
def test_dispatch_with_subscriptions(store: StoreType) -> None: | ||
for _ in range(1000): | ||
|
||
def callback(_: StateType | None) -> None: | ||
pass | ||
|
||
store.subscribe(callback) | ||
|
||
count = 500 | ||
for _ in range(count): | ||
store.dispatch(IncrementAction()) | ||
|
||
assert store.state is not None | ||
assert store.state.value == count |