Skip to content

Commit

Permalink
Merge pull request #93 from gamultong/feature/add-event-recorder
Browse files Browse the repository at this point in the history
EventRecorder 추가
  • Loading branch information
onee-only authored Dec 29, 2024
2 parents 522f862 + 73ff96a commit c911e4e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 12 deletions.
1 change: 1 addition & 0 deletions event/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .internal.event_broker import EventBroker, Receiver
from .internal.event_recorder import EventRecorder
from .internal.exceptions import NoMatchingReceiverException
10 changes: 4 additions & 6 deletions event/internal/event_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from .exceptions import NoMatchingReceiverException
from message.internal.message import EVENT_TYPE
from uuid import uuid4
from datetime import datetime

from .event_recorder import EventRecorder


class Receiver(Generic[EVENT_TYPE]):
Expand Down Expand Up @@ -74,19 +77,14 @@ def remove_receiver(receiver: Receiver):

@staticmethod
async def publish(message: Message):
EventBroker._debug(message)

if message.event not in EventBroker.event_dict:
raise NoMatchingReceiverException(message.event)

coroutines = []
coroutines = [EventRecorder.record(timestamp=datetime.now(), msg=message)]

receiver_ids = EventBroker.event_dict[message.event]
for id in receiver_ids:
receiver = Receiver.get_receiver(id)
coroutines.append(receiver(message))

await asyncio.gather(*coroutines)

def _debug(message: Message):
print(message.to_str(del_header=False))
38 changes: 38 additions & 0 deletions event/internal/event_recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from message import Message
from db import db
from datetime import datetime
import asyncio
import json

TABLE_NAME = "events"


async def init_table():
await db.execute(f"""
CREATE TABLE IF NOT EXISTS {TABLE_NAME}(
event TEXT NOT NULL,
timestamp FLOAT NOT NULL,
header TEXT NOT NULL,
payload TEXT NOT NULL
)""")

asyncio.run(init_table())


class EventRecorder:
async def record(timestamp: datetime, msg: Message):
header = json.dumps(obj=msg.header, sort_keys=True)
payload = json.dumps(obj=msg.payload, default=lambda o: o.__dict__, sort_keys=True)

await db.execute(
f"""
INSERT INTO {TABLE_NAME} (event, timestamp, header, payload)
VALUES (:event, :timestamp, :header, :payload)
""",
{
"event": msg.event,
"timestamp": timestamp.timestamp(),
"header": header,
"payload": payload
}
)
1 change: 1 addition & 0 deletions event/test/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .event_broker_test import EventBrokerTestCase
from .event_recorder_test import EventRecorderTestCase

import unittest

Expand Down
12 changes: 8 additions & 4 deletions event/test/event_broker_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import unittest
from unittest.mock import MagicMock, AsyncMock
from unittest.mock import MagicMock, AsyncMock, patch

from message import Message
from event import EventBroker, Receiver, NoMatchingReceiverException
from .utils import clear_records


class EventBrokerTestCase(unittest.IsolatedAsyncioTestCase):
Expand All @@ -19,7 +20,8 @@ def setUp(self):
self.handler.receive_a = self.func_receive_a
self.handler.receive_b = self.func_receive_b

def tearDown(self):
async def asyncTearDown(self):
await clear_records()
EventBroker.remove_receiver(self.handler.receive_a)
EventBroker.remove_receiver(self.handler.receive_b)

Expand All @@ -36,7 +38,8 @@ def test_remove_receiver(self):
self.assertNotIn(self.handler.receive_a.id, Receiver.receiver_dict)
self.assertNotIn("example_a", EventBroker.event_dict)

async def test_publish(self):
@patch("event.EventRecorder.record")
async def test_publish(self, mock: AsyncMock):
message = Message(event="example_a", payload=None)

await EventBroker.publish(message=message)
Expand All @@ -45,7 +48,8 @@ async def test_publish(self):
mock_message = self.handler.receive_a.func.mock_calls[0].args[0]
self.assertEqual(mock_message.event, message.event)

async def test_multiple_receiver_publish(self):
@patch("event.EventRecorder.record")
async def test_multiple_receiver_publish(self, mock: AsyncMock):
message_b = Message(event="example_b", payload=None)
await EventBroker.publish(message=message_b)

Expand Down
35 changes: 35 additions & 0 deletions event/test/event_recorder_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import unittest

from event import EventRecorder

from message import Message
from message.payload import ErrorEvent, ErrorPayload
from .utils import clear_records

from datetime import datetime


class EventRecorderTestCase(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self):
await clear_records()

async def asyncTearDown(self):
await clear_records()

async def test_record(self):
message = Message(
event=ErrorEvent.ERROR,
header={
"ayo": "pizza here",
"thisisint": 1
},
payload=ErrorPayload(msg="heelo world")
)

timestamp = datetime.now()

await EventRecorder.record(timestamp, message)


if __name__ == "__main__":
unittest.main()
5 changes: 5 additions & 0 deletions event/test/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from db import db


async def clear_records():
await db.execute("DELETE FROM events")
7 changes: 5 additions & 2 deletions server_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from server import app
from message import Message
from message.payload import FetchTilesPayload, TilesPayload, TilesEvent, NewConnEvent
from board.data.storage.test.fixtures import setup_board
from board.data.storage.test.fixtures import setup_board, teardown_board
from board.event.handler import BoardEventHandler
from board.data import Point, Tile, Tiles
from event import EventBroker
from event.test.utils import clear_records
from conn.manager import ConnectionManager

import unittest
Expand All @@ -19,7 +20,9 @@ async def asyncSetUp(self):
await setup_board()
self.client = TestClient(app)

def tearDown(self):
async def asyncTearDown(self):
await teardown_board()
await clear_records()
self.client.params = {}
self.client.close()

Expand Down

0 comments on commit c911e4e

Please sign in to comment.