2021-04-15 09:59:45 +02:00
|
|
|
|
# standard imports
|
|
|
|
|
import logging
|
|
|
|
|
import unittest
|
|
|
|
|
import os
|
2021-04-15 16:23:30 +02:00
|
|
|
|
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
|
2021-04-15 17:45:35 +02:00
|
|
|
|
from chainsyncer.backend.sql import SQLBackend
|
2021-04-15 16:23:30 +02:00
|
|
|
|
from chainsyncer.backend.file import (
|
2021-04-15 17:16:31 +02:00
|
|
|
|
FileBackend,
|
2021-04-15 16:23:30 +02:00
|
|
|
|
data_dir_for,
|
|
|
|
|
)
|
2021-09-26 19:32:08 +02:00
|
|
|
|
from chainsyncer.error import LockError
|
2021-04-15 09:59:45 +02:00
|
|
|
|
|
|
|
|
|
# test imports
|
2021-08-26 10:09:47 +02:00
|
|
|
|
from tests.chainsyncer_base import TestBase
|
2021-04-15 11:15:48 +02:00
|
|
|
|
from chainsyncer.unittest.base import (
|
|
|
|
|
MockBlock,
|
2021-04-15 15:27:19 +02:00
|
|
|
|
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):
|
2021-09-26 19:32:08 +02:00
|
|
|
|
self.c += 1
|
2021-04-15 09:59:45 +02:00
|
|
|
|
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')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-04-15 15:06:07 +02:00
|
|
|
|
class TestInterrupt(TestBase):
|
2021-04-15 09:59:45 +02:00
|
|
|
|
|
2021-04-15 15:06:07 +02:00
|
|
|
|
def setUp(self):
|
|
|
|
|
super(TestInterrupt, self).setUp()
|
2021-04-15 16:23:30 +02:00
|
|
|
|
|
|
|
|
|
self.backend = None
|
|
|
|
|
self.conn = MockConn()
|
|
|
|
|
self.vectors = [
|
|
|
|
|
[4, 3, 2],
|
|
|
|
|
[6, 4, 2],
|
|
|
|
|
[6, 5, 2],
|
|
|
|
|
[6, 4, 3],
|
|
|
|
|
]
|
2021-09-26 19:32:08 +02:00
|
|
|
|
self.track_complete = True
|
2021-04-15 16:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
2021-08-26 10:09:47 +02:00
|
|
|
|
def assert_filter_interrupt(self, vector, chain_interface):
|
2021-04-15 16:23:30 +02:00
|
|
|
|
|
|
|
|
|
logg.debug('running vector {} {}'.format(str(self.backend), vector))
|
|
|
|
|
|
|
|
|
|
z = 0
|
|
|
|
|
for v in vector:
|
|
|
|
|
z += v
|
|
|
|
|
|
2021-08-26 10:09:47 +02:00
|
|
|
|
syncer = TestSyncer(self.backend, chain_interface, vector)
|
2021-04-15 16:23:30 +02:00
|
|
|
|
|
|
|
|
|
filters = [
|
2021-04-15 09:59:45 +02:00
|
|
|
|
CountFilter('foo'),
|
|
|
|
|
CountFilter('bar'),
|
2021-04-15 15:27:19 +02:00
|
|
|
|
NaughtyCountExceptionFilter('xyzzy', croak_on=3),
|
2021-04-15 09:59:45 +02:00
|
|
|
|
CountFilter('baz'),
|
2021-04-15 15:06:07 +02:00
|
|
|
|
]
|
2021-04-15 09:59:45 +02:00
|
|
|
|
|
2021-04-15 16:23:30 +02:00
|
|
|
|
for fltr in filters:
|
2021-04-15 15:06:07 +02:00
|
|
|
|
syncer.add_filter(fltr)
|
2021-04-15 09:59:45 +02:00
|
|
|
|
|
2021-04-15 11:15:48 +02:00
|
|
|
|
try:
|
2021-04-15 15:27:19 +02:00
|
|
|
|
syncer.loop(0.1, self.conn)
|
2021-04-15 11:15:48 +02:00
|
|
|
|
except RuntimeError:
|
2021-09-26 19:32:08 +02:00
|
|
|
|
self.croaked = 2
|
2021-04-15 14:11:06 +02:00
|
|
|
|
logg.info('caught croak')
|
2021-04-15 11:15:48 +02:00
|
|
|
|
pass
|
2021-04-15 15:06:07 +02:00
|
|
|
|
(pair, fltr) = self.backend.get()
|
|
|
|
|
self.assertGreater(fltr, 0)
|
2021-09-26 19:32:08 +02:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
syncer.loop(0.1, self.conn)
|
|
|
|
|
except LockError:
|
|
|
|
|
self.backend.complete_filter(2)
|
|
|
|
|
syncer.loop(0.1, self.conn)
|
2021-04-15 09:59:45 +02:00
|
|
|
|
|
2021-04-15 16:23:30 +02:00
|
|
|
|
for fltr in filters:
|
2021-04-15 11:15:48 +02:00
|
|
|
|
logg.debug('{} {}'.format(str(fltr), fltr.c))
|
2021-04-15 16:23:30 +02:00
|
|
|
|
self.assertEqual(fltr.c, z)
|
2021-04-15 15:06:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_filter_interrupt_memory(self):
|
2021-09-26 19:32:08 +02:00
|
|
|
|
self.track_complete = True
|
2021-04-15 16:23:30 +02:00
|
|
|
|
for vector in self.vectors:
|
|
|
|
|
self.backend = MemBackend(self.chain_spec, None, target_block=len(vector))
|
2021-08-26 10:09:47 +02:00
|
|
|
|
self.assert_filter_interrupt(vector, self.interface)
|
2021-04-15 15:06:07 +02:00
|
|
|
|
|
2021-09-26 19:32:08 +02:00
|
|
|
|
#TODO: implement flag lock in file backend
|
|
|
|
|
@unittest.expectedFailure
|
2021-04-15 17:16:31 +02:00
|
|
|
|
def test_filter_interrupt_file(self):
|
|
|
|
|
#for vector in self.vectors:
|
|
|
|
|
vector = self.vectors.pop()
|
2021-04-15 16:23:30 +02:00
|
|
|
|
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)
|
2021-08-26 10:09:47 +02:00
|
|
|
|
self.assert_filter_interrupt(vector, self.interface)
|
2021-04-15 16:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
2021-04-15 15:06:07 +02:00
|
|
|
|
def test_filter_interrupt_sql(self):
|
2021-09-26 19:32:08 +02:00
|
|
|
|
self.track_complete = True
|
2021-04-15 16:23:30 +02:00
|
|
|
|
for vector in self.vectors:
|
2021-04-15 17:45:35 +02:00
|
|
|
|
self.backend = SQLBackend.initial(self.chain_spec, len(vector))
|
2021-08-26 10:09:47 +02:00
|
|
|
|
self.assert_filter_interrupt(vector, self.interface)
|
2021-04-15 16:23:30 +02:00
|
|
|
|
|
|
|
|
|
|
2021-04-15 09:59:45 +02:00
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|