Skip to content

Commit c7ddd7b

Browse files
committed
Rename event_bus -> bus
1 parent 7ff41fc commit c7ddd7b

10 files changed

Lines changed: 65 additions & 65 deletions

File tree

ascetic_ddd/bus/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from ascetic_ddd.bus.in_memory_bus import *
2+
from ascetic_ddd.bus.interfaces import *
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import collections
22
import typing
33

4+
from ascetic_ddd.bus.interfaces import IBus, IHandler
45
from ascetic_ddd.disposable import Disposable, IDisposable
5-
from ascetic_ddd.event_bus.interfaces import IEventBus, IEventHandler
66

7-
__all__ = ("InMemoryEventBus",)
7+
__all__ = ("InMemoryBus",)
88

99
SessionT = typing.TypeVar("SessionT")
10-
EventT = typing.TypeVar("EventT")
10+
MessageT = typing.TypeVar("MessageT")
1111

1212

13-
class InMemoryEventBus(IEventBus[SessionT], typing.Generic[SessionT]):
13+
class InMemoryBus(IBus[SessionT], typing.Generic[SessionT]):
1414
def __init__(self) -> None:
1515
self._subscribers: collections.defaultdict[str, list] = collections.defaultdict(list)
1616

17-
async def publish(self, session: SessionT, uri: str, event: EventT) -> None:
17+
async def publish(self, session: SessionT, uri: str, message: MessageT) -> None:
1818
for handler in self._subscribers[uri]:
19-
await handler(session, uri, event)
19+
await handler(session, uri, message)
2020

21-
async def subscribe(self, uri: str, handler: IEventHandler[SessionT, EventT]) -> IDisposable:
21+
async def subscribe(self, uri: str, handler: IHandler[SessionT, MessageT]) -> IDisposable:
2222
self._subscribers[uri].append(handler)
2323

2424
async def callback() -> None:
2525
await self.unsubscribe(uri, handler)
2626

2727
return Disposable(callback)
2828

29-
async def unsubscribe(self, uri: str, handler: IEventHandler[SessionT, EventT]) -> None:
29+
async def unsubscribe(self, uri: str, handler: IHandler[SessionT, MessageT]) -> None:
3030
self._subscribers[uri].remove(handler)

ascetic_ddd/bus/interfaces.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import typing
2+
from abc import ABCMeta, abstractmethod
3+
4+
from ascetic_ddd.disposable import IDisposable
5+
6+
__all__ = (
7+
"IBus",
8+
"IHandler",
9+
)
10+
11+
SessionT = typing.TypeVar("SessionT")
12+
MessageT = typing.TypeVar("MessageT")
13+
14+
SessionT_contra = typing.TypeVar("SessionT_contra", contravariant=True)
15+
MessageT_contra = typing.TypeVar("MessageT_contra", contravariant=True)
16+
17+
18+
class IHandler(typing.Protocol[SessionT_contra, MessageT_contra]):
19+
def __call__(self, session: SessionT_contra, uri: str, message: MessageT_contra):
20+
...
21+
22+
23+
class IBus(typing.Generic[SessionT], metaclass=ABCMeta):
24+
@abstractmethod
25+
async def publish(self, session: SessionT, uri: str, message: MessageT) -> None:
26+
raise NotImplementedError
27+
28+
@abstractmethod
29+
async def subscribe(self, uri: str, handler: IHandler[SessionT, MessageT]) -> IDisposable:
30+
raise NotImplementedError
31+
32+
@abstractmethod
33+
async def unsubscribe(self, uri: str, handler: IHandler[SessionT, MessageT]) -> None:
34+
raise NotImplementedError
File renamed without changes.

ascetic_ddd/event_bus/tests/event_bus/test_event_bus.py renamed to ascetic_ddd/bus/tests/bus/test_bus.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,45 @@
33
from contextlib import contextmanager
44
from unittest import IsolatedAsyncioTestCase, mock
55

6-
from ascetic_ddd.event_bus.in_memory_event_bus import InMemoryEventBus
6+
from ascetic_ddd.bus.in_memory_bus import InMemoryBus
77

88

