chainsyncer/tests/test_session.py

156 lines
4.5 KiB
Python
Raw Normal View History

2022-03-18 00:48:23 +01:00
# standard imports
import unittest
import tempfile
import shutil
import logging
import stat
import os
# local imports
from chainsyncer.store.fs import SyncFsStore
from chainsyncer.session import SyncSession
from chainsyncer.error import (
LockError,
FilterDone,
IncompleteFilterError,
SyncDone,
)
from chainsyncer.unittest import (
2022-03-29 09:28:28 +02:00
MockBlockGenerator,
2022-03-18 00:48:23 +01:00
MockFilter,
MockConn,
MockTx,
MockBlock,
2022-03-18 02:11:30 +01:00
MockDriver,
2022-03-21 22:03:24 +01:00
MockFilterError,
state_event_handler,
filter_state_event_handler,
2022-03-18 00:48:23 +01:00
)
from chainsyncer.driver import SyncDriver
logging.basicConfig(level=logging.STATETRACE)
2022-03-18 00:48:23 +01:00
logg = logging.getLogger()
class TestFilter(unittest.TestCase):
def setUp(self):
self.path = tempfile.mkdtemp()
self.store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
2022-03-18 00:48:23 +01:00
self.conn = MockConn()
def tearDown(self):
shutil.rmtree(self.path)
def test_filter_basic(self):
session = SyncSession(self.store)
session.start(target=1)
fltr_one = MockFilter('foo')
self.store.register(fltr_one)
2022-03-18 00:48:23 +01:00
tx_hash = os.urandom(32).hex()
tx = MockTx(42, tx_hash)
block = MockBlock(0, [tx_hash])
session.filter(self.conn, block, tx)
tx_hash = os.urandom(32).hex()
tx = MockTx(42, tx_hash)
block = MockBlock(1, [tx_hash])
2022-03-18 02:11:30 +01:00
session.filter(self.conn, block, tx)
2022-03-18 00:48:23 +01:00
self.assertEqual(len(fltr_one.contents), 2)
def test_driver(self):
2022-03-18 02:11:30 +01:00
drv = MockDriver(self.store, target=1)
2022-03-29 09:28:28 +02:00
generator = MockBlockGenerator()
generator.generate([1, 2], driver=drv)
2022-03-18 02:11:30 +01:00
fltr_one = MockFilter('foo')
self.store.register(fltr_one)
with self.assertRaises(SyncDone):
drv.run(self.conn)
self.assertEqual(len(fltr_one.contents), 3)
2022-03-18 00:48:23 +01:00
2022-03-29 08:56:21 +02:00
def test_driver_interrupt_noresume(self):
2022-03-21 22:03:24 +01:00
drv = MockDriver(self.store, target=1)
2022-03-29 09:28:28 +02:00
generator = MockBlockGenerator()
generator.generate([1], driver=drv)
2022-03-21 22:03:24 +01:00
fltr_one = MockFilter('foo')
2022-03-21 22:03:24 +01:00
self.store.register(fltr_one)
fltr_two = MockFilter('bar', brk_hard=1)
2022-03-21 22:03:24 +01:00
self.store.register(fltr_two)
with self.assertRaises(MockFilterError):
drv.run(self.conn)
self.assertEqual(len(fltr_one.contents), 1)
self.assertEqual(len(fltr_two.contents), 0)
store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
2022-03-29 08:56:21 +02:00
fltr_one = MockFilter('foo') #, brk_hard=1)
store.register(fltr_one)
fltr_two = MockFilter('bar')
store.register(fltr_two)
2022-03-21 22:03:24 +01:00
with self.assertRaises(LockError):
drv = MockDriver(store, target=1)
2022-03-21 22:03:24 +01:00
self.assertEqual(len(fltr_one.contents), 0)
self.assertEqual(len(fltr_two.contents), 0)
2022-03-21 22:03:24 +01:00
2022-03-29 09:28:28 +02:00
def test_driver_interrupt_filter(self):
2022-03-29 08:56:21 +02:00
drv = MockDriver(self.store, target=1)
2022-03-29 09:28:28 +02:00
generator = MockBlockGenerator()
generator.generate([1, 1], driver=drv)
2022-03-29 08:56:21 +02:00
fltr_one = MockFilter('foo')
2022-03-29 08:56:21 +02:00
self.store.register(fltr_one)
fltr_two = MockFilter('bar', brk=1)
2022-03-29 08:56:21 +02:00
self.store.register(fltr_two)
fltr_three = MockFilter('baz')
self.store.register(fltr_three)
2022-03-29 08:56:21 +02:00
store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
with self.assertRaises(SyncDone):
drv.run(self.conn)
self.assertEqual(len(fltr_one.contents), 2)
self.assertEqual(len(fltr_two.contents), 2)
self.assertEqual(len(fltr_three.contents), 1)
2022-03-29 08:56:21 +02:00
2022-03-29 09:28:28 +02:00
def test_driver_interrupt_sync(self):
2022-03-29 14:56:15 +02:00
drv = MockDriver(self.store, interrupt_block=1, target=2)
2022-03-29 09:28:28 +02:00
generator = MockBlockGenerator()
2022-03-29 10:12:43 +02:00
generator.generate([3, 1, 2], driver=drv)
fltr_one = MockFilter('foo')
self.store.register(fltr_one)
drv.run(self.conn, interval=0.1)
self.assertEqual(len(fltr_one.contents), 3)
2022-03-29 10:12:43 +02:00
store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
store.register(fltr_one)
2022-03-29 14:56:15 +02:00
drv = MockDriver(store)
2022-03-29 10:12:43 +02:00
generator.apply(drv, offset=1)
2022-03-29 14:56:15 +02:00
with self.assertRaises(SyncDone) as e:
drv.run(self.conn, interval=0.1)
self.assertEqual(e, 2)
2022-03-29 09:28:28 +02:00
self.assertEqual(len(fltr_one.contents), 6)
2022-03-29 09:28:28 +02:00
2022-03-18 00:48:23 +01:00
if __name__ == '__main__':
unittest.main()