Use mock conn and process method from headsyncer in testsyncer

This commit is contained in:
nolash 2021-04-15 15:27:19 +02:00
parent 987a18fd6b
commit 6201420ad2
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 22 additions and 17 deletions

View File

@ -92,6 +92,9 @@ class BlockPollSyncer(Syncer):
if self.pre_callback != None: if self.pre_callback != None:
self.pre_callback() self.pre_callback()
while True and Syncer.running_global: while True and Syncer.running_global:
if start_tx > 0:
start_tx -= 1
continue
try: try:
block = self.get(conn) block = self.get(conn)
except SyncDone as e: except SyncDone as e:
@ -108,8 +111,6 @@ class BlockPollSyncer(Syncer):
self.block_callback(block, None) self.block_callback(block, None)
last_block = block last_block = block
if start_tx > 0:
block.txs = block.txs[start_tx:]
self.process(conn, block) self.process(conn, block)
start_tx = 0 start_tx = 0
time.sleep(self.yield_delay) time.sleep(self.yield_delay)
@ -121,8 +122,8 @@ class BlockPollSyncer(Syncer):
class HeadSyncer(BlockPollSyncer): class HeadSyncer(BlockPollSyncer):
def process(self, conn, block): def process(self, conn, block):
logg.debug('process block {}'.format(block))
(pair, fltr) = self.backend.get() (pair, fltr) = self.backend.get()
logg.debug('process block {} (backend {}:{})'.format(block, pair, fltr))
i = pair[1] # set tx index from previous i = pair[1] # set tx index from previous
tx = None tx = None
while True: while True:
@ -137,6 +138,7 @@ class HeadSyncer(BlockPollSyncer):
tx.apply_receipt(rcpt) tx.apply_receipt(rcpt)
self.process_single(conn, block, tx) self.process_single(conn, block, tx)
self.backend.reset_filter()
i += 1 i += 1

View File

@ -12,6 +12,13 @@ from chainsyncer.error import NoBlockForYou
logg = logging.getLogger().getChild(__name__) logg = logging.getLogger().getChild(__name__)
class MockConn:
def do(self, o):
pass
class MockTx: class MockTx:
def __init__(self, index, tx_hash): def __init__(self, index, tx_hash):
@ -19,6 +26,10 @@ class MockTx:
self.index = index self.index = index
def apply_receipt(self, rcpt):
self.rcpt = rcpt
class MockBlock: class MockBlock:
def __init__(self, number, txs): def __init__(self, number, txs):
@ -54,13 +65,3 @@ class TestSyncer(HistorySyncer):
block_txs.append(add_0x(os.urandom(32).hex())) block_txs.append(add_0x(os.urandom(32).hex()))
return MockBlock(block_height, block_txs) return MockBlock(block_height, block_txs)
# TODO: implement mock conn instead, and use HeadSyncer.process
def process(self, conn, block):
i = 0
for tx in block.txs:
self.process_single(conn, block, block.tx(i))
self.backend.reset_filter()
i += 1
self.backend.set(block.number + 1, 0)

View File

@ -14,6 +14,7 @@ from chainsyncer.backend.sql import SyncerBackend
from tests.base import TestBase from tests.base import TestBase
from chainsyncer.unittest.base import ( from chainsyncer.unittest.base import (
MockBlock, MockBlock,
MockConn,
TestSyncer, TestSyncer,
) )
@ -63,27 +64,28 @@ class TestInterrupt(TestBase):
self.filters = [ self.filters = [
CountFilter('foo'), CountFilter('foo'),
CountFilter('bar'), CountFilter('bar'),
NaughtyCountExceptionFilter('xyzzy', 3), NaughtyCountExceptionFilter('xyzzy', croak_on=3),
CountFilter('baz'), CountFilter('baz'),
] ]
self.backend = None self.backend = None
self.conn = MockConn()
def assert_filter_interrupt(self): def assert_filter_interrupt(self):
syncer = TestSyncer(self.backend, [4, 2, 3]) syncer = TestSyncer(self.backend, [4, 3, 2])
for fltr in self.filters: for fltr in self.filters:
syncer.add_filter(fltr) syncer.add_filter(fltr)
try: try:
syncer.loop(0.1, None) syncer.loop(0.1, self.conn)
except RuntimeError: except RuntimeError:
logg.info('caught croak') logg.info('caught croak')
pass pass
(pair, fltr) = self.backend.get() (pair, fltr) = self.backend.get()
self.assertGreater(fltr, 0) self.assertGreater(fltr, 0)
syncer.loop(0.1, None) syncer.loop(0.1, self.conn)
for fltr in self.filters: for fltr in self.filters:
logg.debug('{} {}'.format(str(fltr), fltr.c)) logg.debug('{} {}'.format(str(fltr), fltr.c))