chainsyncer

Blockchain syncer driver
Info | Log | Files | Refs | LICENSE

test_session.py (6974B)


      1 # standard imports
      2 import unittest
      3 import tempfile
      4 import shutil
      5 import logging
      6 import stat
      7 import os
      8 
      9 # local imports
     10 from chainsyncer.store.fs import SyncFsStore
     11 from chainsyncer.session import SyncSession
     12 from chainsyncer.error import (
     13         LockError,
     14         FilterDone,
     15         IncompleteFilterError,
     16         SyncDone,
     17         )
     18 from chainsyncer.unittest import (
     19         MockBlockGenerator,
     20         MockFilter,
     21         MockConn,
     22         MockTx,
     23         MockBlock,
     24         MockDriver,
     25         MockFilterError,
     26         state_event_handler,
     27         filter_state_event_handler,
     28         )
     29 from chainsyncer.driver import SyncDriver
     30 
     31 logging.basicConfig(level=logging.STATETRACE)
     32 logg = logging.getLogger()
     33 
     34 
     35 class TestFilter(unittest.TestCase):
     36 
     37     def setUp(self):
     38         self.path = tempfile.mkdtemp()
     39         self.store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
     40         self.conn = MockConn()
     41 
     42 
     43 #    def tearDown(self):
     44 #        shutil.rmtree(self.path)
     45 
     46 
     47     def test_filter_basic(self):
     48         session = SyncSession(self.store)
     49         session.start(target=1)
     50         fltr_one = MockFilter('foo')
     51         self.store.register(fltr_one)
     52 
     53         tx_hash = os.urandom(32).hex()
     54         tx = MockTx(42, tx_hash)
     55         block = MockBlock(0, [tx_hash])
     56         session.filter(self.conn, block, tx)
     57  
     58         tx_hash = os.urandom(32).hex()
     59         tx = MockTx(42, tx_hash)
     60         block = MockBlock(1, [tx_hash])
     61         session.filter(self.conn, block, tx)
     62         self.assertEqual(len(fltr_one.contents), 2)
     63 
     64 
     65     def test_driver(self):
     66         drv = MockDriver(self.store, target=1)
     67         generator = MockBlockGenerator()
     68         generator.generate([1, 2], driver=drv)
     69 
     70         fltr_one = MockFilter('foo')
     71         self.store.register(fltr_one)
     72         with self.assertRaises(SyncDone):
     73             drv.run(self.conn)
     74 
     75         self.assertEqual(len(fltr_one.contents), 3)
     76 
     77 
     78     def test_driver_interrupt_noresume(self):
     79         drv = MockDriver(self.store, target=1)
     80         generator = MockBlockGenerator()
     81         generator.generate([1], driver=drv)
     82 
     83         fltr_one = MockFilter('foo')
     84         self.store.register(fltr_one)
     85         fltr_two = MockFilter('bar', brk_hard=1)
     86         self.store.register(fltr_two)
     87 
     88         with self.assertRaises(MockFilterError):
     89             drv.run(self.conn)
     90 
     91         self.assertEqual(len(fltr_one.contents), 1)
     92         self.assertEqual(len(fltr_two.contents), 0)
     93 
     94         store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
     95         
     96         fltr_one = MockFilter('foo') #, brk_hard=1)
     97         store.register(fltr_one)
     98         fltr_two = MockFilter('bar')
     99         store.register(fltr_two)
    100 
    101         with self.assertRaises(LockError):
    102             drv = MockDriver(store, target=1)
    103 
    104         self.assertEqual(len(fltr_one.contents), 0)
    105         self.assertEqual(len(fltr_two.contents), 0)
    106 
    107 
    108     def test_driver_interrupt_filter(self):
    109         drv = MockDriver(self.store, target=1)
    110         generator = MockBlockGenerator()
    111         generator.generate([1, 1], driver=drv)
    112 
    113         fltr_one = MockFilter('foo')
    114         self.store.register(fltr_one)
    115         fltr_two = MockFilter('bar', brk=1)
    116         self.store.register(fltr_two)
    117         fltr_three = MockFilter('baz')
    118         self.store.register(fltr_three)
    119 
    120         store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
    121 
    122         with self.assertRaises(SyncDone):
    123             drv.run(self.conn)
    124 
    125         self.assertEqual(len(fltr_one.contents), 2)
    126         self.assertEqual(len(fltr_two.contents), 2)
    127         self.assertEqual(len(fltr_three.contents), 1)
    128 
    129 
    130     def test_driver_interrupt_sync(self):
    131         drv = MockDriver(self.store, interrupt_block=1, target=2)
    132         generator = MockBlockGenerator()
    133         generator.generate([3, 1, 2], driver=drv)
    134 
    135         fltr_one = MockFilter('foo')
    136         self.store.register(fltr_one)
    137 
    138         drv.run(self.conn, interval=0.1)
    139 
    140         self.assertEqual(len(fltr_one.contents), 3)
    141 
    142         store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
    143         store.register(fltr_one)
    144         drv = MockDriver(store)
    145         generator.apply(drv, offset=1)
    146 
    147         with self.assertRaises(SyncDone) as e:
    148             drv.run(self.conn, interval=0.1)
    149             self.assertEqual(e, 2)
    150 
    151         self.assertEqual(len(fltr_one.contents), 6)
    152 
    153 
    154     def test_driver_open_interrupt_sync_multifilter(self):
    155         drv = MockDriver(self.store, interrupt_block=2, target=-1)
    156         generator = MockBlockGenerator()
    157         generator.generate([3, 1, 2], driver=drv)
    158 
    159         fltr_one = MockFilter('foo_bar')
    160         self.store.register(fltr_one)
    161 
    162         fltr_two = MockFilter('bar_baz')
    163         self.store.register(fltr_two)
    164 
    165         drv.run(self.conn, interval=0.1)
    166 
    167         logg.info('resume')
    168         store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
    169         store.register(fltr_one)
    170         store.register(fltr_two)
    171         
    172         drv = MockDriver(store, interrupt_block=4, target=-1)
    173         generator = MockBlockGenerator(offset=2)
    174         generator.generate([3, 1, 2, 5, 3], driver=drv)
    175 
    176         drv.run(self.conn, interval=0.1)
    177 
    178 
    179     def test_driver_resume_nofilter(self):
    180         #drv = MockDriver(self.store, interrupt_block=7, target=10)
    181         drv = MockDriver(self.store, target=2)
    182         generator = MockBlockGenerator()
    183         generator.generate([3, 1, 1], driver=drv)
    184         with self.assertRaises(SyncDone):
    185             drv.run(self.conn, interval=0.1)
    186 
    187         drv = MockDriver(self.store, target=4)
    188         generator = MockBlockGenerator(offset=3)
    189         generator.generate([3, 1, 1], driver=drv)
    190         drv.run(self.conn, interval=0.1)
    191 
    192 
    193     def test_driver_coldresume_interrupt(self):
    194         drv = MockDriver(self.store, interrupt_block=2, interrupt_global=True, target=-1)
    195         generator = MockBlockGenerator()
    196         generator.generate([3, 1, 2, 4], driver=drv)
    197 
    198         fltr_one = MockFilter('foo', brk=10) # will break on all "foo" filter invocations
    199         self.store.register(fltr_one)
    200         fltr_two = MockFilter('bar')
    201         self.store.register(fltr_two)
    202 
    203         drv.run(self.conn, interval=0.1)
    204 
    205         logg.info('resume')
    206         SyncDriver.running_global = True
    207         store = SyncFsStore(self.path, state_event_callback=state_event_handler, filter_state_event_callback=filter_state_event_handler)
    208         store.register(fltr_one)
    209         store.register(fltr_two)
    210         drv = MockDriver(store, interrupt_block=4, target=-1)
    211         generator = MockBlockGenerator(offset=2)
    212         generator.generate([4, 1, 2], driver=drv)
    213         drv.run(self.conn, interval=0.1)
    214 
    215 
    216 if __name__ == '__main__':
    217     unittest.main()