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.
22 from utils
import run_in_component_init
, get_default_trace_class
25 class TraceClassTestCase(unittest
.TestCase
):
27 def test_create_default(self
):
29 return comp_self
._create
_trace
_class
()
31 tc
= run_in_component_init(f
)
33 self
.assertEqual(len(tc
), 0)
34 self
.assertEqual(len(tc
.env
), 0)
35 self
.assertIsNone(tc
.uuid
)
36 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
40 return comp_self
._create
_trace
_class
(uuid
=uuid
.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
42 tc
= run_in_component_init(f
)
44 self
.assertEqual(tc
.uuid
, uuid
.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
46 def test_automatic_stream_class_id(self
):
48 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
50 tc
= run_in_component_init(f
)
51 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
53 # This should not throw.
54 sc1
= tc
.create_stream_class()
55 sc2
= tc
.create_stream_class()
57 self
.assertNotEqual(sc1
.id, sc2
.id)
59 def test_automatic_stream_class_id_raises(self
):
61 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
63 tc
= run_in_component_init(f
)
64 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
66 with self
.assertRaises(ValueError):
67 sc1
= tc
.create_stream_class(23)
69 def test_no_assigns_automatic_stream_class_id(self
):
71 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
73 tc
= run_in_component_init(f
)
74 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
76 sc
= tc
.create_stream_class(id=28)
77 self
.assertEqual(sc
.id, 28)
79 def test_no_assigns_automatic_stream_class_id_raises(self
):
81 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
83 tc
= run_in_component_init(f
)
84 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
86 # In this mode, it is required to pass an explicit id.
87 with self
.assertRaises(ValueError):
88 tc
.create_stream_class()
90 def test_env_get(self
):
92 return comp_self
._create
_trace
_class
(env
={'hello': 'you', 'foo': -5})
94 tc
= run_in_component_init(f
)
96 self
.assertEqual(tc
.env
['hello'], 'you')
97 self
.assertEqual(tc
.env
['foo'], -5)
99 def test_env_get_non_existent(self
):
101 return comp_self
._create
_trace
_class
(env
={'hello': 'you', 'foo': -5})
103 tc
= run_in_component_init(f
)
105 with self
.assertRaises(KeyError):
109 def _create_trace_class_with_some_stream_classes():
111 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
113 tc
= run_in_component_init(f
)
114 sc1
= tc
.create_stream_class(id=12)
115 sc2
= tc
.create_stream_class(id=54)
116 sc3
= tc
.create_stream_class(id=2018)
117 return tc
, sc1
, sc2
, sc3
119 def test_getitem(self
):
120 tc
, _
, _
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
121 self
.assertEqual(tc
[2018].addr
, sc3
.addr
)
123 def test_getitem_wrong_key_type(self
):
124 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
125 with self
.assertRaises(TypeError):
128 def test_getitem_wrong_key(self
):
129 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
130 with self
.assertRaises(KeyError):
134 tc
= get_default_trace_class()
135 self
.assertEqual(len(tc
), 0)
136 tc
.create_stream_class()
137 self
.assertEqual(len(tc
), 1)
140 tc
, sc1
, sc2
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
142 for sc_id
, stream_class
in tc
.items():
143 self
.assertIsInstance(stream_class
, bt2
.stream_class
._StreamClass
)
146 self
.assertEqual(stream_class
.addr
, sc1
.addr
)
148 self
.assertEqual(stream_class
.addr
, sc2
.addr
)
150 self
.assertEqual(stream_class
.addr
, sc3
.addr
)
152 def test_destruction_listener(self
):
153 def on_trace_class_destruction(trace_class
):
154 nonlocal trace_class_destroyed
155 trace_class_destroyed
= True
157 trace_class_destroyed
= False
159 trace_class
= get_default_trace_class()
160 trace_class
.add_destruction_listener(on_trace_class_destruction
)
162 self
.assertFalse(trace_class_destroyed
)
166 self
.assertTrue(trace_class_destroyed
)