def test_clear_break(self): """ Tests clearing a breakpoint. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) clear_break_offset = 14 set_break_offset = 8 db.cmd_manager.enqueue(lambda t: t.clear_break( self.filename, sys._getframe().f_lineno + clear_break_offset )) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) db.set_break( self.filename, sys._getframe().f_lineno + set_break_offset ) db.set_trace(stop=False) continued = False with Timeout(0.1): db.set_trace(stop=False) for n in xrange(2): pass continued = True self.assertTrue(continued)
def test_set_trace_without_stop(self): """ Asserts that calling set_trace with stop=False will start tracing but not stop. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) line_offset = 8 # The difference in the set_break call and line_3. db.set_break( self.filename, sys._getframe().f_lineno + line_offset, ) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace(stop=False) # Should not stop us here. line_1 = True line_2 = True line_3 = True # Since we are stepping, we should not hit this line. self.assertTrue(line_1) self.assertTrue(line_2) # We should have still stopped at this breakpoint if we are tracing. self.assertFalse(line_3) db.disable() db = Qdb(cmd_manager=NopCommandManager) line_1 = False with Timeout(0.1, False): db.set_trace(stop=False) line_1 = True self.assertTrue(line_1)
def test_clear_break(self): """ Tests clearing a breakpoint. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) clear_break_offset = 14 set_break_offset = 8 db.cmd_manager.enqueue(lambda t: t.clear_break( self.filename, sys._getframe().f_lineno + clear_break_offset)) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) db.set_break(self.filename, sys._getframe().f_lineno + set_break_offset) db.set_trace(stop=False) continued = False with Timeout(0.1): db.set_trace(stop=False) for n in xrange(2): pass continued = True self.assertTrue(continued)
def test_conditional_breakpoint_timeout(self): """ Tests conditional breakpoints that cause timeouts. WARNING: This test relies on the relative line numbers inside the test. """ stopped = [False] def stop(): stopped[0] = True return True # Execute the assertion. line = None cond = 'g()' cmd_manager = QueueCommandManager() db = Qdb(cmd_manager=cmd_manager, execution_timeout=1) cmd_manager.enqueue(lambda t: stop() and self.assertEqual(line, 1)) line_offset = 10 # Set a condition that will time out. db.set_break( self.filename, sys._getframe().f_lineno + line_offset, cond='g()', ) db.set_trace(stop=False) def g(): while True: pass line = 1 line = 2 line = 3 db.disable() errors = [e['p'] for e in cmd_manager.sent if e['e'] == 'error'] self.assertEqual(len(errors), 1) error = errors[0] self.assertEqual(error['type'], 'condition') negative_line_offset = 14 exc = QdbExecutionTimeout(cond, db.execution_timeout) self.assertEqual( error['data'], { 'line': sys._getframe().f_lineno - negative_line_offset, 'cond': cond, 'exc': type(exc).__name__, 'output': db.exception_serializer(exc), } ) # Make sure we stopped when we raised the exception. self.assertTrue(stopped[0])
def test_conditional_breakpoint_raises(self): """ Tests conditional breakpoints that raise an exception. WARNING: This test relies on the relative line numbers inside the test. """ line = None exc = ValueError('lol wut r u doing?') cond = 'raiser()' stopped = [False] def stop(): stopped[0] = True return True # Execute the assertion. cmd_manager = QueueCommandManager() db = Qdb(cmd_manager=cmd_manager) cmd_manager.enqueue(lambda t: stop() and self.assertEqual(line, 1)) line_offset = 9 # Set a condition that will raise a ValueError. db.set_break( self.filename, sys._getframe().f_lineno + line_offset, cond=cond, ) db.set_trace(stop=False) def raiser(): raise exc line = 1 line = 2 # This line number is used in the data assertion. line = 3 db.disable() errors = [e['p'] for e in cmd_manager.sent if e['e'] == 'error'] self.assertEqual(len(errors), 1) error = errors[0] self.assertEqual(error['type'], 'condition') negative_line_offset = 13 self.assertEqual( error['data'], { 'line': sys._getframe().f_lineno - negative_line_offset, 'cond': cond, 'exc': type(exc).__name__, 'output': db.exception_serializer(exc), } ) # Make sure we stopped when we raised the exception. self.assertTrue(stopped[0])
def test_conditional_breakpoint(self): """ Tests valid conditional breakpoints. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) loop_counter = 0 db.cmd_manager.enqueue(lambda t: self.assertEqual(loop_counter, 5)) line_offset = 5 db.set_break(self.filename, sys._getframe().f_lineno + line_offset, cond='loop_counter == 5') db.set_trace(stop=False) while loop_counter < 10: loop_counter += 1
def test_conditional_breakpoint(self): """ Tests valid conditional breakpoints. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) loop_counter = 0 db.cmd_manager.enqueue(lambda t: self.assertEqual(loop_counter, 5)) line_offset = 5 db.set_break( self.filename, sys._getframe().f_lineno + line_offset, cond='loop_counter == 5' ) db.set_trace(stop=False) while loop_counter < 10: loop_counter += 1
def test_set_continue_with_breaks(self): """ Tests the behavior of continue when there are breakpoints in the mix. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) line_offset = 8 # The difference in the set_break call and line_2. db.set_break( self.filename, sys._getframe().f_lineno + line_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True # Assert we only got to line_1 because of the breakpoint. # These are split up to give more helpful messages if the test fails. self.assertTrue(line_1) self.assertFalse(line_2) self.assertFalse(line_3) # We are still in stepping mode so we should be reporting the stack. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),') sys.settrace(None) db = Qdb(cmd_manager=QueueCommandManager) line_2_offset = 13 # The difference in the set_break call and line_2. line_3_offset = 10 # THe difference in the set_break call and line_3. db.set_break( self.filename, sys._getframe().f_lineno + line_2_offset, ) db.set_break( self.filename, sys._getframe().f_lineno + line_3_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True self.assertTrue(line_1) self.assertTrue(line_2) self.assertFalse(line_3) self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),')
def test_temporary_breakpoint(self): """ Tests conditional breakpoints. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) loop_counter = 0 line_offset = 6 db.set_break( self.filename, sys._getframe().f_lineno + line_offset, temporary=True, ) with Timeout(0.1, False): db.set_trace(stop=False) while loop_counter < 10: loop_counter += 1 # By hitting it the first time, we cleared the breakpoint and did not # stop there again. self.assertEqual(loop_counter, 10)
def test_set_continue_with_breaks(self): """ Tests the behavior of continue when there are breakpoints in the mix. WARNING: This test relies on the relative line numbers inside the test. """ db = Qdb(cmd_manager=QueueCommandManager) line_offset = 8 # The difference in the set_break call and line_2. db.set_break( self.filename, sys._getframe().f_lineno + line_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True # Assert we only got to line_1 because of the breakpoint. # These are split up to give more helpful messages if the test fails. self.assertTrue(line_1) self.assertFalse(line_2) self.assertFalse(line_3) # We are still in stepping mode so we should be reporting the stack. self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),' ) sys.settrace(None) db = Qdb(cmd_manager=QueueCommandManager) line_2_offset = 13 # The difference in the set_break call and line_2. line_3_offset = 10 # THe difference in the set_break call and line_3. db.set_break( self.filename, sys._getframe().f_lineno + line_2_offset, ) db.set_break( self.filename, sys._getframe().f_lineno + line_3_offset, ) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.enqueue(lambda t: t.set_continue()) db.cmd_manager.user_wait(0.2) line_1 = line_2 = line_3 = False with Timeout(0.1, False): db.set_trace() line_1 = True line_2 = True line_3 = True self.assertTrue(line_1) self.assertTrue(line_2) self.assertFalse(line_3) self.assertEqual( db.get_line(self.filename, db.curframe.f_lineno), ' db.get_line(self.filename, db.curframe.f_lineno),' )