Add cache handling
This commit is contained in:
@@ -12,7 +12,7 @@ from chainqueue import (
|
||||
)
|
||||
|
||||
|
||||
class MockIndexStore:
|
||||
class MockContentStore:
|
||||
|
||||
def __init__(self):
|
||||
self.store = {}
|
||||
@@ -26,10 +26,24 @@ class MockIndexStore:
|
||||
return self.store.get(k)
|
||||
|
||||
|
||||
class MockCounter:
|
||||
|
||||
def __init__(self):
|
||||
self.c = 0
|
||||
|
||||
|
||||
def next(self):
|
||||
c = self.c
|
||||
self.c += 1
|
||||
return c
|
||||
|
||||
|
||||
class TestShepBase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.path = tempfile.mkdtemp()
|
||||
factory = SimpleFileStoreFactory(self.path).add
|
||||
self.state = Status(factory)
|
||||
self.store = Store(self.state, MockIndexStore())
|
||||
content_store = MockContentStore()
|
||||
counter = MockCounter()
|
||||
self.store = Store(self.state, content_store, counter)
|
||||
|
||||
71
tests/test_cache.py
Normal file
71
tests/test_cache.py
Normal file
@@ -0,0 +1,71 @@
|
||||
# standard imports
|
||||
import os
|
||||
import logging
|
||||
import unittest
|
||||
import hashlib
|
||||
|
||||
# external imports
|
||||
from hexathon import add_0x
|
||||
|
||||
# local imports
|
||||
from chainqueue import QueueEntry
|
||||
from chainqueue.cache import (
|
||||
CacheTokenTx,
|
||||
)
|
||||
|
||||
# test imports
|
||||
from tests.base_shep import TestShepBase
|
||||
|
||||
logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class MockCacheTokenTx(CacheTokenTx):
|
||||
|
||||
def deserialize(self, signed_tx):
|
||||
h = hashlib.sha1()
|
||||
h.update(signed_tx + b'\x01')
|
||||
z = h.digest()
|
||||
nonce = int.from_bytes(z[:4], 'big')
|
||||
token_value = int.from_bytes(z[4:8], 'big')
|
||||
value = int.from_bytes(z[8:12], 'big')
|
||||
|
||||
h = hashlib.sha1()
|
||||
h.update(z)
|
||||
z = h.digest()
|
||||
sender = z.hex()
|
||||
|
||||
h = hashlib.sha1()
|
||||
h.update(z)
|
||||
z = h.digest()
|
||||
recipient = z.hex()
|
||||
|
||||
h = hashlib.sha1()
|
||||
h.update(z)
|
||||
z = h.digest()
|
||||
token = z.hex()
|
||||
|
||||
tx = CacheTokenTx()
|
||||
tx.init(nonce, sender, recipient, value)
|
||||
tx.set('src_token', token)
|
||||
tx.set('dst_token', token)
|
||||
tx.set('src_value', token_value)
|
||||
tx.set('dst_value', token_value)
|
||||
|
||||
return tx
|
||||
|
||||
|
||||
class TestCache(TestShepBase):
|
||||
|
||||
def setUp(self):
|
||||
super(TestCache, self).setUp()
|
||||
self.tx = MockCacheTokenTx()
|
||||
|
||||
def test_basic_translator(self):
|
||||
a = b'foo'
|
||||
tx = self.tx.deserialize(a)
|
||||
print(tx)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@@ -16,24 +16,20 @@ logging.basicConfig(level=logging.DEBUG)
|
||||
logg = logging.getLogger()
|
||||
|
||||
|
||||
class MockTranslator:
|
||||
pass
|
||||
|
||||
|
||||
class TestShep(TestShepBase):
|
||||
class TestEntry(TestShepBase):
|
||||
|
||||
def test_entry_get(self):
|
||||
tx_hash_one = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
nonce = 42
|
||||
entry = QueueEntry(self.store, tx_hash_one)
|
||||
entry.create(nonce, signed_tx)
|
||||
entry.create(signed_tx)
|
||||
|
||||
tx_hash_two = add_0x(os.urandom(32).hex())
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
nonce = 42
|
||||
entry = QueueEntry(self.store, tx_hash_two)
|
||||
entry.create(nonce, signed_tx)
|
||||
entry.create(signed_tx)
|
||||
|
||||
txs = self.store.list()
|
||||
self.assertEqual(len(txs), 2)
|
||||
|
||||
@@ -31,7 +31,7 @@ class TestShep(TestShepBase):
|
||||
signed_tx = add_0x(os.urandom(128).hex())
|
||||
nonce = 42
|
||||
tx = QueueEntry(self.store, tx_hash)
|
||||
tx.create(nonce, signed_tx)
|
||||
tx.create(signed_tx)
|
||||
|
||||
tx_retrieved = QueueEntry(self.store, tx_hash)
|
||||
tx_retrieved.load()
|
||||
|
||||
Reference in New Issue
Block a user