-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
55 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from asyncio import gather | ||
from typing import NamedTuple | ||
|
||
from pytest import mark | ||
|
||
from sharded_queue import Handler, Queue, Route, Tube, Worker | ||
from sharded_queue.drivers import RuntimeLock, RuntimeStorage | ||
from sharded_queue.settings import settings | ||
|
||
|
||
class ActionMessage(NamedTuple): | ||
id: int | ||
|
||
|
||
class Action(NamedTuple): | ||
id: str | ||
|
||
|
||
class BaseAction(Handler): | ||
async def handle(self, *requests: ActionMessage) -> None: | ||
return gather(*[ | ||
self.perform(Action(request.id)) | ||
for request in requests | ||
]) | ||
... | ||
|
||
|
||
class CreateUserAction(BaseAction): | ||
async def perform(self, action: Action): | ||
... | ||
|
||
|
||
class UpdateUserAction(BaseAction): | ||
async def perform(self, action: Action): | ||
... | ||
|
||
|
||
@mark.asyncio | ||
async def test_nested() -> None: | ||
queue: Queue = Queue(RuntimeStorage()) | ||
|
||
await queue.register(CreateUserAction, Action(1)) | ||
await queue.register(UpdateUserAction, Action(2)) | ||
create_user_pipe = Tube(CreateUserAction, Route(0, 0)).pipe | ||
update_user_pipe = Tube(UpdateUserAction, Route(0, 0)).pipe | ||
|
||
assert await queue.storage.length(create_user_pipe) == 1 | ||
assert await queue.storage.length(update_user_pipe) == 1 | ||
|
||
settings.worker_empty_pause = 0 | ||
await Worker(RuntimeLock(), queue).loop(2) | ||
|
||
assert await queue.storage.length(create_user_pipe) == 0 | ||
assert await queue.storage.length(update_user_pipe) == 0 | ||
|