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 assertRaisesInComponentInit(self
, expected_exc_type
, user_code
):
28 except Exception as exc
:
31 exc_type
= run_in_component_init(f
)
32 self
.assertIsNotNone(exc_type
)
33 self
.assertEqual(exc_type
, expected_exc_type
)
35 def test_create_default(self
):
37 return comp_self
._create
_trace
_class
()
39 tc
= run_in_component_init(f
)
41 self
.assertEqual(len(tc
), 0)
42 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
43 self
.assertEqual(len(tc
.user_attributes
), 0)
45 def test_create_user_attributes(self
):
47 return comp_self
._create
_trace
_class
(user_attributes
={'salut': 23})
49 tc
= run_in_component_init(f
)
50 self
.assertEqual(tc
.user_attributes
, {'salut': 23})
52 def test_create_invalid_user_attributes(self
):
54 return comp_self
._create
_trace
_class
(user_attributes
=object())
56 self
.assertRaisesInComponentInit(TypeError, f
)
58 def test_create_invalid_user_attributes_value_type(self
):
60 return comp_self
._create
_trace
_class
(user_attributes
=23)
62 self
.assertRaisesInComponentInit(TypeError, f
)
64 def test_automatic_stream_class_id(self
):
66 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
68 tc
= run_in_component_init(f
)
69 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
71 # This should not throw.
72 sc1
= tc
.create_stream_class()
73 sc2
= tc
.create_stream_class()
75 self
.assertNotEqual(sc1
.id, sc2
.id)
77 def test_automatic_stream_class_id_raises(self
):
79 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
81 tc
= run_in_component_init(f
)
82 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
84 with self
.assertRaises(ValueError):
85 sc1
= tc
.create_stream_class(23)
87 def test_no_assigns_automatic_stream_class_id(self
):
89 return comp_self
._create
_trace
_class
(
90 assigns_automatic_stream_class_id
=False
93 tc
= run_in_component_init(f
)
94 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
96 sc
= tc
.create_stream_class(id=28)
97 self
.assertEqual(sc
.id, 28)
99 def test_no_assigns_automatic_stream_class_id_raises(self
):
101 return comp_self
._create
_trace
_class
(
102 assigns_automatic_stream_class_id
=False
105 tc
= run_in_component_init(f
)
106 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
108 # In this mode, it is required to pass an explicit id.
109 with self
.assertRaises(ValueError):
110 tc
.create_stream_class()
113 def _create_trace_class_with_some_stream_classes():
115 return comp_self
._create
_trace
_class
(
116 assigns_automatic_stream_class_id
=False
119 tc
= run_in_component_init(f
)
120 sc1
= tc
.create_stream_class(id=12)
121 sc2
= tc
.create_stream_class(id=54)
122 sc3
= tc
.create_stream_class(id=2018)
123 return tc
, sc1
, sc2
, sc3
125 def test_getitem(self
):
126 tc
, _
, _
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
127 self
.assertEqual(tc
[2018].addr
, sc3
.addr
)
129 def test_getitem_wrong_key_type(self
):
130 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
131 with self
.assertRaises(TypeError):
134 def test_getitem_wrong_key(self
):
135 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
136 with self
.assertRaises(KeyError):
140 tc
= get_default_trace_class()
141 self
.assertEqual(len(tc
), 0)
142 tc
.create_stream_class()
143 self
.assertEqual(len(tc
), 1)
146 tc
, sc1
, sc2
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
148 for sc_id
, stream_class
in tc
.items():
150 self
.assertEqual(stream_class
.addr
, sc1
.addr
)
152 self
.assertEqual(stream_class
.addr
, sc2
.addr
)
154 self
.assertEqual(stream_class
.addr
, sc3
.addr
)
156 def test_destruction_listener(self
):
157 def on_trace_class_destruction(trace_class
):
158 nonlocal trace_class_destroyed
159 trace_class_destroyed
= True
161 trace_class_destroyed
= False
163 trace_class
= get_default_trace_class()
164 trace_class
.add_destruction_listener(on_trace_class_destruction
)
166 self
.assertFalse(trace_class_destroyed
)
170 self
.assertTrue(trace_class_destroyed
)