2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
21 run_in_component_init
,
22 get_default_trace_class
,
23 get_const_stream_beginning_message
,
25 from bt2
import stream_class
as bt2_stream_class
26 from bt2
import trace_class
as bt2_trace_class
29 class TraceClassTestCase(unittest
.TestCase
):
30 def assertRaisesInComponentInit(self
, expected_exc_type
, user_code
):
34 except Exception as exc
:
37 exc_type
= run_in_component_init(f
)
38 self
.assertIsNotNone(exc_type
)
39 self
.assertEqual(exc_type
, expected_exc_type
)
41 def test_create_default(self
):
43 return comp_self
._create
_trace
_class
()
45 tc
= run_in_component_init(f
)
47 self
.assertEqual(len(tc
), 0)
48 self
.assertIs(type(tc
), bt2_trace_class
._TraceClass
)
49 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
50 self
.assertEqual(len(tc
.user_attributes
), 0)
52 def test_create_user_attributes(self
):
54 return comp_self
._create
_trace
_class
(user_attributes
={'salut': 23})
56 tc
= run_in_component_init(f
)
57 self
.assertEqual(tc
.user_attributes
, {'salut': 23})
59 def test_create_invalid_user_attributes(self
):
61 return comp_self
._create
_trace
_class
(user_attributes
=object())
63 self
.assertRaisesInComponentInit(TypeError, f
)
65 def test_create_invalid_user_attributes_value_type(self
):
67 return comp_self
._create
_trace
_class
(user_attributes
=23)
69 self
.assertRaisesInComponentInit(TypeError, f
)
71 def test_automatic_stream_class_id(self
):
73 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
75 tc
= run_in_component_init(f
)
76 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
78 # This should not throw.
79 sc1
= tc
.create_stream_class()
80 sc2
= tc
.create_stream_class()
82 self
.assertIs(type(sc1
), bt2_stream_class
._StreamClass
)
83 self
.assertIs(type(sc2
), bt2_stream_class
._StreamClass
)
84 self
.assertNotEqual(sc1
.id, sc2
.id)
86 def test_automatic_stream_class_id_raises(self
):
88 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
90 tc
= run_in_component_init(f
)
91 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
93 with self
.assertRaises(ValueError):
94 tc
.create_stream_class(23)
96 def test_no_assigns_automatic_stream_class_id(self
):
98 return comp_self
._create
_trace
_class
(
99 assigns_automatic_stream_class_id
=False
102 tc
= run_in_component_init(f
)
103 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
105 sc
= tc
.create_stream_class(id=28)
106 self
.assertEqual(sc
.id, 28)
108 def test_no_assigns_automatic_stream_class_id_raises(self
):
110 return comp_self
._create
_trace
_class
(
111 assigns_automatic_stream_class_id
=False
114 tc
= run_in_component_init(f
)
115 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
117 # In this mode, it is required to pass an explicit id.
118 with self
.assertRaises(ValueError):
119 tc
.create_stream_class()
122 def _create_trace_class_with_some_stream_classes():
124 return comp_self
._create
_trace
_class
(
125 assigns_automatic_stream_class_id
=False
128 tc
= run_in_component_init(f
)
129 sc1
= tc
.create_stream_class(id=12)
130 sc2
= tc
.create_stream_class(id=54)
131 sc3
= tc
.create_stream_class(id=2018)
132 return tc
, sc1
, sc2
, sc3
134 def test_getitem(self
):
135 tc
, _
, _
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
136 self
.assertIs(type(tc
[2018]), bt2_stream_class
._StreamClass
)
137 self
.assertEqual(tc
[2018].addr
, sc3
.addr
)
139 def test_const_getitem(self
):
140 const_tc
= get_const_stream_beginning_message().stream
.trace
.cls
141 self
.assertIs(type(const_tc
[0]), bt2_stream_class
._StreamClassConst
)
143 def test_getitem_wrong_key_type(self
):
144 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
145 with self
.assertRaises(TypeError):
148 def test_getitem_wrong_key(self
):
149 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
150 with self
.assertRaises(KeyError):
154 tc
= get_default_trace_class()
155 self
.assertEqual(len(tc
), 0)
156 tc
.create_stream_class()
157 self
.assertEqual(len(tc
), 1)
160 tc
, sc1
, sc2
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
162 for sc_id
, stream_class
in tc
.items():
164 self
.assertIs(type(stream_class
), bt2_stream_class
._StreamClass
)
165 self
.assertEqual(stream_class
.addr
, sc1
.addr
)
167 self
.assertEqual(stream_class
.addr
, sc2
.addr
)
169 self
.assertEqual(stream_class
.addr
, sc3
.addr
)
171 def test_const_iter(self
):
172 const_tc
= get_const_stream_beginning_message().stream
.trace
.cls
173 const_sc
= list(const_tc
.values())[0]
174 self
.assertIs(type(const_sc
), bt2_stream_class
._StreamClassConst
)
176 def test_destruction_listener(self
):
177 def on_trace_class_destruction(trace_class
):
178 nonlocal trace_class_destroyed
179 trace_class_destroyed
= True
181 trace_class_destroyed
= False
183 trace_class
= get_default_trace_class()
184 trace_class
.add_destruction_listener(on_trace_class_destruction
)
186 self
.assertFalse(trace_class_destroyed
)
190 self
.assertTrue(trace_class_destroyed
)