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 from utils
import run_in_component_init
, get_default_trace_class
24 class TraceClassTestCase(unittest
.TestCase
):
26 def test_create_default(self
):
28 return comp_self
._create
_trace
_class
()
30 tc
= run_in_component_init(f
)
32 self
.assertEqual(len(tc
), 0)
33 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
35 def test_automatic_stream_class_id(self
):
37 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
39 tc
= run_in_component_init(f
)
40 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
42 # This should not throw.
43 sc1
= tc
.create_stream_class()
44 sc2
= tc
.create_stream_class()
46 self
.assertNotEqual(sc1
.id, sc2
.id)
48 def test_automatic_stream_class_id_raises(self
):
50 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
52 tc
= run_in_component_init(f
)
53 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
55 with self
.assertRaises(ValueError):
56 sc1
= tc
.create_stream_class(23)
58 def test_no_assigns_automatic_stream_class_id(self
):
60 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
62 tc
= run_in_component_init(f
)
63 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
65 sc
= tc
.create_stream_class(id=28)
66 self
.assertEqual(sc
.id, 28)
68 def test_no_assigns_automatic_stream_class_id_raises(self
):
70 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
72 tc
= run_in_component_init(f
)
73 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
75 # In this mode, it is required to pass an explicit id.
76 with self
.assertRaises(ValueError):
77 tc
.create_stream_class()
80 def _create_trace_class_with_some_stream_classes():
82 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
84 tc
= run_in_component_init(f
)
85 sc1
= tc
.create_stream_class(id=12)
86 sc2
= tc
.create_stream_class(id=54)
87 sc3
= tc
.create_stream_class(id=2018)
88 return tc
, sc1
, sc2
, sc3
90 def test_getitem(self
):
91 tc
, _
, _
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
92 self
.assertEqual(tc
[2018].addr
, sc3
.addr
)
94 def test_getitem_wrong_key_type(self
):
95 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
96 with self
.assertRaises(TypeError):
99 def test_getitem_wrong_key(self
):
100 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
101 with self
.assertRaises(KeyError):
105 tc
= get_default_trace_class()
106 self
.assertEqual(len(tc
), 0)
107 tc
.create_stream_class()
108 self
.assertEqual(len(tc
), 1)
111 tc
, sc1
, sc2
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
113 for sc_id
, stream_class
in tc
.items():
114 self
.assertIsInstance(stream_class
, bt2
.stream_class
._StreamClass
)
117 self
.assertEqual(stream_class
.addr
, sc1
.addr
)
119 self
.assertEqual(stream_class
.addr
, sc2
.addr
)
121 self
.assertEqual(stream_class
.addr
, sc3
.addr
)
123 def test_destruction_listener(self
):
124 def on_trace_class_destruction(trace_class
):
125 nonlocal trace_class_destroyed
126 trace_class_destroyed
= True
128 trace_class_destroyed
= False
130 trace_class
= get_default_trace_class()
131 trace_class
.add_destruction_listener(on_trace_class_destruction
)
133 self
.assertFalse(trace_class_destroyed
)
137 self
.assertTrue(trace_class_destroyed
)