2022-03-12 14:48:40 +01:00
|
|
|
# standard imports
|
2022-03-13 17:04:14 +01:00
|
|
|
import os
|
2022-03-12 14:48:40 +01:00
|
|
|
import tempfile
|
|
|
|
import unittest
|
2022-03-12 15:12:02 +01:00
|
|
|
import logging
|
2022-03-13 18:22:39 +01:00
|
|
|
import time
|
2022-03-12 14:48:40 +01:00
|
|
|
|
|
|
|
# external imports
|
|
|
|
from shep.store.file import SimpleFileStoreFactory
|
|
|
|
from chainlib.chain import ChainSpec
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from chainqueue import (
|
|
|
|
Store,
|
|
|
|
Status,
|
|
|
|
)
|
|
|
|
|
|
|
|
# test imports
|
|
|
|
from tests.common import (
|
|
|
|
MockCounter,
|
2022-03-12 15:12:02 +01:00
|
|
|
MockTokenCache,
|
|
|
|
MockCacheTokenTx,
|
2022-03-13 15:58:26 +01:00
|
|
|
MockContentStore,
|
2022-03-12 14:48:40 +01:00
|
|
|
)
|
2022-03-12 15:12:02 +01:00
|
|
|
from tests.base_shep import TestShepBase
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
2022-03-12 14:48:40 +01:00
|
|
|
|
|
|
|
|
2022-03-12 15:12:02 +01:00
|
|
|
class TestIntegrateBase(TestShepBase):
|
2022-03-12 14:48:40 +01:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.path = tempfile.mkdtemp()
|
|
|
|
factory = SimpleFileStoreFactory(self.path).add
|
|
|
|
self.state = Status(factory)
|
|
|
|
content_store = MockContentStore()
|
|
|
|
counter = MockCounter()
|
|
|
|
chain_spec = ChainSpec('foo', 'bar', 42, 'baz')
|
|
|
|
self.cache = MockTokenCache()
|
|
|
|
self.store = Store(chain_spec, self.state, content_store, counter, cache=self.cache)
|
|
|
|
|
|
|
|
|
2022-03-12 15:12:02 +01:00
|
|
|
def test_integration_valid(self):
|
2022-03-13 17:04:14 +01:00
|
|
|
self.store.put(os.urandom(4).hex(), os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
2022-03-12 14:48:40 +01:00
|
|
|
|
2022-03-13 17:04:14 +01:00
|
|
|
|
|
|
|
def test_state_default(self):
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
v = self.store.pending()
|
|
|
|
self.assertEqual(len(v), 1)
|
|
|
|
self.assertEqual(v[0], hx)
|
2022-03-12 14:48:40 +01:00
|
|
|
|
2022-03-13 17:04:14 +01:00
|
|
|
|
|
|
|
def test_state_enqueue(self):
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
2022-03-13 16:40:45 +01:00
|
|
|
self.store.get(hx)
|
|
|
|
self.store.enqueue(hx)
|
|
|
|
v = self.store.upcoming()
|
|
|
|
self.assertEqual(len(v), 1)
|
2022-03-13 17:04:14 +01:00
|
|
|
v = self.store.pending()
|
|
|
|
self.assertEqual(len(v), 0)
|
2022-03-13 16:40:45 +01:00
|
|
|
|
|
|
|
|
2022-03-13 16:45:48 +01:00
|
|
|
def test_state_defer(self):
|
2022-03-13 17:04:14 +01:00
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
self.store.fail(hx)
|
|
|
|
v = self.store.deferred()
|
|
|
|
self.assertEqual(len(v), 1)
|
|
|
|
self.assertEqual(v[0], hx)
|
|
|
|
|
|
|
|
|
|
|
|
def test_state_multiple(self):
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
self.store.fail(hx)
|
|
|
|
hx = os.urandom(8).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
self.store.fail(hx)
|
|
|
|
v = self.store.deferred()
|
|
|
|
self.assertEqual(len(v), 2)
|
|
|
|
|
|
|
|
|
|
|
|
def test_state_multiple_sort(self):
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
self.store.fail(hx)
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
self.store.enqueue(hx)
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
2022-03-13 16:45:48 +01:00
|
|
|
self.store.fail(hx)
|
2022-03-13 17:04:14 +01:00
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
v = self.store.deferred()
|
|
|
|
self.assertEqual(len(v), 2)
|
2022-03-13 16:45:48 +01:00
|
|
|
|
|
|
|
|
2022-03-13 18:22:39 +01:00
|
|
|
def test_state_date_threshold(self):
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
s = self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
self.store.fail(hx)
|
|
|
|
then = self.store.modified(s)
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
hx = os.urandom(4).hex()
|
|
|
|
s = self.store.put(hx, os.urandom(8).hex(), cache_adapter=MockCacheTokenTx)
|
|
|
|
self.store.fail(hx)
|
|
|
|
|
|
|
|
v = self.store.deferred(threshold=then)
|
|
|
|
self.assertEqual(len(v), 1)
|
|
|
|
|
|
|
|
|
2022-03-12 14:48:40 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|