Implement embedded tx hash generation, reusable test provisions module
This commit is contained in:
parent
8e2cb86266
commit
4db22bcc08
@ -1 +0,0 @@
|
|||||||
#from .setup import Environment
|
|
@ -4,7 +4,7 @@ from chainqueue import Store as QueueStore
|
|||||||
|
|
||||||
class ChaindAdapter:
|
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):
|
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.deserialize = deserializer
|
self.cache_adapter = cache_adapter
|
||||||
self.dispatcher = dispatcher
|
self.dispatcher = dispatcher
|
||||||
self.store = QueueStore(chain_spec, state_store, index_store, counter_store, cache=cache)
|
self.store = QueueStore(chain_spec, state_store, index_store, counter_store, cache=cache)
|
||||||
|
@ -19,18 +19,18 @@ logg = logging.getLogger(__name__)
|
|||||||
|
|
||||||
class ChaindFsAdapter(ChaindAdapter):
|
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
|
factory = SimpleFileStoreFactory(path).add
|
||||||
state_store = Status(factory)
|
state_store = Status(factory)
|
||||||
index_store = IndexStore(path, digest_bytes=digest_bytes)
|
index_store = IndexStore(path, digest_bytes=digest_bytes)
|
||||||
counter_store = CounterStore(path)
|
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):
|
def put(self, signed_tx):
|
||||||
cache_tx = self.deserialize(signed_tx)
|
#cache_tx = self.deserialize(signed_tx)
|
||||||
self.store.put(cache_tx.hash, signed_tx)
|
(s, tx_hash,) = self.store.put(signed_tx, cache_adapter=self.cache_adapter)
|
||||||
return cache_tx.hash
|
return tx_hash
|
||||||
|
|
||||||
|
|
||||||
def get(self, tx_hash):
|
def get(self, tx_hash):
|
||||||
|
55
chaind/unittest/common.py
Normal file
55
chaind/unittest/common.py
Normal 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)
|
||||||
|
|
@ -1,69 +1,34 @@
|
|||||||
# standard imports
|
# standard imports
|
||||||
import os
|
import os
|
||||||
import tempfile
|
|
||||||
import unittest
|
import unittest
|
||||||
import shutil
|
import shutil
|
||||||
import logging
|
import logging
|
||||||
import hashlib
|
|
||||||
|
|
||||||
# external imports
|
# external imports
|
||||||
from chainlib.chain import ChainSpec
|
|
||||||
from chainqueue.cache import CacheTokenTx
|
|
||||||
from chainlib.error import RPCException
|
|
||||||
from chainlib.status import Status as TxStatus
|
from chainlib.status import Status as TxStatus
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from chaind.adapters.fs import ChaindFsAdapter
|
|
||||||
from chaind.driver import QueueDriver
|
from chaind.driver import QueueDriver
|
||||||
from chaind.filter import StateFilter
|
from chaind.filter import StateFilter
|
||||||
|
|
||||||
|
# test imports
|
||||||
|
from chaind.unittest.common import (
|
||||||
|
MockTx,
|
||||||
|
MockCacheAdapter,
|
||||||
|
TestChaindFsBase,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.DEBUG)
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
logg = logging.getLogger()
|
logg = logging.getLogger()
|
||||||
|
|
||||||
|
|
||||||
class MockCacheAdapter(CacheTokenTx):
|
|
||||||
|
|
||||||
def deserialize(self, v):
|
class TestChaindFs(TestChaindFsBase):
|
||||||
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):
|
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
|
self.cache_adapter = MockCacheAdapter
|
||||||
self.path = tempfile.mkdtemp()
|
super(TestChaindFs, self).setUp()
|
||||||
self.dispatcher = MockDispatcher()
|
|
||||||
deserializer = MockCacheAdapter().deserialize
|
|
||||||
self.adapter = ChaindFsAdapter(self.chain_spec, self.path, deserializer, self.dispatcher)
|
|
||||||
|
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user