github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_block_cache/tests.py (about) 1 # Copyright 2017 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 16 import logging 17 import unittest 18 import time 19 20 from sawtooth_validator.journal.block_cache import BlockCache 21 from sawtooth_validator.journal.block_wrapper import BlockWrapper 22 23 from sawtooth_validator.protobuf.block_pb2 import Block 24 from sawtooth_validator.protobuf.block_pb2 import BlockHeader 25 26 27 LOGGER = logging.getLogger(__name__) 28 29 30 class BlockCacheTest(unittest.TestCase): 31 def test_block_cache(self): 32 block_store = {} 33 cache = BlockCache(block_store=block_store, keep_time=1, 34 purge_frequency=1) 35 36 header1 = BlockHeader(previous_block_id="000") 37 block1 = BlockWrapper(Block(header=header1.SerializeToString(), 38 header_signature="ABC")) 39 40 header2 = BlockHeader(previous_block_id="ABC") 41 block2 = BlockWrapper(Block(header=header2.SerializeToString(), 42 header_signature="DEF")) 43 44 header3 = BlockHeader(previous_block_id="BCA") 45 block3 = BlockWrapper(Block(header=header3.SerializeToString(), 46 header_signature="FED")) 47 48 cache[block1.header_signature] = block1 49 cache[block2.header_signature] = block2 50 51 # Check that blocks are in the BlockCache 52 self.assertIn("ABC", cache) 53 self.assertIn("DEF", cache) 54 55 # Wait for purge time to expire 56 time.sleep(1) 57 # Add "FED" 58 cache[block3.header_signature] = block3 59 60 # Check that "ABC" is still in the cache even though the keep time has 61 # expired because it has a referecne count of 1 but "DEF" has been 62 # removed 63 self.assertIn("ABC", cache) 64 self.assertNotIn("DEF", cache) 65 self.assertIn("FED", cache)