Add mock block generator

This commit is contained in:
lash 2022-03-29 07:28:28 +00:00
parent 61d7ee49f3
commit 55e30eb13b
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 37 additions and 39 deletions

View File

@ -28,6 +28,27 @@ class MockFilterError(Exception):
pass
class MockBlockGenerator:
def __init__(self, offset=0):
self.blocks = {}
self.offset = offset
self.cursor = offset
def generate(self, spec=[], driver=None):
for v in spec:
txs = []
for i in range(v):
tx_hash = os.urandom(32).hex()
tx = MockTx(0, tx_hash)
txs.append(tx)
block = MockBlock(self.cursor, txs)
driver.add_block(block)
self.cursor += 1
class MockConn:
"""Noop connection mocker.
@ -72,6 +93,7 @@ class MockBlock:
"""
self.number = number
self.txs = txs
self.hash = os.urandom(32).hex()
def tx(self, i):
@ -141,6 +163,7 @@ class MockDriver(SyncDriver):
def add_block(self, block):
logg.debug('add block {} {} with {} txs'.format(block.number, block.hash, len(block.txs)))
self.blocks[block.number] = block

View File

@ -16,6 +16,7 @@ from chainsyncer.error import (
SyncDone,
)
from chainsyncer.unittest import (
MockBlockGenerator,
MockFilter,
MockConn,
MockTx,
@ -63,18 +64,8 @@ class TestFilter(unittest.TestCase):
def test_driver(self):
drv = MockDriver(self.store, target=1)
tx_hash = os.urandom(32).hex()
tx = MockTx(0, tx_hash)
block = MockBlock(0, [tx_hash])
drv.add_block(block)
tx_hash_one = os.urandom(32).hex()
tx = MockTx(0, tx_hash_one)
tx_hash_two = os.urandom(32).hex()
tx = MockTx(1, tx_hash_two)
block = MockBlock(1, [tx_hash_one, tx_hash_two])
drv.add_block(block)
generator = MockBlockGenerator()
generator.generate([1, 2], driver=drv)
fltr_one = MockFilter('foo')
self.store.register(fltr_one)
@ -86,11 +77,8 @@ class TestFilter(unittest.TestCase):
def test_driver_interrupt_noresume(self):
drv = MockDriver(self.store, target=1)
tx_hash = os.urandom(32).hex()
tx = MockTx(0, tx_hash)
block = MockBlock(0, [tx_hash])
drv.add_block(block)
generator = MockBlockGenerator()
generator.generate([1], driver=drv)
fltr_one = MockFilter('foo', brk_hard=1)
self.store.register(fltr_one)
@ -110,30 +98,11 @@ class TestFilter(unittest.TestCase):
with self.assertRaises(LockError):
drv = MockDriver(store, target=1)
# drv.add_block(block)
#
# tx_hash_one = os.urandom(32).hex()
# tx = MockTx(0, tx_hash_one)
# tx_hash_two = os.urandom(32).hex()
# tx = MockTx(1, tx_hash_two)
# block = MockBlock(1, [tx_hash_one, tx_hash_two])
# drv.add_block(block)
# drv.run(self.conn)
def test_driver_interrupt_resume(self):
def test_driver_interrupt_filter(self):
drv = MockDriver(self.store, target=1)
tx_hash = os.urandom(32).hex()
tx = MockTx(0, tx_hash)
block = MockBlock(0, [tx_hash])
drv.add_block(block)
tx_hash = os.urandom(32).hex()
tx = MockTx(0, tx_hash)
block = MockBlock(1, [tx_hash])
drv.add_block(block)
generator = MockBlockGenerator()
generator.generate([1, 1], driver=drv)
fltr_one = MockFilter('foo', brk=1)
self.store.register(fltr_one)
@ -147,5 +116,11 @@ class TestFilter(unittest.TestCase):
drv.run(self.conn)
def test_driver_interrupt_sync(self):
generator = MockBlockGenerator()
drv = MockDriver(self.store, target=1)
generator.generate([1, 2], driver=drv)
if __name__ == '__main__':
unittest.main()