async def test_register(db_path, create_event): conf = { 'db_path': str(db_path), 'max_db_size': 1024 * 1024 * 1024, 'sync_period': 30, 'server_id': 1, 'conditions': [{ 'subscriptions': [('f', )], 'condition': { 'type': 'json' } }], 'latest': { 'subscriptions': [['*']] }, 'ordered': [{ 'order_by': 'TIMESTAMP', 'subscriptions': [['*']] }] } backend = await hat.event.server.backends.lmdb.backend.create(conf) result = await backend.query(common.QueryData()) assert result == [] event1 = create_event(('a', )) await asyncio.sleep(0.001) event2 = create_event(('b', ))._replace(event_id=common.EventId(2, 1)) await asyncio.sleep(0.001) event3 = create_event(('c', ))._replace(event_id=event1.event_id) await asyncio.sleep(0.001) event4 = create_event(('d', )) await asyncio.sleep(0.001) event5 = create_event(('e', ))._replace(timestamp=event1.timestamp) await asyncio.sleep(0.001) event6 = create_event(('f', )) await asyncio.sleep(0.001) event7 = create_event(('g', )) await asyncio.sleep(0.001) events = [event1, event2, event3, event4, event5, event6, event7] result = await backend.register(events) assert result == events result = await backend.query(common.QueryData()) assert result == [event7, event4, event1] await backend.async_close() backend = await hat.event.server.backends.lmdb.backend.create(conf) result = await backend.query(common.QueryData()) assert result == [event7, event4, event1] await backend.async_close()
def create_event(event_type, payload): nonlocal counter counter += 1 event_id = common.EventId(1, counter) event = common.Event(event_id=event_id, event_type=event_type, timestamp=common.now(), source_timestamp=None, payload=payload) return event
async def test_get_last_event_id(db_path, create_event): server_id = 1 conf = { 'db_path': str(db_path), 'max_db_size': 1024 * 1024 * 1024, 'sync_period': 30, 'server_id': server_id, 'conditions': [], 'latest': { 'subscriptions': [['*']] }, 'ordered': [{ 'order_by': 'TIMESTAMP', 'subscriptions': [['*']] }] } backend = await hat.event.server.backends.lmdb.backend.create(conf) event_id = await backend.get_last_event_id(server_id) assert event_id == common.EventId(server_id, 0) event_id = await backend.get_last_event_id(server_id + 1) assert event_id == common.EventId(server_id + 1, 0) event = create_event(('a', )) await backend.register([event]) event_id = await backend.get_last_event_id(server_id) assert event_id == event.event_id event_id = await backend.get_last_event_id(server_id + 1) assert event_id == common.EventId(server_id + 1, 0) await backend.async_close() backend = await hat.event.server.backends.lmdb.backend.create(conf) event_id = await backend.get_last_event_id(server_id) assert event_id == event.event_id await backend.async_close()
def create_event(event_type, with_source_timestamp): nonlocal counter counter += 1 event_id = common.EventId(1, counter) t = common.now() event = common.Event( event_id=event_id, event_type=event_type, timestamp=t, source_timestamp=(t if with_source_timestamp else None), payload=None) return event
def create_event(event_type, payload): return common.Event(event_id=common.EventId(1, 2), event_type=event_type, timestamp=common.now(), source_timestamp=None, payload=payload)
async def get_last_event_id(self, server_id: int) -> common.EventId: if server_id != self._sys_db.data.server_id: return common.EventId(server=server_id, instance=0) instance_id = self._sys_db.data.last_instance_id or 0 return common.EventId(server=server_id, instance=instance_id)
import pytest from hat.event.server.backends.lmdb import common import hat.event.server.backends.lmdb.encoder @pytest.mark.parametrize('event', [ common.Event(event_id=common.EventId(1, 2), event_type=('a', 'b', 'c'), timestamp=common.now(), source_timestamp=None, payload=None) ]) def test_event(event): encoded = hat.event.server.backends.lmdb.encoder.encode_event(event) decoded = hat.event.server.backends.lmdb.encoder.decode_event(encoded) assert event == decoded @pytest.mark.parametrize('timestamp_id', [ (common.now(), 123) ]) def test_timestamp_id(timestamp_id): encoded = hat.event.server.backends.lmdb.encoder.encode_timestamp_id( timestamp_id) decoded = hat.event.server.backends.lmdb.encoder.decode_timestamp_id( encoded) assert timestamp_id == decoded @pytest.mark.parametrize('value', [