def test_a_transaction_can_be_ended(self): """ Ensure that a simple transaction can be ended """ begin = Instruction("begin(T1)") self.trans_manager.execute(begin) self.assertTrue("T1" in self.trans_manager.transactions) read = Instruction("R(T1,x2)") self.trans_manager.execute(read) site_list = [] for site in self.trans_manager.sites_transactions_accessed_log["T1"]: site_list.append(site.identifer) # All sites should have been visited and obtained a read lock for site_id, site in self.trans_manager.sites.iteritems(): self.assertTrue(site_id in site_list) for lock in site.data_manager.locks["x2"]: self.assertTrue(lock.lock_type == LockType.READ) self.assertTrue(lock.transaction.identifier == "T1") transaction = self.trans_manager.transactions["T1"] with std_out() as (out, err): self.trans_manager.end_transaction(transaction) output = out.getvalue().strip() self.assertTrue(output == "T1 committed")
def test_first_time_read_instruction(self): """ Ensure a read lock is obtained and the correct value is read """ trans_identifer = "T1" variable_identifier = "x1" site_id = 2 begin = Instruction("begin(%s)" % trans_identifer) self.trans_manager.execute(begin) # Confirm transaction was created and added to list of Transactions self.assertTrue(trans_identifer in self.trans_manager.transactions) self.assertEquals(self.trans_manager.transactions[trans_identifer].transaction_type, \ TransactionType.READ_WRITE) # Confirm that the variable does exist self.assertTrue(len(self.trans_manager.variables_to_site_map[variable_identifier]) > 0) # Confirm all sites are up for site in self.trans_manager.sites.values(): self.assertTrue(site.status == SiteStatus.UP) read = Instruction("R(%s,%s)" % (trans_identifer, variable_identifier)) with std_out() as (out, err): self.trans_manager.execute(read) output = out.getvalue().strip() self.assertEquals(output, "%s: Read %s - value %s at site %s" % \ (trans_identifer, variable_identifier, str(10), str(2)))
def test_obtain_read_lock_with_existing_write_lock(self): """ Test that a lock cannot be obtained when another Transaction has a lock """ transaction = Transaction("T1", TransactionType.READ_WRITE, 1) instruction = Instruction("W(T1,x2, 103)") self.data_manager.obtain_write_lock(instruction, transaction) transaction = Transaction("T2", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T2, x2)") self.assertTrue("x2" in self.data_manager.locks) self.assertFalse( self.data_manager.obtain_read_lock(transaction, instruction))
def test_read_with_no_locks(self): """ Test the read method, which should return the last committed value """ transaction = Transaction("T1", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T1, x2)") self.assertEquals(self.data_manager.read(transaction, instruction), "20")
def test_begin_ro_transaction_snapshot(self): """ Testing that a snapshot holds the correct values """ instruction = Instruction("BeginRO(T1)") self.transaction_manager.execute(instruction) for site in self.transaction_manager.sites.values(): self.assertTrue(len(site.data_manager.variables) > 0) for variable_identifier in site.data_manager.variables: self.assertTrue(variable_identifier in self. transaction_manager.readonly_snapshots["T1"])
def test_obtain_read_lock_with_ro_transaction(self): """ Test that a lock can be obtained when the Transaction Is Read-Only """ transaction = Transaction("T1", TransactionType.READ_ONLY, 1) instruction = Instruction("R(T1, x2)") self.assertTrue( transaction.transaction_type == TransactionType.READ_ONLY) self.assertTrue( self.data_manager.obtain_read_lock(transaction, instruction))
def test_obtain_read_lock_with_existing_read_lock(self): """ Test that a lock cannot be obtained when another Transaction has a lock """ transaction = Transaction("T1", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T1, x2)") self.assertTrue( self.data_manager.obtain_read_lock(transaction, instruction)) new_lock = self.data_manager.locks["x2"][0] self.assertTrue(new_lock.lock_type == LockType.READ) self.assertEquals(new_lock.transaction.index, 1) transaction = Transaction("T2", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T2, x2)") self.assertTrue("x2" in self.data_manager.locks) self.assertTrue( self.data_manager.obtain_read_lock(transaction, instruction))
def test_dump_variable_instruction_type(self): """ Test a Dump Variable Instruction Type """ instruction = Instruction("dump(x3)") self.assertEquals(instruction.instruction_type, InstructionType.DUMP_VAR) self.assertEquals(instruction.variable_identifier, "x3") self.assertIsNone(instruction.site_identifier, 3) self.assertIsNone(instruction.transaction_identifier) self.assertIsNone(instruction.value)
def test_write_instruction_type(self): """ Test a Write Instruction Type """ instruction = Instruction("W(T1,x3,33)") self.assertEquals(instruction.instruction_type, InstructionType.WRITE) self.assertEquals(instruction.transaction_identifier, "T1") self.assertEquals(instruction.variable_identifier, "x3") self.assertEquals(instruction.value, "33") self.assertIsNone(instruction.site_identifier)
def test_begin_instruction_type(self): """ Test a Begin Instruction Type """ instruction = Instruction("begin(T1)") self.assertEquals(instruction.instruction_type, InstructionType.BEGIN) self.assertEquals(instruction.transaction_identifier, "T1") self.assertIsNone(instruction.variable_identifier) self.assertIsNone(instruction.site_identifier) self.assertIsNone(instruction.value)
def test_fail_instruction_type(self): """ Test a Fail Instruction Type """ instruction = Instruction("fail(3)") self.assertEquals(instruction.instruction_type, InstructionType.FAIL) self.assertEquals(instruction.site_identifier, 3) self.assertIsNone(instruction.transaction_identifier) self.assertIsNone(instruction.variable_identifier) self.assertIsNone(instruction.value)
def test_recover_instruction_type(self): """ Test a Recover Instruction Type """ instruction = Instruction("recover(3)") self.assertEquals(instruction.instruction_type, InstructionType.RECOVER) self.assertEquals(instruction.site_identifier, 3) self.assertIsNone(instruction.transaction_identifier) self.assertIsNone(instruction.variable_identifier) self.assertIsNone(instruction.value)
def test_read_instruction_type(self): """ Test a Read Instruction Type """ instruction = Instruction("R(T2,x2)") self.assertEquals(instruction.instruction_type, InstructionType.READ) self.assertEquals(instruction.transaction_identifier, "T2") self.assertEquals(instruction.variable_identifier, "x2") self.assertIsNone(instruction.site_identifier) self.assertIsNone(instruction.value)
def test_end_instruction_type(self): """ Test a End Instruction Type """ instruction = Instruction("end(T3)") self.assertEquals(instruction.instruction_type, InstructionType.END) self.assertEquals(instruction.transaction_identifier, "T3") self.assertIsNone(instruction.site_identifier) self.assertIsNone(instruction.variable_identifier) self.assertIsNone(instruction.value)
def test_dump_all_instruction_type(self): """ Test a Dump All Instruction Type """ instruction = Instruction("dump()") self.assertEquals(instruction.instruction_type, InstructionType.DUMP_ALL) self.assertIsNone(instruction.transaction_identifier) self.assertIsNone(instruction.variable_identifier) self.assertIsNone(instruction.site_identifier) self.assertIsNone(instruction.value)
def test_obtain_read_lock_when_trans_has_exiting_lock(self): """ Test that a lock can be obtained when same Transaction requests the same lock """ transaction = Transaction("T1", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T2, x2)") self.assertTrue( self.data_manager.obtain_read_lock(transaction, instruction)) self.assertTrue("x2" in self.data_manager.locks) self.assertTrue( self.data_manager.obtain_read_lock(transaction, instruction))
def test_obtain_read_lock_when_variable_unreadable(self): """ Test that a lock cannot be obtained when the Variable Readable is set to False """ transaction = Transaction("T1", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T1, x2)") variable = self.data_manager.variables["x2"] variable.readable = False self.data_manager.variables["x2"] = variable self.assertFalse(self.data_manager.variables["x2"].readable) self.assertFalse( self.data_manager.obtain_read_lock(transaction, instruction))
def test_execute_begin_transaction_read_write(self): """ Testing a Read/Write Begin transaction statement """ instruction = Instruction("Begin(T1)") self.transaction_manager.execute(instruction) trans1 = self.transaction_manager.transactions["T1"] self.assertEquals(len(self.transaction_manager.transactions), 1) self.assertEquals(trans1.identifier, "T1") self.assertEquals(trans1.transaction_type, TransactionType.READ_WRITE) self.assertEquals(trans1.start_time, 1) self.assertIsNone(trans1.end_time) self.assertEquals(trans1.state, TransactionState.RUNNING)
def test_execute_dump_site_transaction(self): """ Given a Dump Site instruction, dump method will be called """ instruction = Instruction("dump(3)") with std_out() as (out, err): self.transaction_manager.execute(instruction) output = out.getvalue().strip() self.assertEqual( output, "{'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}" )
def test_execute_dump_all_transaction(self): """ Given a Dump All instruction, dump method will be called """ instruction = Instruction("dump()") with std_out() as (out, err): self.transaction_manager.execute(instruction) output = out.getvalue().strip() self.assertEqual( output, "{1: {'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}, 2: {'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x11': { x11: 110 }, 'x12': { x12: 120 }, 'x1': { x1: 10 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}, 3: {'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}, 4: {'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x3': { x3: 30 }, 'x12': { x12: 120 }, 'x13': { x13: 130 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}, 5: {'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}, 6: {'x14': { x14: 140 }, 'x20': { x20: 200 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x15': { x15: 150 }, 'x4': { x4: 40 }, 'x5': { x5: 50 }}, 7: {'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}, 8: {'x14': { x14: 140 }, 'x20': { x20: 200 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x7': { x7: 70 }, 'x4': { x4: 40 }, 'x17': { x17: 170 }}, 9: {'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}, 10: {'x19': { x19: 190 }, 'x14': { x14: 140 }, 'x18': { x18: 180 }, 'x10': { x10: 100 }, 'x8': { x8: 80 }, 'x9': { x9: 90 }, 'x16': { x16: 160 }, 'x2': { x2: 20 }, 'x12': { x12: 120 }, 'x6': { x6: 60 }, 'x20': { x20: 200 }, 'x4': { x4: 40 }}}" )
def test_obtain_read_lock_with_no_existing_locks(self): """ Test that a lock can be obtained when no locks exist """ transaction = Transaction("T1", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T1, x2)") self.assertEquals(instruction.variable_identifier, "x2") variable = self.data_manager.variables["x2"] self.assertTrue(variable.readable) self.assertFalse("x2" in self.data_manager.locks) value = self.data_manager.obtain_read_lock(transaction, instruction) self.assertEquals(len(self.data_manager.locks), 1) self.assertTrue(value)
def test_execute_begin_ro_transaction(self): """ Testing a Read Only Begin transaction statement """ instruction = Instruction("BeginRO(T1)") self.transaction_manager.execute(instruction) trans1 = self.transaction_manager.transactions["T1"] self.assertTrue("T1" in self.transaction_manager.transactions) self.assertEquals( self.transaction_manager.transactions["T1"].transaction_type, TransactionType.READ_ONLY) for site in self.transaction_manager.sites.values(): for variable_identifier in site.data_manager.variables: self.assertTrue(variable_identifier in self. transaction_manager.readonly_snapshots["T1"]) self.assertEquals(trans1.identifier, "T1") self.assertEquals(trans1.transaction_type, TransactionType.READ_ONLY) self.assertEquals(trans1.start_time, 1) self.assertIsNone(trans1.end_time) self.assertEquals(trans1.state, TransactionState.RUNNING)
def test_obtain_read_lock_with_existing_locks(self): """ Test that a lock can be obtained when other locks exist """ dummy_tran = Transaction("T3", TransactionType.READ_WRITE, 1) dummy_var = Variable(1, 4) dummy_var1 = Variable(1, 6) self.data_manager.locks["x4"] = [ Lock(LockType.READ, dummy_tran, dummy_var) ] self.data_manager.locks["x6"] = [ Lock(LockType.WRITE, dummy_tran, dummy_var1) ] transaction = Transaction("T1", TransactionType.READ_WRITE, 1) instruction = Instruction("R(T1, x2)") variable = self.data_manager.variables["x2"] self.assertTrue(variable.readable) self.assertFalse("x2" in self.data_manager.locks) value = self.data_manager.obtain_read_lock(transaction, instruction) self.assertTrue("x2" in self.data_manager.locks) self.assertTrue(value)
def test_read_instruction_with_no_transaction(self): """ Ensure a Transaction was started before a read is attempted """ instruction = Instruction("R(T3,x4)") self.assertRaises(ValueError, self.trans_manager.execute, instruction)