def test_cache_invalidate(self): """ Test the cache invalidation. """ state = {'count': 0} def read(_): """ Read dummy. """ if state['count'] == 0: state['count'] = 1 return {'data': bytearray([255] * 256)} elif state['count'] == 1: state['count'] = 2 return {'data': bytearray([255] * 256)} else: raise Exception('Too many reads !') SetUpTestInjections(master_communicator=MasterCommunicator(read)) eeprom_file = EepromFile() address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) # Second read should come from cache. address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) eeprom_file.invalidate_cache() # Should be read from communicator, since cache is invalid address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) self.assertEqual(2, state['count'])
def get_module_addresses(module_type): # type: (str) -> List[str] """ Get the addresses for the modules of the given type. :param module_type: the type of the module (O, R, D, I, T, C) :returns: A list containing the addresses of the modules (strings of length 4). """ eeprom_file = EepromFile() base_address = EepromAddress(0, 1, 2) no_modules = eeprom_file.read([base_address]) modules = [] no_input_modules = no_modules[base_address].bytes[0] for i in range(no_input_modules): address = EepromAddress(2 + i, 252, 1) is_can = chr(eeprom_file.read([address])[address].bytes[0]) == 'C' address = EepromAddress(2 + i, 0, 4) module = eeprom_file.read([address])[address].bytes if not is_can or chr(module[0]) == 'C': modules.append(module) no_output_modules = no_modules[base_address].bytes[1] for i in range(no_output_modules): address = EepromAddress(33 + i, 0, 4) modules.append(eeprom_file.read([address])[address].bytes) return [module for module in modules if chr(module[0]) == module_type]
def test_read_multiple_banks(self): """ Test read from multiple banks. """ def read(_data): """ Read dummy. """ if _data['bank'] == 1: return {'data': bytearray(b'abc') + bytearray([255] * 200) + bytearray(b'def') + bytearray([255] * 48)} if _data['bank'] == 100: return {'data': bytearray(b'hello') + bytearray([0] * 100) + bytearray(b'world') + bytearray([0] * 146)} else: raise Exception('Wrong page') SetUpTestInjections(master_communicator=MasterCommunicator(read)) eeprom_file = EepromFile() address1 = EepromAddress(1, 2, 10) address2 = EepromAddress(100, 4, 10) address3 = EepromAddress(100, 105, 5) data = eeprom_file.read([address1, address2, address3]) self.assertEqual(3, len(data)) self.assertEqual(address1, data[address1].address) self.assertEqual(bytearray(b'c') + bytearray([255] * 9), data[address1].bytes) self.assertEqual(address2, data[address2].address) self.assertEqual(bytearray(b'o') + bytearray([0] * 9), data[address2].bytes) self.assertEqual(address3, data[address3].address) self.assertEqual(bytearray(b'world'), data[address3].bytes)
def test_read_one_bank_two_addresses(self): """ Test read from one bank with two addresses. """ def read(_data): """ Read dummy """ if _data['bank'] == 1: return { 'data': bytearray(b'abc') + bytearray([255] * 200) + bytearray(b'def') + bytearray([255] * 48) } else: raise Exception('Wrong page') SetUpTestInjections(master_communicator=MasterCommunicator(read)) eeprom_file = EepromFile() address1 = EepromAddress(1, 2, 10) address2 = EepromAddress(1, 203, 4) data = eeprom_file.read([address1, address2]) self.assertEqual(2, len(data)) self.assertEqual(address1, data[address1].address) self.assertEqual( bytearray(b'c') + bytearray([255] * 9), data[address1].bytes) self.assertEqual(address2, data[address2].address) self.assertEqual(bytearray(b'def\xff'), data[address2].bytes)
def test_cache_write(self): """ Test the eeprom cache on writing. """ state = {'read': 0, 'write': 0} def read(_): """ Read dummy. """ if state['read'] == 0: state['read'] = 1 return {'data': bytearray([255] * 256)} else: raise Exception('Too many reads !') def write(data): """ Write dummy. """ if state['write'] == 0: self.assertEqual(1, data['bank']) self.assertEqual(100, data['address']) self.assertEqual(bytearray([0] * 10), data['data']) state['write'] = 1 elif state['write'] == 1: self.assertEqual(1, data['bank']) self.assertEqual(110, data['address']) self.assertEqual(bytearray([0] * 10), data['data']) state['write'] = 2 else: raise Exception('Too many writes !') SetUpTestInjections( master_communicator=MasterCommunicator(read, write)) eeprom_file = EepromFile() address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) eeprom_file.write( [EepromData(EepromAddress(1, 100, 20), bytearray([0] * 20))]) address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 100 + [0] * 20 + [255] * 136), read[address].bytes) self.assertEqual(1, state['read']) self.assertEqual(2, state['write'])
def test_cache_write_exception(self): """ The cache should be invalidated if a write fails. """ state = {'read': 0, 'write': 0} def read(_): """ Read dummy. """ if state['read'] == 0: state['read'] = 1 return {'data': bytearray([255] * 256)} elif state['read'] == 1: state['read'] = 2 return {'data': bytearray([255] * 256)} else: raise Exception('Too many reads !') def write(_): """ Write dummy. """ state['write'] += 1 raise Exception('write fails...') SetUpTestInjections( master_communicator=MasterCommunicator(read, write)) eeprom_file = EepromFile() address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) try: eeprom_file.write( [EepromData(EepromAddress(1, 100, 20), bytearray([0] * 20))]) self.fail('Should not get here !') except Exception: pass address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) self.assertEqual(2, state['read']) self.assertEqual(1, state['write'])
def test_cache(self): """ Test the caching of banks. """ state = {'count': 0} def read(_): """ Read dummy. """ if state['count'] == 0: state['count'] = 1 return {'data': bytearray([255] * 256)} else: raise Exception('Too many reads !') SetUpTestInjections(master_communicator=MasterCommunicator(read)) eeprom_file = EepromFile() address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes) # Second read should come from cache, if read is called # an exception will be thrown. address = EepromAddress(1, 0, 256) read = eeprom_file.read([address]) self.assertEqual(bytearray([255] * 256), read[address].bytes)
def test_read_one_bank_one_address(self): """ Test read from one bank with one address """ def read(_data): """ Read dummy. """ if _data['bank'] == 1: return {'data': bytearray(b'abc') + bytearray([255] * 200) + bytearray(b'def') + bytearray([255] * 48)} else: raise Exception('Wrong page') SetUpTestInjections(master_communicator=MasterCommunicator(read)) eeprom_file = EepromFile() address = EepromAddress(1, 0, 3) data = eeprom_file.read([address]) self.assertEqual(1, len(data)) self.assertEqual(address, data[address].address) self.assertEqual(bytearray(b'abc'), data[address].bytes)