Apply black code formatter on all Python code
[babeltrace.git] / tests / bindings / python / bt2 / test_trace_class.py
CommitLineData
d2d857a8
MJ
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
fbbe9302
SM
19import unittest
20import bt2
21from utils import run_in_component_init, get_default_trace_class
22
23
24class TraceClassTestCase(unittest.TestCase):
fbbe9302
SM
25 def test_create_default(self):
26 def f(comp_self):
27 return comp_self._create_trace_class()
28
29 tc = run_in_component_init(f)
30
31 self.assertEqual(len(tc), 0)
fbbe9302
SM
32 self.assertTrue(tc.assigns_automatic_stream_class_id)
33
fbbe9302
SM
34 def test_automatic_stream_class_id(self):
35 def f(comp_self):
36 return comp_self._create_trace_class(assigns_automatic_stream_class_id=True)
37
38 tc = run_in_component_init(f)
39 self.assertTrue(tc.assigns_automatic_stream_class_id)
40
41 # This should not throw.
42 sc1 = tc.create_stream_class()
43 sc2 = tc.create_stream_class()
44
45 self.assertNotEqual(sc1.id, sc2.id)
46
47 def test_automatic_stream_class_id_raises(self):
48 def f(comp_self):
49 return comp_self._create_trace_class(assigns_automatic_stream_class_id=True)
50
51 tc = run_in_component_init(f)
52 self.assertTrue(tc.assigns_automatic_stream_class_id)
53
4430bc80 54 with self.assertRaises(ValueError):
fbbe9302
SM
55 sc1 = tc.create_stream_class(23)
56
57 def test_no_assigns_automatic_stream_class_id(self):
58 def f(comp_self):
cfbd7cf3
FD
59 return comp_self._create_trace_class(
60 assigns_automatic_stream_class_id=False
61 )
fbbe9302
SM
62
63 tc = run_in_component_init(f)
64 self.assertFalse(tc.assigns_automatic_stream_class_id)
65
66 sc = tc.create_stream_class(id=28)
67 self.assertEqual(sc.id, 28)
68
69 def test_no_assigns_automatic_stream_class_id_raises(self):
70 def f(comp_self):
cfbd7cf3
FD
71 return comp_self._create_trace_class(
72 assigns_automatic_stream_class_id=False
73 )
fbbe9302
SM
74
75 tc = run_in_component_init(f)
76 self.assertFalse(tc.assigns_automatic_stream_class_id)
77
78 # In this mode, it is required to pass an explicit id.
4430bc80 79 with self.assertRaises(ValueError):
fbbe9302
SM
80 tc.create_stream_class()
81
fbbe9302
SM
82 @staticmethod
83 def _create_trace_class_with_some_stream_classes():
84 def f(comp_self):
cfbd7cf3
FD
85 return comp_self._create_trace_class(
86 assigns_automatic_stream_class_id=False
87 )
fbbe9302
SM
88
89 tc = run_in_component_init(f)
90 sc1 = tc.create_stream_class(id=12)
91 sc2 = tc.create_stream_class(id=54)
92 sc3 = tc.create_stream_class(id=2018)
93 return tc, sc1, sc2, sc3
94
95 def test_getitem(self):
96 tc, _, _, sc3 = self._create_trace_class_with_some_stream_classes()
97 self.assertEqual(tc[2018].addr, sc3.addr)
98
99 def test_getitem_wrong_key_type(self):
100 tc, _, _, _ = self._create_trace_class_with_some_stream_classes()
101 with self.assertRaises(TypeError):
102 tc['hello']
103
104 def test_getitem_wrong_key(self):
105 tc, _, _, _ = self._create_trace_class_with_some_stream_classes()
106 with self.assertRaises(KeyError):
107 tc[4]
108
109 def test_len(self):
110 tc = get_default_trace_class()
111 self.assertEqual(len(tc), 0)
112 tc.create_stream_class()
113 self.assertEqual(len(tc), 1)
114
115 def test_iter(self):
116 tc, sc1, sc2, sc3 = self._create_trace_class_with_some_stream_classes()
117
118 for sc_id, stream_class in tc.items():
2c6f8520 119 self.assertIsInstance(stream_class, bt2.stream_class._StreamClass)
fbbe9302
SM
120
121 if sc_id == 12:
122 self.assertEqual(stream_class.addr, sc1.addr)
123 elif sc_id == 54:
124 self.assertEqual(stream_class.addr, sc2.addr)
125 elif sc_id == 2018:
126 self.assertEqual(stream_class.addr, sc3.addr)
127
128 def test_destruction_listener(self):
129 def on_trace_class_destruction(trace_class):
130 nonlocal trace_class_destroyed
131 trace_class_destroyed = True
132
133 trace_class_destroyed = False
134
135 trace_class = get_default_trace_class()
136 trace_class.add_destruction_listener(on_trace_class_destruction)
137
138 self.assertFalse(trace_class_destroyed)
139
140 del trace_class
141
142 self.assertTrue(trace_class_destroyed)
This page took 0.03358 seconds and 4 git commands to generate.