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 get_default_trace_class
23 from bt2
import trace_class
as bt2_trace_class
24 from bt2
import value
as bt2_value
25 from bt2
import trace
as bt2_trace
26 from bt2
import stream
as bt2_stream
29 class TraceTestCase(unittest
.TestCase
):
31 self
._tc
= get_default_trace_class()
33 def test_create_default(self
):
35 self
.assertIsNone(trace
.name
)
36 self
.assertIsNone(trace
.uuid
)
37 self
.assertEqual(len(trace
.environment
), 0)
38 self
.assertEqual(len(trace
.user_attributes
), 0)
40 def test_create_invalid_name(self
):
41 with self
.assertRaises(TypeError):
44 def test_create_user_attributes(self
):
45 trace
= self
._tc
(user_attributes
={'salut': 23})
46 self
.assertEqual(trace
.user_attributes
, {'salut': 23})
47 self
.assertIs(type(trace
.user_attributes
), bt2_value
.MapValue
)
49 def test_create_invalid_user_attributes(self
):
50 with self
.assertRaises(TypeError):
51 self
._tc
(user_attributes
=object())
53 def test_create_invalid_user_attributes_value_type(self
):
54 with self
.assertRaises(TypeError):
55 self
._tc
(user_attributes
=23)
57 def test_attr_trace_class(self
):
59 self
.assertEqual(trace
.cls
.addr
, self
._tc
.addr
)
60 self
.assertIs(type(trace
.cls
), bt2_trace_class
._TraceClass
)
62 def test_const_attr_trace_class(self
):
63 trace
= utils
.get_const_stream_beginning_message().stream
.trace
64 self
.assertIs(type(trace
.cls
), bt2_trace_class
._TraceClassConst
)
66 def test_attr_name(self
):
67 trace
= self
._tc
(name
='mein trace')
68 self
.assertEqual(trace
.name
, 'mein trace')
70 def test_attr_uuid(self
):
71 trace
= self
._tc
(uuid
=uuid
.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
72 self
.assertEqual(trace
.uuid
, uuid
.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
74 def test_env_get(self
):
75 trace
= self
._tc
(environment
={'hello': 'you', 'foo': -5})
76 self
.assertIs(type(trace
.environment
), bt2_trace
._TraceEnvironment
)
77 self
.assertIs(type(trace
.environment
['foo']), bt2_value
.SignedIntegerValue
)
78 self
.assertEqual(trace
.environment
['hello'], 'you')
79 self
.assertEqual(trace
.environment
['foo'], -5)
81 def test_env_iter(self
):
82 trace
= self
._tc
(environment
={'hello': 'you', 'foo': -5})
83 values
= set(trace
.environment
)
84 self
.assertEqual(values
, {'hello', 'foo'})
86 def test_const_env_get(self
):
87 trace
= utils
.get_const_stream_beginning_message().stream
.trace
88 self
.assertIs(type(trace
.environment
), bt2_trace
._TraceEnvironmentConst
)
90 type(trace
.environment
['patate']), bt2_value
._SignedIntegerValueConst
93 def test_env_iter(self
):
94 trace
= utils
.get_const_stream_beginning_message().stream
.trace
95 values
= set(trace
.environment
)
96 self
.assertEqual(values
, {'patate'})
98 def test_const_env_set(self
):
99 trace
= utils
.get_const_stream_beginning_message().stream
.trace
100 with self
.assertRaises(TypeError):
101 trace
.environment
['patate'] = 33
103 def test_env_get_non_existent(self
):
104 trace
= self
._tc
(environment
={'hello': 'you', 'foo': -5})
106 with self
.assertRaises(KeyError):
107 trace
.environment
['lel']
111 sc
= self
._tc
.create_stream_class()
112 self
.assertEqual(len(trace
), 0)
114 trace
.create_stream(sc
)
115 self
.assertEqual(len(trace
), 1)
117 def _create_trace_with_some_streams(self
):
118 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=False)
120 trace
.create_stream(sc
, id=12)
121 trace
.create_stream(sc
, id=15)
122 trace
.create_stream(sc
, id=17)
127 trace
= self
._create
_trace
_with
_some
_streams
()
128 stream_ids
= set(trace
)
129 self
.assertEqual(stream_ids
, {12, 15, 17})
131 def test_getitem(self
):
132 trace
= self
._create
_trace
_with
_some
_streams
()
133 self
.assertEqual(trace
[12].id, 12)
134 self
.assertIs(type(trace
[12]), bt2_stream
._Stream
)
136 def test_const_getitem(self
):
137 trace
= utils
.get_const_stream_beginning_message().stream
.trace
138 self
.assertIs(type(trace
[0]), bt2_stream
._StreamConst
)
140 def test_getitem_invalid_key(self
):
141 trace
= self
._create
_trace
_with
_some
_streams
()
142 with self
.assertRaises(KeyError):
145 def test_destruction_listener(self
):
146 def on_trace_class_destruction(trace_class
):
147 nonlocal trace_class_destroyed
148 trace_class_destroyed
= True
150 def on_trace_destruction(trace
):
151 nonlocal trace_destroyed
152 trace_destroyed
= True
154 trace_destroyed
= False
155 trace_class_destroyed
= False
157 trace_class
= get_default_trace_class()
158 stream_class
= trace_class
.create_stream_class()
159 trace
= trace_class()
160 stream
= trace
.create_stream(stream_class
)
162 trace_class
.add_destruction_listener(on_trace_class_destruction
)
163 trace
.add_destruction_listener(on_trace_destruction
)
165 self
.assertFalse(trace_class_destroyed
)
166 self
.assertFalse(trace_destroyed
)
170 self
.assertFalse(trace_class_destroyed
)
171 self
.assertFalse(trace_destroyed
)
175 self
.assertFalse(trace_class_destroyed
)
176 self
.assertTrue(trace_destroyed
)
180 self
.assertFalse(trace_class_destroyed
)
181 self
.assertTrue(trace_destroyed
)
185 self
.assertTrue(trace_class_destroyed
)
186 self
.assertTrue(trace_destroyed
)