9-
class InMemoryEventBusTestCase(IsolatedAsyncioTestCase):
9+
class InMemoryBusTestCase(IsolatedAsyncioTestCase):
1010
session = object()
1111
uri = "sb://test-uri"
12-
event = {"a": 5}
12+
message = {"a": 5}
1313

1414
def setUp(self) -> None:
15-
self.event_bus = InMemoryEventBus()
15+
self.bus = InMemoryBus()
1616

1717
async def test_publish(self):
1818
handler = mock.AsyncMock()
19-
await self.event_bus.subscribe(self.uri, handler)
19+
await self.bus.subscribe(self.uri, handler)
2020
with self._wait_for_response_until(lambda: handler.called):
21-
await self.event_bus.publish(self.session, self.uri, self.event)
22-
handler.assert_called_once_with(self.session, self.uri, self.event)
21+
await self.bus.publish(self.session, self.uri, self.message)
22+
handler.assert_called_once_with(self.session, self.uri, self.message)
2323

2424
async def test_unsubscribe(self):
2525
handler = mock.AsyncMock()
2626
handler2 = mock.AsyncMock()
27-
await self.event_bus.subscribe(self.uri, handler)
28-
await self.event_bus.subscribe(self.uri, handler2)
29-
await self.event_bus.unsubscribe(self.uri, handler)
27+
await self.bus.subscribe(self.uri, handler)
28+
await self.bus.subscribe(self.uri, handler2)
29+
await self.bus.unsubscribe(self.uri, handler)
3030
with self._wait_for_response_until(lambda: handler2.called):
31-
await self.event_bus.publish(self.session, self.uri, self.event)
31+
await self.bus.publish(self.session, self.uri, self.message)
3232
handler.assert_not_called()
33-
handler2.assert_called_once_with(self.session, self.uri, self.event)
33+
handler2.assert_called_once_with(self.session, self.uri, self.message)
3434

35-
async def test_disposable_event(self):
35+
async def test_disposable(self):
3636
handler = mock.AsyncMock()
3737
handler2 = mock.AsyncMock()
38-
disposable = await self.event_bus.subscribe(self.uri, handler)
39-
await self.event_bus.subscribe(self.uri, handler2)
38+
disposable = await self.bus.subscribe(self.uri, handler)
39+
await self.bus.subscribe(self.uri, handler2)
4040
await disposable.dispose()
4141
with self._wait_for_response_until(lambda: handler2.called):
42-
await self.event_bus.publish(self.session, self.uri, self.event)
42+
await self.bus.publish(self.session, self.uri, self.message)
4343
handler.assert_not_called()
44-
handler2.assert_called_once_with(self.session, self.uri, self.event)
44+
handler2.assert_called_once_with(self.session, self.uri, self.message)
4545

4646
@staticmethod
4747
def _wait_for_response_until(*checkers):

ascetic_ddd/event_bus/__init__.py

Lines changed: 0 additions & 2 deletions
This file was deleted.

ascetic_ddd/event_bus/interfaces.py

Lines changed: 0 additions & 34 deletions
This file was deleted.

ascetic_ddd/factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ascetic_ddd.event_bus import IEventBus, InMemoryEventBus
1+
from ascetic_ddd.bus import IBus, InMemoryBus
22
from ascetic_ddd.session.interfaces import ISession
33
from ascetic_ddd.utils.amemo import amemo
44

@@ -10,8 +10,8 @@
1010

1111
class BuildingBlocksFactory:
1212
@amemo
13-
async def make_in_memory_event_bus(self) -> IEventBus[ISession]:
14-
return InMemoryEventBus[ISession]()
13+
async def make_in_memory_bus(self) -> IBus[ISession]:
14+
return InMemoryBus[ISession]()
1515

1616

1717
ascetic_ddd_factory = BuildingBlocksFactory()

docs/architecture/overview.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Package Structure
4141
+-- outbox/ # Transactional outbox
4242
+-- inbox/ # Idempotent inbox
4343
+-- saga/ # Saga orchestration
44-
+-- event_bus/ # In-process event bus
44+
+-- bus/ # In-process message bus
4545
+-- mediator/ # Command/query mediator
4646
+-- signals/ # Typed Signal pattern
4747
+-- session/ # Session/UoW management

0 commit comments

Comments
 (0)