chainqueue/tests/test_entry.py

86 lines
2.3 KiB
Python
Raw Permalink Normal View History

2022-03-11 20:38:12 +01:00
# standard imports
import os
import logging
import unittest
# external imports
from hexathon import add_0x
2022-04-10 16:00:01 +02:00
from chainlib.tx import Tx
from chainlib.block import Block
2022-03-11 20:38:12 +01:00
# local imports
from chainqueue import QueueEntry
# test imports
from tests.base_shep import TestShepBase
2022-03-15 09:06:39 +01:00
from tests.common import MockCacheTokenTx
2022-03-11 20:38:12 +01:00
logging.basicConfig(level=logging.DEBUG)
logg = logging.getLogger()
2022-03-12 09:48:19 +01:00
class TestEntry(TestShepBase):
2022-03-11 20:38:12 +01:00
def test_entry_get(self):
signed_tx = add_0x(os.urandom(128).hex())
nonce = 42
2022-03-15 09:06:39 +01:00
entry = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
tx_hash_one = entry.create(signed_tx)
2022-03-11 20:38:12 +01:00
signed_tx = add_0x(os.urandom(128).hex())
nonce = 42
2022-03-15 09:06:39 +01:00
entry = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
tx_hash_two = entry.create(signed_tx)
2022-03-11 20:38:12 +01:00
2022-03-12 14:48:40 +01:00
txs = self.store.by_state()
2022-03-11 20:38:12 +01:00
self.assertEqual(len(txs), 2)
2022-03-15 09:06:39 +01:00
logg.debug('tx hash one {}'.format(tx_hash_one))
entry = QueueEntry(self.store, tx_hash=tx_hash_one, cache_adapter=MockCacheTokenTx)
2022-03-11 20:38:12 +01:00
entry.load()
entry.sent()
2022-03-12 14:48:40 +01:00
txs = self.store.by_state()
2022-03-11 20:38:12 +01:00
self.assertEqual(len(txs), 1)
2022-03-12 14:48:40 +01:00
txs = self.store.by_state(state=self.store.IN_NETWORK)
2022-03-11 20:38:12 +01:00
self.assertEqual(len(txs), 1)
2022-03-14 22:17:00 +01:00
entry.succeed(None, None)
2022-03-12 14:48:40 +01:00
txs = self.store.by_state()
2022-03-11 20:38:12 +01:00
self.assertEqual(len(txs), 1)
entry = QueueEntry(self.store, tx_hash_two)
entry.load()
entry.sent()
2022-03-12 14:48:40 +01:00
txs = self.store.by_state(state=self.store.IN_NETWORK)
2022-03-11 20:38:12 +01:00
self.assertEqual(len(txs), 2)
2022-03-12 14:48:40 +01:00
txs = self.store.by_state(state=self.store.IN_NETWORK, strict=True)
2022-03-11 20:43:00 +01:00
self.assertEqual(len(txs), 1)
2022-03-11 20:38:12 +01:00
2022-04-10 16:00:01 +02:00
def test_entry_change(self):
signed_tx = add_0x(os.urandom(128).hex())
nonce = 42
entry = QueueEntry(self.store, cache_adapter=MockCacheTokenTx)
tx_hash = entry.create(signed_tx)
block = Block()
block.number = 13
tx = Tx(None)
tx.index = 666
entry.readysend()
entry.reserve()
entry.sendfail()
entry = QueueEntry(self.store, tx_hash, cache_adapter=MockCacheTokenTx)
entry.load()
self.assertEqual(str(entry), tx_hash + ': SENDFAIL')
2022-03-11 20:38:12 +01:00
if __name__ == '__main__':
unittest.main()