chainsyncer/tests/test_interrupt.py

139 lines
3.2 KiB
Python
Raw Normal View History

2021-04-15 09:59:45 +02:00
# standard imports
import logging
import unittest
import os
import tempfile
2021-04-15 09:59:45 +02:00
# external imports
from chainlib.chain import ChainSpec
# local imports
from chainsyncer.backend.memory import MemBackend
from chainsyncer.backend.sql import SyncerBackend
from chainsyncer.backend.file import (
2021-04-15 17:16:31 +02:00
FileBackend,
data_dir_for,
)
2021-04-15 09:59:45 +02:00
# test imports
from tests.base import TestBase
2021-04-15 11:15:48 +02:00
from chainsyncer.unittest.base import (
MockBlock,
MockConn,
2021-04-15 11:15:48 +02:00
TestSyncer,
)
2021-04-15 09:59:45 +02:00
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class NaughtyCountExceptionFilter:
def __init__(self, name, croak_on):
self.c = 0
self.croak = croak_on
self.name = name
def filter(self, conn, block, tx, db_session=None):
if self.c == self.croak:
2021-04-15 14:11:06 +02:00
self.croak = -1
2021-04-15 09:59:45 +02:00
raise RuntimeError('foo')
2021-04-15 14:11:06 +02:00
self.c += 1
2021-04-15 09:59:45 +02:00
def __str__(self):
return '{} {}'.format(self.__class__.__name__, self.name)
class CountFilter:
def __init__(self, name):
self.c = 0
self.name = name
def filter(self, conn, block, tx, db_session=None):
self.c += 1
def __str__(self):
return '{} {}'.format(self.__class__.__name__, self.name)
class TestInterrupt(TestBase):
2021-04-15 09:59:45 +02:00
def setUp(self):
super(TestInterrupt, self).setUp()
self.backend = None
self.conn = MockConn()
self.vectors = [
[4, 3, 2],
[6, 4, 2],
[6, 5, 2],
[6, 4, 3],
]
def assert_filter_interrupt(self, vector):
logg.debug('running vector {} {}'.format(str(self.backend), vector))
z = 0
for v in vector:
z += v
syncer = TestSyncer(self.backend, vector)
filters = [
2021-04-15 09:59:45 +02:00
CountFilter('foo'),
CountFilter('bar'),
NaughtyCountExceptionFilter('xyzzy', croak_on=3),
2021-04-15 09:59:45 +02:00
CountFilter('baz'),
]
2021-04-15 09:59:45 +02:00
for fltr in filters:
syncer.add_filter(fltr)
2021-04-15 09:59:45 +02:00
2021-04-15 11:15:48 +02:00
try:
syncer.loop(0.1, self.conn)
2021-04-15 11:15:48 +02:00
except RuntimeError:
2021-04-15 14:11:06 +02:00
logg.info('caught croak')
2021-04-15 11:15:48 +02:00
pass
(pair, fltr) = self.backend.get()
self.assertGreater(fltr, 0)
syncer.loop(0.1, self.conn)
2021-04-15 09:59:45 +02:00
for fltr in filters:
2021-04-15 11:15:48 +02:00
logg.debug('{} {}'.format(str(fltr), fltr.c))
self.assertEqual(fltr.c, z)
def test_filter_interrupt_memory(self):
for vector in self.vectors:
self.backend = MemBackend(self.chain_spec, None, target_block=len(vector))
self.assert_filter_interrupt(vector)
2021-04-15 17:16:31 +02:00
def test_filter_interrupt_file(self):
#for vector in self.vectors:
vector = self.vectors.pop()
d = tempfile.mkdtemp()
#os.makedirs(data_dir_for(self.chain_spec, 'foo', d))
2021-04-15 17:16:31 +02:00
self.backend = FileBackend.initial(self.chain_spec, len(vector), base_dir=d) #'foo', base_dir=d)
self.assert_filter_interrupt(vector)
def test_filter_interrupt_sql(self):
for vector in self.vectors:
self.backend = SyncerBackend.initial(self.chain_spec, len(vector))
self.assert_filter_interrupt(vector)
2021-04-15 11:15:48 +02:00
2021-04-15 09:59:45 +02:00
if __name__ == '__main__':
unittest.main()