+ self.assertEqual(num_trace_class_destroyed_calls, 1)
+ self.assertEqual(num_trace_destroyed_calls, 1)
+
+ def test_remove_destruction_listener_wrong_type(self):
+ trace_class = get_default_trace_class()
+ trace = trace_class()
+
+ with self.assertRaisesRegex(
+ TypeError, r"'int' is not a '<class 'bt2.utils._ListenerHandle'>' object"
+ ):
+ trace.remove_destruction_listener(123)
+
+ def test_remove_destruction_listener_wrong_object(self):
+ def on_trace_destruction(trace):
+ pass
+
+ trace_class_1 = get_default_trace_class()
+ trace1 = trace_class_1()
+ trace_class_2 = get_default_trace_class()
+ trace2 = trace_class_2()
+
+ handle1 = trace1.add_destruction_listener(on_trace_destruction)
+
+ with self.assertRaisesRegex(
+ ValueError,
+ r'This trace destruction listener does not match the trace object\.',
+ ):
+ trace2.remove_destruction_listener(handle1)
+
+ def test_remove_destruction_listener_twice(self):
+ def on_trace_destruction(trace_class):
+ pass
+
+ trace_class = get_default_trace_class()
+ trace = trace_class()
+ handle = trace.add_destruction_listener(on_trace_destruction)
+
+ trace.remove_destruction_listener(handle)
+
+ with self.assertRaisesRegex(
+ ValueError, r'This trace destruction listener was already removed\.'
+ ):
+ trace.remove_destruction_listener(handle)
+
+ def test_raise_in_destruction_listener(self):
+ def on_trace_destruction(trace):
+ raise ValueError('it hurts')
+
+ trace_class = get_default_trace_class()
+ trace = trace_class()
+ trace.add_destruction_listener(on_trace_destruction)
+
+ del trace
+
+
+if __name__ == '__main__':
+ unittest.main()