tests: Add missing copyright headers
[babeltrace.git] / tests / bindings / python / bt2 / test_trace.py
1 #
2 # Copyright (C) 2019 EfficiOS Inc.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; only version 2
7 # of the License.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #
18
19 import unittest
20 from utils import get_default_trace_class
21
22
23 class TraceTestCase(unittest.TestCase):
24 def setUp(self):
25 self._tc = get_default_trace_class()
26
27 def test_create_default(self):
28 trace = self._tc()
29 self.assertEqual(trace.name, None)
30
31 def test_create_full(self):
32 trace = self._tc(name='my name')
33 self.assertEqual(trace.name, 'my name')
34
35 def test_create_invalid_name(self):
36 with self.assertRaises(TypeError):
37 self._tc(name=17)
38
39 def test_attr_trace_class(self):
40 trace = self._tc(name='my name')
41 self.assertEqual(trace.cls.addr, self._tc.addr)
42
43 def test_len(self):
44 trace = self._tc()
45 sc = self._tc.create_stream_class()
46 self.assertEqual(len(trace), 0)
47
48 trace.create_stream(sc)
49 self.assertEqual(len(trace), 1)
50
51 def _create_trace_with_some_streams(self):
52 sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
53 trace = self._tc()
54 trace.create_stream(sc, id=12)
55 trace.create_stream(sc, id=15)
56 trace.create_stream(sc, id=17)
57
58 return trace
59
60 def test_iter(self):
61 trace = self._create_trace_with_some_streams()
62 stream_ids = set(trace)
63 self.assertEqual(stream_ids, {12, 15, 17})
64
65 def test_getitem(self):
66 trace = self._create_trace_with_some_streams()
67
68 self.assertEqual(trace[12].id, 12)
69
70 def test_getitem_invalid_key(self):
71 trace = self._create_trace_with_some_streams()
72 with self.assertRaises(KeyError):
73 trace[18]
74
75 def test_destruction_listener(self):
76 def on_trace_class_destruction(trace_class):
77 nonlocal trace_class_destroyed
78 trace_class_destroyed = True
79
80 def on_trace_destruction(trace):
81 nonlocal trace_destroyed
82 trace_destroyed = True
83
84 trace_destroyed = False
85 trace_class_destroyed = False
86
87 trace_class = get_default_trace_class()
88 stream_class = trace_class.create_stream_class()
89 trace = trace_class()
90 stream = trace.create_stream(stream_class)
91
92 trace_class.add_destruction_listener(on_trace_class_destruction)
93 trace.add_destruction_listener(on_trace_destruction)
94
95 self.assertFalse(trace_class_destroyed)
96 self.assertFalse(trace_destroyed)
97
98 del trace
99
100 self.assertFalse(trace_class_destroyed)
101 self.assertFalse(trace_destroyed)
102
103 del stream
104
105 self.assertFalse(trace_class_destroyed)
106 self.assertTrue(trace_destroyed)
107
108 del trace_class
109
110 self.assertFalse(trace_class_destroyed)
111 self.assertTrue(trace_destroyed)
112
113 del stream_class
114
115 self.assertTrue(trace_class_destroyed)
116 self.assertTrue(trace_destroyed)
This page took 0.031734 seconds and 4 git commands to generate.