X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=tests%2Fbindings%2Fpython%2Fbt2%2Ftest_trace_class.py;h=67f6456b7f894c87e4291b021c0e2125894d03c4;hp=69397c633d08cd414723c268631a351d8c162329;hb=0235b0db7de5bcacdb3650c92461f2ce5eb2143d;hpb=f0a42b33ac3951cd5cb2ee0f66ac04437a681621 diff --git a/tests/bindings/python/bt2/test_trace_class.py b/tests/bindings/python/bt2/test_trace_class.py index 69397c63..67f6456b 100644 --- a/tests/bindings/python/bt2/test_trace_class.py +++ b/tests/bindings/python/bt2/test_trace_class.py @@ -1,20 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0-only # # Copyright (C) 2019 EfficiOS Inc. # -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; only version 2 -# of the License. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. -# import unittest from utils import ( @@ -24,6 +11,7 @@ from utils import ( ) from bt2 import stream_class as bt2_stream_class from bt2 import trace_class as bt2_trace_class +from bt2 import utils as bt2_utils class TraceClassTestCase(unittest.TestCase): @@ -68,6 +56,14 @@ class TraceClassTestCase(unittest.TestCase): self.assertRaisesInComponentInit(TypeError, f) + def test_create_invalid_automatic_stream_class_id_type(self): + def f(comp_self): + return comp_self._create_trace_class( + assigns_automatic_stream_class_id='perchaude' + ) + + self.assertRaisesInComponentInit(TypeError, f) + def test_automatic_stream_class_id(self): def f(comp_self): return comp_self._create_trace_class(assigns_automatic_stream_class_id=True) @@ -175,16 +171,72 @@ class TraceClassTestCase(unittest.TestCase): def test_destruction_listener(self): def on_trace_class_destruction(trace_class): - nonlocal trace_class_destroyed - trace_class_destroyed = True + nonlocal num_destruct_calls + num_destruct_calls += 1 - trace_class_destroyed = False + num_destruct_calls = 0 trace_class = get_default_trace_class() - trace_class.add_destruction_listener(on_trace_class_destruction) - self.assertFalse(trace_class_destroyed) + handle1 = trace_class.add_destruction_listener(on_trace_class_destruction) + self.assertIs(type(handle1), bt2_utils._ListenerHandle) + + handle2 = trace_class.add_destruction_listener(on_trace_class_destruction) + + trace_class.remove_destruction_listener(handle2) + + self.assertEqual(num_destruct_calls, 0) + + del trace_class + + self.assertEqual(num_destruct_calls, 1) + + def test_remove_destruction_listener_wrong_type(self): + trace_class = get_default_trace_class() + + with self.assertRaisesRegex( + TypeError, r"'int' is not a '' object" + ): + trace_class.remove_destruction_listener(123) + + def test_remove_destruction_listener_wrong_object(self): + def on_trace_class_destruction(trace_class): + pass + + trace_class_1 = get_default_trace_class() + trace_class_2 = get_default_trace_class() + + handle1 = trace_class_1.add_destruction_listener(on_trace_class_destruction) + + with self.assertRaisesRegex( + ValueError, + r'This trace class destruction listener does not match the trace class object\.', + ): + trace_class_2.remove_destruction_listener(handle1) + + def test_remove_destruction_listener_twice(self): + def on_trace_class_destruction(trace_class): + pass + + trace_class = get_default_trace_class() + handle = trace_class.add_destruction_listener(on_trace_class_destruction) + + trace_class.remove_destruction_listener(handle) + + with self.assertRaisesRegex( + ValueError, r'This trace class destruction listener was already removed\.' + ): + trace_class.remove_destruction_listener(handle) + + def test_raise_in_destruction_listener(self): + def on_trace_class_destruction(trace_class): + raise ValueError('it hurts') + + trace_class = get_default_trace_class() + trace_class.add_destruction_listener(on_trace_class_destruction) del trace_class - self.assertTrue(trace_class_destroyed) + +if __name__ == '__main__': + unittest.main()