commit 55e30eb13b6f2e7dae063b2f24c1c6cbd5e80ffb
parent 61d7ee49f337d093f02632c256af778cb9adeafe
Author: lash <dev@holbrook.no>
Date: Tue, 29 Mar 2022 07:28:28 +0000
Add mock block generator
Diffstat:
2 files changed, 37 insertions(+), 39 deletions(-)
diff --git a/chainsyncer/unittest/base.py b/chainsyncer/unittest/base.py
@@ -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
diff --git a/tests/test_session.py b/tests/test_session.py
@@ -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()