Back to index

apport  2.4
test_rethread.py
Go to the documentation of this file.
00001 import unittest, time, traceback, sys
00002 
00003 import apport.REThread
00004 
00005 
00006 def idle(seconds):
00007     '''Test thread to just wait a bit.'''
00008 
00009     time.sleep(seconds)
00010 
00011 
00012 def div(x, y):
00013     '''Test thread to divide two numbers.'''
00014 
00015     return x / y
00016 
00017 
00018 class T(unittest.TestCase):
00019     def test_return_value(self):
00020         '''return value works properly.'''
00021 
00022         t = apport.REThread.REThread(target=div, args=(42, 2))
00023         t.start()
00024         t.join()
00025         # exc_raise() should be a no-op on successful functions
00026         t.exc_raise()
00027         self.assertEqual(t.return_value(), 21)
00028         self.assertEqual(t.exc_info(), None)
00029 
00030     def test_no_return_value(self):
00031         '''apport.REThread.REThread works if run() does not return anything.'''
00032 
00033         t = apport.REThread.REThread(target=idle, args=(0.5,))
00034         t.start()
00035         # thread must be joined first
00036         self.assertRaises(AssertionError, t.return_value)
00037         t.join()
00038         self.assertEqual(t.return_value(), None)
00039         self.assertEqual(t.exc_info(), None)
00040 
00041     def test_exception(self):
00042         '''exception in thread is caught and passed.'''
00043 
00044         t = apport.REThread.REThread(target=div, args=(1, 0))
00045         t.start()
00046         t.join()
00047         # thread did not terminate normally, no return value
00048         self.assertRaises(AssertionError, t.return_value)
00049         self.assertTrue(t.exc_info()[0] == ZeroDivisionError)
00050         exc = traceback.format_exception(t.exc_info()[0], t.exc_info()[1],
00051                                          t.exc_info()[2])
00052         self.assertTrue(exc[-1].startswith('ZeroDivisionError'), 'not a ZeroDivisionError:' + str(exc))
00053         self.assertTrue(exc[-2].endswith('return x / y\n'))
00054 
00055     def test_exc_raise(self):
00056         '''exc_raise() raises caught thread exception.'''
00057 
00058         t = apport.REThread.REThread(target=div, args=(1, 0))
00059         t.start()
00060         t.join()
00061         # thread did not terminate normally, no return value
00062         self.assertRaises(AssertionError, t.return_value)
00063         raised = False
00064         try:
00065             t.exc_raise()
00066         except:
00067             raised = True
00068             e = sys.exc_info()
00069             exc = traceback.format_exception(e[0], e[1], e[2])
00070             self.assertTrue(exc[-1].startswith('ZeroDivisionError'), 'not a ZeroDivisionError:' + str(e))
00071             self.assertTrue(exc[-2].endswith('return x / y\n'))
00072         self.assertTrue(raised)
00073 
00074 unittest.main()