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.
20 from utils
import run_in_component_init
, get_default_trace_class
23 class TraceClassTestCase(unittest
.TestCase
):
24 def test_create_default(self
):
26 return comp_self
._create
_trace
_class
()
28 tc
= run_in_component_init(f
)
30 self
.assertEqual(len(tc
), 0)
31 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
33 def test_automatic_stream_class_id(self
):
35 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
37 tc
= run_in_component_init(f
)
38 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
40 # This should not throw.
41 sc1
= tc
.create_stream_class()
42 sc2
= tc
.create_stream_class()
44 self
.assertNotEqual(sc1
.id, sc2
.id)
46 def test_automatic_stream_class_id_raises(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 with self
.assertRaises(ValueError):
54 sc1
= tc
.create_stream_class(23)
56 def test_no_assigns_automatic_stream_class_id(self
):
58 return comp_self
._create
_trace
_class
(
59 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
(
71 assigns_automatic_stream_class_id
=False
74 tc
= run_in_component_init(f
)
75 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
77 # In this mode, it is required to pass an explicit id.
78 with self
.assertRaises(ValueError):
79 tc
.create_stream_class()
82 def _create_trace_class_with_some_stream_classes():
84 return comp_self
._create
_trace
_class
(
85 assigns_automatic_stream_class_id
=False
88 tc
= run_in_component_init(f
)
89 sc1
= tc
.create_stream_class(id=12)
90 sc2
= tc
.create_stream_class(id=54)
91 sc3
= tc
.create_stream_class(id=2018)
92 return tc
, sc1
, sc2
, sc3
94 def test_getitem(self
):
95 tc
, _
, _
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
96 self
.assertEqual(tc
[2018].addr
, sc3
.addr
)
98 def test_getitem_wrong_key_type(self
):
99 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
100 with self
.assertRaises(TypeError):
103 def test_getitem_wrong_key(self
):
104 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
105 with self
.assertRaises(KeyError):
109 tc
= get_default_trace_class()
110 self
.assertEqual(len(tc
), 0)
111 tc
.create_stream_class()
112 self
.assertEqual(len(tc
), 1)
115 tc
, sc1
, sc2
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
117 for sc_id
, stream_class
in tc
.items():
119 self
.assertEqual(stream_class
.addr
, sc1
.addr
)
121 self
.assertEqual(stream_class
.addr
, sc2
.addr
)
123 self
.assertEqual(stream_class
.addr
, sc3
.addr
)
125 def test_destruction_listener(self
):
126 def on_trace_class_destruction(trace_class
):
127 nonlocal trace_class_destroyed
128 trace_class_destroyed
= True
130 trace_class_destroyed
= False
132 trace_class
= get_default_trace_class()
133 trace_class
.add_destruction_listener(on_trace_class_destruction
)
135 self
.assertFalse(trace_class_destroyed
)
139 self
.assertTrue(trace_class_destroyed
)