def testDeletePrevEntries(self): log = Log(10**8, 10**8, None) iteratorIDs = [0, 1] log.initIterators(iteratorIDs, []) func = print args = [1, 2] params_1 = [0, 1, 2, 3, 4, 5] shm_name, entry_size = log.createLogEntry(func, args, params_1) reference_1 = {} reference_1['func'], reference_1['args'] = func, args reference_1['logIndex'], reference_1['currentTerm'], reference_1[ 'prevLogIndex'] = params_1[0], params_1[1], params_1[2] reference_1['prevLogTerm'], reference_1['commitIndex'], reference_1[ 'leaderID'] = params_1[3], params_1[4], params_1[5] reference_1['shm_name'], reference_1['shm_size'], reference_1[ 'isCommitted'] = shm_name, entry_size, False params_1_extended = params_1 + [entry_size, False] log.addEntry(shm_name, params_1_extended) func = print args = [2, 3, 4] params_2 = [1, 2, 3, 4, 5, 6] shm_name, entry_size = log.createLogEntry(func, args, params_2) reference_2 = {} reference_2['func'], reference_2['args'] = func, args reference_2['logIndex'], reference_2['currentTerm'], reference_2[ 'prevLogIndex'] = params_2[0], params_2[1], params_2[2] reference_2['prevLogTerm'], reference_2['commitIndex'], reference_2[ 'leaderID'] = params_2[3], params_2[4], params_2[5] reference_2['shm_name'], reference_2['shm_size'], reference_2[ 'isCommitted'] = shm_name, entry_size, False params_2_extended = params_2 + [entry_size, False] log.addEntry(shm_name, params_2_extended) entry_0 = log.jump(0, 2, getNode=True) self.assertEqualEntry(log, entry_0, reference_2) log.deleteAllPrevEntries(entry_0) self.assertEqual(len(log), 3, "len(log) should be 3") entry_1 = log.jump(1, 2, getNode=True) log.deleteAllPrevEntries(entry_0) self.assertEqual(len(log), 1, "len(log) should be 1") self.assertEqual(log.nodeSizes[0], reference_2['shm_size'], 'shm_size does not match for reference') log.deleteAllEntries() self.assertEqual(len(log), 0, "len(log) should be 0")
def testAddEntry(self): log = Log(10**8, 10**8, None) iteratorIDs = [0, 1] log.initIterators(iteratorIDs, []) func = print args = [1, 2] # params = [0, 1, -1, 1, 0, 0] params = [0, 1, 2, 3, 4, 5] shm_name, entry_size = log.createLogEntry(func, args, params) reference = {} reference['func'], reference['args'] = func, args reference['logIndex'], reference['currentTerm'], reference[ 'prevLogIndex'] = params[0], params[1], params[2] reference['prevLogTerm'], reference['commitIndex'], reference[ 'leaderID'] = params[3], params[4], params[5] reference['shm_name'], reference['shm_size'], reference[ 'isCommitted'] = shm_name, entry_size, False params_extended = params + [entry_size, False] log.addEntry(shm_name, params_extended) self.assertEqual(len(log), 2, "len(log) should be 2") self.assertEqual(log.current(0), 0, "log.first should be 0 by default") next_entry = log.next(0, getNode=True) self.assertEqualEntry(log, next_entry, reference) self.assertEqualEntry(log, log.current(0, getNode=True), reference) self.assertEqual(log.ptrs[0][1], 1, 'index should be 1') self.assertEqual(len(log.nodeSizes), 2, 'log.nodeSizes should have 2 entries') self.assertEqual(log.nodeSizes[1], reference['shm_size'], 'shm_size does not match for reference') shm = multiprocessing.shared_memory.SharedMemory( name=next_entry.value[0]) self.assertEqualEntryBuffer(log, shm.buf, reference) utils.freeSharedMemory(shm)
def testDeleteEntry(self): log = Log(10**8, 10**8, None) iteratorIDs = [0, 1] log.initIterators(iteratorIDs, []) func = print args = [1, 2] params_1 = [0, 1, 2, 3, 4, 5] shm_name, entry_size = log.createLogEntry(func, args, params_1) reference_1 = {} reference_1['func'], reference_1['args'] = func, args reference_1['logIndex'], reference_1['currentTerm'], reference_1[ 'prevLogIndex'] = params_1[0], params_1[1], params_1[2] reference_1['prevLogTerm'], reference_1['commitIndex'], reference_1[ 'leaderID'] = params_1[3], params_1[4], params_1[5] reference_1['shm_name'], reference_1['shm_size'], reference_1[ 'isCommitted'] = shm_name, entry_size, False params_1_extended = params_1 + [entry_size, False] log.addEntry(shm_name, params_1_extended) func = print args = [2, 3, 4] params_2 = [1, 2, 3, 4, 5, 6] shm_name, entry_size = log.createLogEntry(func, args, params_2) reference_2 = {} reference_2['func'], reference_2['args'] = func, args reference_2['logIndex'], reference_2['currentTerm'], reference_2[ 'prevLogIndex'] = params_2[0], params_2[1], params_2[2] reference_2['prevLogTerm'], reference_2['commitIndex'], reference_2[ 'leaderID'] = params_2[3], params_2[4], params_2[5] reference_2['shm_name'], reference_2['shm_size'], reference_2[ 'isCommitted'] = shm_name, entry_size, False params_2_extended = params_2 + [entry_size, False] log.addEntry(shm_name, params_2_extended) self.assertEqual(len(log), 3, "len(log) should be 2") self.assertEqual(log.current(0), 0, "log.first should be 0 by default") next_entry = log.next(0, getNode=True) next_entry = log.next(0, getNode=True) self.assertEqualEntry(log, next_entry, reference_2) self.assertEqualEntry(log, log.current(0, getNode=True), reference_2) self.assertEqual(log.ptrs[0][1], 2, 'index should be 2') shm = multiprocessing.shared_memory.SharedMemory( name=next_entry.value[0]) self.assertEqualEntryBuffer(log, shm.buf, reference_2) utils.freeSharedMemory(shm, clear=False) prev_entry = log.prev(0, saveState=False, getNode=True) self.assertEqualEntry(log, prev_entry, reference_1) self.assertEqual(log.ptrs[0][1], 2, 'index should be 2') log.deleteEntry( prev_entry ) # At this stage prev_entry cannot be deleted because one of the pointers is at idx=0 self.assertEqual(len(log), 3, 'len(log) should be 3') next_entry = log.next(1, getNode=True) next_entry = log.next(1, getNode=True) log.deleteEntry( prev_entry ) # At this stage prev_entry can be deleted because both of the pointers is at idx=2 self.assertEqual(len(log), 2, 'len(log) should be 2') self.assertEqualEntry(log, log.current(0, getNode=True), reference_2) self.assertEqual(len(log.nodeSizes), 2, 'len(log.nodeSizes) should be 2') self.assertEqual(log.nodeSizes[1], reference_2['shm_size'], 'shm_size does not match for reference') log.deleteAllEntries() self.assertEqual(len(log), 0, 'log should be empty') self.assertEqual(sum(log.nodeSizes), 0, 'nodeSizes should be empty') self.assertEqual(log.logSize, 0, 'logSize should be 0')