-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add application states (#1974)
* feat: add application state * docs: generate API References --------- Co-authored-by: Artem-Safronov <[email protected]>
- Loading branch information
1 parent
7025d86
commit b84c032
Showing
4 changed files
with
85 additions
and
9 deletions.
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
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,28 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
from faststream._internal.state.fast_depends import DIState | ||
|
||
|
||
class ApplicationState(ABC): | ||
def __init__(self, di_state: DIState) -> None: | ||
self._di_state = di_state | ||
|
||
@property | ||
@abstractmethod | ||
def running(self) -> bool: ... | ||
|
||
@property | ||
def di_state(self) -> DIState: | ||
return self._di_state | ||
|
||
|
||
class BasicApplicationState(ApplicationState): | ||
@property | ||
def running(self) -> bool: | ||
return False | ||
|
||
|
||
class RunningApplicationState(ApplicationState): | ||
@property | ||
def running(self) -> bool: | ||
return True |
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,31 @@ | ||
from unittest.mock import AsyncMock, patch | ||
|
||
import pytest | ||
|
||
from faststream import FastStream | ||
|
||
|
||
@pytest.mark.asyncio() | ||
async def test_state_running(app: FastStream) -> None: | ||
with patch( | ||
"faststream._internal.application.Application.start", new_callable=AsyncMock | ||
): | ||
await app._startup() | ||
|
||
assert app.running | ||
|
||
|
||
@pytest.mark.asyncio() | ||
async def test_state_stopped(app: FastStream) -> None: | ||
with ( | ||
patch( | ||
"faststream._internal.application.Application.start", new_callable=AsyncMock | ||
), | ||
patch( | ||
"faststream._internal.application.Application.stop", new_callable=AsyncMock | ||
), | ||
): | ||
await app._startup() | ||
await app._shutdown() | ||
|
||
assert not app.running |