Commit | Line | Data |
---|---|---|
32d2d479 MJ |
1 | # |
2 | # Copyright (C) 2019 EfficiOS Inc. | |
3 | # | |
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 | |
7 | # of the License. | |
8 | # | |
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. | |
13 | # | |
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. | |
17 | # | |
18 | ||
cd03c43c | 19 | import uuid |
9cf643d1 | 20 | import unittest |
9cbe0c59 | 21 | import utils |
f377a958 | 22 | from utils import get_default_trace_class |
9cbe0c59 FD |
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 | |
9cf643d1 PP |
27 | |
28 | ||
29 | class TraceTestCase(unittest.TestCase): | |
30 | def setUp(self): | |
f377a958 | 31 | self._tc = get_default_trace_class() |
9cf643d1 PP |
32 | |
33 | def test_create_default(self): | |
f377a958 | 34 | trace = self._tc() |
cd03c43c PP |
35 | self.assertIsNone(trace.name) |
36 | self.assertIsNone(trace.uuid) | |
3f1a6e26 | 37 | self.assertEqual(len(trace.environment), 0) |
b2df5780 | 38 | self.assertEqual(len(trace.user_attributes), 0) |
9cf643d1 | 39 | |
f377a958 | 40 | def test_create_invalid_name(self): |
9cf643d1 | 41 | with self.assertRaises(TypeError): |
f377a958 | 42 | self._tc(name=17) |
9cf643d1 | 43 | |
b2df5780 PP |
44 | def test_create_user_attributes(self): |
45 | trace = self._tc(user_attributes={'salut': 23}) | |
46 | self.assertEqual(trace.user_attributes, {'salut': 23}) | |
9cbe0c59 | 47 | self.assertIs(type(trace.user_attributes), bt2_value.MapValue) |
b2df5780 PP |
48 | |
49 | def test_create_invalid_user_attributes(self): | |
50 | with self.assertRaises(TypeError): | |
51 | self._tc(user_attributes=object()) | |
52 | ||
53 | def test_create_invalid_user_attributes_value_type(self): | |
54 | with self.assertRaises(TypeError): | |
55 | self._tc(user_attributes=23) | |
56 | ||
a7f0580d | 57 | def test_attr_trace_class(self): |
cd03c43c | 58 | trace = self._tc() |
a7f0580d | 59 | self.assertEqual(trace.cls.addr, self._tc.addr) |
9cbe0c59 FD |
60 | self.assertIs(type(trace.cls), bt2_trace_class._TraceClass) |
61 | ||
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) | |
a7f0580d | 65 | |
cd03c43c PP |
66 | def test_attr_name(self): |
67 | trace = self._tc(name='mein trace') | |
68 | self.assertEqual(trace.name, 'mein trace') | |
69 | ||
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')) | |
73 | ||
74 | def test_env_get(self): | |
3f1a6e26 | 75 | trace = self._tc(environment={'hello': 'you', 'foo': -5}) |
9cbe0c59 FD |
76 | self.assertIs(type(trace.environment), bt2_trace._TraceEnvironment) |
77 | self.assertIs(type(trace.environment['foo']), bt2_value.SignedIntegerValue) | |
3f1a6e26 PP |
78 | self.assertEqual(trace.environment['hello'], 'you') |
79 | self.assertEqual(trace.environment['foo'], -5) | |
cd03c43c | 80 | |
9cbe0c59 FD |
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'}) | |
85 | ||
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) | |
89 | self.assertIs( | |
90 | type(trace.environment['patate']), bt2_value._SignedIntegerValueConst | |
91 | ) | |
92 | ||
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'}) | |
97 | ||
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 | |
102 | ||
cd03c43c | 103 | def test_env_get_non_existent(self): |
3f1a6e26 | 104 | trace = self._tc(environment={'hello': 'you', 'foo': -5}) |
cd03c43c PP |
105 | |
106 | with self.assertRaises(KeyError): | |
3f1a6e26 | 107 | trace.environment['lel'] |
cd03c43c | 108 | |
f377a958 SM |
109 | def test_len(self): |
110 | trace = self._tc() | |
111 | sc = self._tc.create_stream_class() | |
112 | self.assertEqual(len(trace), 0) | |
9cf643d1 | 113 | |
f377a958 SM |
114 | trace.create_stream(sc) |
115 | self.assertEqual(len(trace), 1) | |
9cf643d1 | 116 | |
f377a958 SM |
117 | def _create_trace_with_some_streams(self): |
118 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) | |
119 | trace = self._tc() | |
120 | trace.create_stream(sc, id=12) | |
121 | trace.create_stream(sc, id=15) | |
122 | trace.create_stream(sc, id=17) | |
9cf643d1 | 123 | |
f377a958 | 124 | return trace |
9cf643d1 PP |
125 | |
126 | def test_iter(self): | |
f377a958 SM |
127 | trace = self._create_trace_with_some_streams() |
128 | stream_ids = set(trace) | |
129 | self.assertEqual(stream_ids, {12, 15, 17}) | |
9cf643d1 | 130 | |
f377a958 SM |
131 | def test_getitem(self): |
132 | trace = self._create_trace_with_some_streams() | |
f377a958 | 133 | self.assertEqual(trace[12].id, 12) |
9cbe0c59 FD |
134 | self.assertIs(type(trace[12]), bt2_stream._Stream) |
135 | ||
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) | |
9cf643d1 | 139 | |
f377a958 SM |
140 | def test_getitem_invalid_key(self): |
141 | trace = self._create_trace_with_some_streams() | |
9cf643d1 | 142 | with self.assertRaises(KeyError): |
f377a958 | 143 | trace[18] |
9cf643d1 | 144 | |
f377a958 SM |
145 | def test_destruction_listener(self): |
146 | def on_trace_class_destruction(trace_class): | |
147 | nonlocal trace_class_destroyed | |
148 | trace_class_destroyed = True | |
f6a5e476 | 149 | |
f377a958 SM |
150 | def on_trace_destruction(trace): |
151 | nonlocal trace_destroyed | |
152 | trace_destroyed = True | |
f6a5e476 | 153 | |
f377a958 SM |
154 | trace_destroyed = False |
155 | trace_class_destroyed = False | |
f6a5e476 | 156 | |
f377a958 SM |
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) | |
f6a5e476 | 161 | |
f377a958 SM |
162 | trace_class.add_destruction_listener(on_trace_class_destruction) |
163 | trace.add_destruction_listener(on_trace_destruction) | |
f6a5e476 | 164 | |
f377a958 SM |
165 | self.assertFalse(trace_class_destroyed) |
166 | self.assertFalse(trace_destroyed) | |
9cf643d1 | 167 | |
f377a958 | 168 | del trace |
9cf643d1 | 169 | |
f377a958 SM |
170 | self.assertFalse(trace_class_destroyed) |
171 | self.assertFalse(trace_destroyed) | |
9cf643d1 | 172 | |
f377a958 | 173 | del stream |
9cf643d1 | 174 | |
f377a958 SM |
175 | self.assertFalse(trace_class_destroyed) |
176 | self.assertTrue(trace_destroyed) | |
9cf643d1 | 177 | |
f377a958 | 178 | del trace_class |
9cf643d1 | 179 | |
f377a958 SM |
180 | self.assertFalse(trace_class_destroyed) |
181 | self.assertTrue(trace_destroyed) | |
9cf643d1 | 182 | |
f377a958 | 183 | del stream_class |
9cf643d1 | 184 | |
f377a958 SM |
185 | self.assertTrue(trace_class_destroyed) |
186 | self.assertTrue(trace_destroyed) |