Implement embedded tx hash generation, reusable test provisions module

This commit is contained in:
lash 2022-03-15 08:28:10 +00:00
parent 8e2cb86266
commit 4db22bcc08
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 73 additions and 54 deletions

View File

@ -1 +0,0 @@
#from .setup import Environment

View File

@ -4,7 +4,7 @@ from chainqueue import Store as QueueStore
class ChaindAdapter:
def __init__(self, chain_spec, state_store, index_store, counter_store,deserializer, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0):
self.deserialize = deserializer
def __init__(self, chain_spec, state_store, index_store, counter_store, cache_adapter, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0):
self.cache_adapter = cache_adapter
self.dispatcher = dispatcher
self.store = QueueStore(chain_spec, state_store, index_store, counter_store, cache=cache)

View File

@ -19,18 +19,18 @@ logg = logging.getLogger(__name__)
class ChaindFsAdapter(ChaindAdapter):
def __init__(self, chain_spec, path, deserializer, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0, digest_bytes=32):
def __init__(self, chain_spec, path, cache_adapter, dispatcher, cache=None, pending_retry_threshold=0, error_retry_threshold=0, digest_bytes=32):
factory = SimpleFileStoreFactory(path).add
state_store = Status(factory)
index_store = IndexStore(path, digest_bytes=digest_bytes)
counter_store = CounterStore(path)
super(ChaindFsAdapter, self).__init__(chain_spec, state_store, index_store, counter_store, deserializer, dispatcher, cache=cache, pending_retry_threshold=pending_retry_threshold, error_retry_threshold=error_retry_threshold)
super(ChaindFsAdapter, self).__init__(chain_spec, state_store, index_store, counter_store, cache_adapter, dispatcher, cache=cache, pending_retry_threshold=pending_retry_threshold, error_retry_threshold=error_retry_threshold)
def put(self, signed_tx):
cache_tx = self.deserialize(signed_tx)
self.store.put(cache_tx.hash, signed_tx)
return cache_tx.hash
#cache_tx = self.deserialize(signed_tx)
(s, tx_hash,) = self.store.put(signed_tx, cache_adapter=self.cache_adapter)
return tx_hash
def get(self, tx_hash):

55
chaind/unittest/common.py Normal file
View File

@ -0,0 +1,55 @@
# standard imports
import unittest
import hashlib
import tempfile
# external imports
from chainqueue.cache import CacheTokenTx
from chainlib.status import Status as TxStatus
from chainlib.chain import ChainSpec
from chainlib.error import RPCException
# local imports
from chaind.adapters.fs import ChaindFsAdapter
class MockCacheAdapter(CacheTokenTx):
def deserialize(self, v):
h = hashlib.sha256()
h.update(v.encode('utf-8'))
z = h.digest()
self.hash = z.hex()
class MockDispatcher:
def __init__(self):
self.fails = []
def add_fail(self, v):
self.fails.append(v)
def send(self, v):
if v not in self.fails:
raise RPCException('{} is in fails'.format(v))
pass
class MockTx:
def __init__(self, tx_hash, status=TxStatus.SUCCESS):
self.hash = tx_hash
self.status = status
class TestChaindFsBase(unittest.TestCase):
def setUp(self):
self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
self.path = tempfile.mkdtemp()
self.dispatcher = MockDispatcher()
self.adapter = ChaindFsAdapter(self.chain_spec, self.path, self.cache_adapter, self.dispatcher)

View File

@ -1,69 +1,34 @@
# standard imports
import os
import tempfile
import unittest
import shutil
import logging
import hashlib
# external imports
from chainlib.chain import ChainSpec
from chainqueue.cache import CacheTokenTx
from chainlib.error import RPCException
from chainlib.status import Status as TxStatus
# local imports
from chaind.adapters.fs import ChaindFsAdapter
from chaind.driver import QueueDriver
from chaind.filter import StateFilter
# test imports
from chaind.unittest.common import (
MockTx,
MockCacheAdapter,
TestChaindFsBase,
)
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
class MockCacheAdapter(CacheTokenTx):
def deserialize(self, v):
tx = CacheTokenTx()
h = hashlib.sha256()
h.update(v.encode('utf-8'))
z = h.digest()
tx.hash = z.hex()
return tx
class MockDispatcher:
def __init__(self):
self.fails = []
def add_fail(self, v):
self.fails.append(v)
def send(self, v):
if v not in self.fails:
raise RPCException('{} is in fails'.format(v))
pass
class MockTx:
def __init__(self, tx_hash, status=TxStatus.SUCCESS):
self.hash = tx_hash
self.status = status
class TestChaindFs(unittest.TestCase):
class TestChaindFs(TestChaindFsBase):
def setUp(self):
self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
self.path = tempfile.mkdtemp()
self.dispatcher = MockDispatcher()
deserializer = MockCacheAdapter().deserialize
self.adapter = ChaindFsAdapter(self.chain_spec, self.path, deserializer, self.dispatcher)
self.cache_adapter = MockCacheAdapter
super(TestChaindFs, self).setUp()
def tearDown(self):