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
):
25 def test_create_default(self
):
27 return comp_self
._create
_trace
_class
()
29 tc
= run_in_component_init(f
)
31 self
.assertEqual(len(tc
), 0)
32 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
34 def test_automatic_stream_class_id(self
):
36 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
38 tc
= run_in_component_init(f
)
39 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
41 # This should not throw.
42 sc1
= tc
.create_stream_class()
43 sc2
= tc
.create_stream_class()
45 self
.assertNotEqual(sc1
.id, sc2
.id)
47 def test_automatic_stream_class_id_raises(self
):
49 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
51 tc
= run_in_component_init(f
)
52 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
54 with self
.assertRaises(ValueError):
55 sc1
= tc
.create_stream_class(23)
57 def test_no_assigns_automatic_stream_class_id(self
):
59 return comp_self
._create
_trace
_class
(
60 assigns_automatic_stream_class_id
=False
63 tc
= run_in_component_init(f
)
64 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
66 sc
= tc
.create_stream_class(id=28)
67 self
.assertEqual(sc
.id, 28)
69 def test_no_assigns_automatic_stream_class_id_raises(self
):
71 return comp_self
._create
_trace
_class
(
72 assigns_automatic_stream_class_id
=False
75 tc
= run_in_component_init(f
)
76 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
78 # In this mode, it is required to pass an explicit id.
79 with self
.assertRaises(ValueError):
80 tc
.create_stream_class()
83 def _create_trace_class_with_some_stream_classes():
85 return comp_self
._create
_trace
_class
(
86 assigns_automatic_stream_class_id
=False
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
95 def test_getitem(self
):
96 tc
, _
, _
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
97 self
.assertEqual(tc
[2018].addr
, sc3
.addr
)
99 def test_getitem_wrong_key_type(self
):
100 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
101 with self
.assertRaises(TypeError):
104 def test_getitem_wrong_key(self
):
105 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
106 with self
.assertRaises(KeyError):
110 tc
= get_default_trace_class()
111 self
.assertEqual(len(tc
), 0)
112 tc
.create_stream_class()
113 self
.assertEqual(len(tc
), 1)
116 tc
, sc1
, sc2
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
118 for sc_id
, stream_class
in tc
.items():
120 self
.assertEqual(stream_class
.addr
, sc1
.addr
)
122 self
.assertEqual(stream_class
.addr
, sc2
.addr
)
124 self
.assertEqual(stream_class
.addr
, sc3
.addr
)
126 def test_destruction_listener(self
):
127 def on_trace_class_destruction(trace_class
):
128 nonlocal trace_class_destroyed
129 trace_class_destroyed
= True
131 trace_class_destroyed
= False
133 trace_class
= get_default_trace_class()
134 trace_class
.add_destruction_listener(on_trace_class_destruction
)
136 self
.assertFalse(trace_class_destroyed
)
140 self
.assertTrue(trace_class_destroyed
)