File: test_trace.py

package info (click to toggle)
babeltrace2 2.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,660 kB
  • sloc: cpp: 106,162; ansic: 78,276; python: 27,115; sh: 9,053; makefile: 1,807; xml: 46
file content (245 lines) | stat: -rw-r--r-- 8,512 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright (C) 2019 EfficiOS Inc.
#

import uuid
import unittest

import utils
from bt2 import trace as bt2_trace
from bt2 import utils as bt2_utils
from bt2 import value as bt2_value
from bt2 import stream as bt2_stream
from bt2 import trace_class as bt2_trace_class
from utils import get_default_trace_class, get_const_stream_beginning_message


class TraceTestCase(unittest.TestCase):
    def setUp(self):
        self._tc = get_default_trace_class()

    def test_create_default(self):
        trace = self._tc()
        self.assertIsNone(trace.name)
        self.assertIsNone(trace.uuid)
        self.assertEqual(len(trace.environment), 0)
        self.assertEqual(len(trace.user_attributes), 0)

    def test_create_invalid_name(self):
        with self.assertRaises(TypeError):
            self._tc(name=17)

    def test_create_user_attributes(self):
        trace = self._tc(user_attributes={"salut": 23})
        self.assertEqual(trace.user_attributes, {"salut": 23})
        self.assertIs(type(trace.user_attributes), bt2_value.MapValue)

    def test_const_user_attributes(self):
        trace = get_const_stream_beginning_message().stream.trace
        self.assertEqual(trace.user_attributes, {"a-trace-attribute": 1})
        self.assertIs(type(trace.user_attributes), bt2_value._MapValueConst)

    def test_create_invalid_user_attributes(self):
        with self.assertRaises(TypeError):
            self._tc(user_attributes=object())

    def test_create_invalid_user_attributes_value_type(self):
        with self.assertRaises(TypeError):
            self._tc(user_attributes=23)

    def test_attr_trace_class(self):
        trace = self._tc()
        self.assertEqual(trace.cls.addr, self._tc.addr)
        self.assertIs(type(trace.cls), bt2_trace_class._TraceClass)

    def test_const_attr_trace_class(self):
        trace = utils.get_const_stream_beginning_message().stream.trace
        self.assertIs(type(trace.cls), bt2_trace_class._TraceClassConst)

    def test_attr_name(self):
        trace = self._tc(name="mein trace")
        self.assertEqual(trace.name, "mein trace")

    def test_attr_uuid(self):
        trace = self._tc(uuid=uuid.UUID("da7d6b6f-3108-4706-89bd-ab554732611b"))
        self.assertEqual(trace.uuid, uuid.UUID("da7d6b6f-3108-4706-89bd-ab554732611b"))

    def test_env_get(self):
        trace = self._tc(environment={"hello": "you", "foo": -5})
        self.assertIs(type(trace.environment), bt2_trace._TraceEnvironment)
        self.assertIs(type(trace.environment["foo"]), bt2_value.SignedIntegerValue)
        self.assertEqual(trace.environment["hello"], "you")
        self.assertEqual(trace.environment["foo"], -5)

    def test_env_iter(self):
        trace = self._tc(environment={"hello": "you", "foo": -5})
        values = set(trace.environment)
        self.assertEqual(values, {"hello", "foo"})

    def test_const_env_get(self):
        trace = utils.get_const_stream_beginning_message().stream.trace
        self.assertIs(type(trace.environment), bt2_trace._TraceEnvironmentConst)
        self.assertIs(
            type(trace.environment["patate"]), bt2_value._SignedIntegerValueConst
        )

    def test_const_env_iter(self):
        trace = utils.get_const_stream_beginning_message().stream.trace
        values = set(trace.environment)
        self.assertEqual(values, {"patate"})

    def test_const_env_set(self):
        trace = utils.get_const_stream_beginning_message().stream.trace
        with self.assertRaises(TypeError):
            trace.environment["patate"] = 33

    def test_env_get_non_existent(self):
        trace = self._tc(environment={"hello": "you", "foo": -5})

        with self.assertRaises(KeyError):
            trace.environment["lel"]

    def test_len(self):
        trace = self._tc()
        sc = self._tc.create_stream_class()
        self.assertEqual(len(trace), 0)

        trace.create_stream(sc)
        self.assertEqual(len(trace), 1)

    def _create_trace_with_some_streams(self):
        sc = self._tc.create_stream_class(assigns_automatic_stream_id=False)
        trace = self._tc()
        trace.create_stream(sc, id=12)
        trace.create_stream(sc, id=15)
        trace.create_stream(sc, id=17)

        return trace

    def test_iter(self):
        trace = self._create_trace_with_some_streams()
        stream_ids = set(trace)
        self.assertEqual(stream_ids, {12, 15, 17})

    def test_getitem(self):
        trace = self._create_trace_with_some_streams()
        self.assertEqual(trace[12].id, 12)
        self.assertIs(type(trace[12]), bt2_stream._Stream)

    def test_const_getitem(self):
        trace = utils.get_const_stream_beginning_message().stream.trace
        self.assertIs(type(trace[0]), bt2_stream._StreamConst)

    def test_getitem_invalid_key(self):
        trace = self._create_trace_with_some_streams()
        with self.assertRaises(KeyError):
            trace[18]

    def test_destruction_listener(self):
        def on_trace_class_destruction(trace_class):
            nonlocal num_trace_class_destroyed_calls
            num_trace_class_destroyed_calls += 1

        def on_trace_destruction(trace):
            nonlocal type_of_passed_trace
            type_of_passed_trace = type(trace)

            nonlocal num_trace_destroyed_calls
            num_trace_destroyed_calls += 1

        num_trace_class_destroyed_calls = 0
        num_trace_destroyed_calls = 0
        type_of_passed_trace = None

        trace_class = get_default_trace_class()
        stream_class = trace_class.create_stream_class()
        trace = trace_class()
        stream = trace.create_stream(stream_class)

        trace_class.add_destruction_listener(on_trace_class_destruction)
        td_handle1 = trace.add_destruction_listener(on_trace_destruction)
        td_handle2 = trace.add_destruction_listener(on_trace_destruction)

        self.assertIs(type(td_handle1), bt2_utils._ListenerHandle)

        trace.remove_destruction_listener(td_handle2)

        self.assertEqual(num_trace_class_destroyed_calls, 0)
        self.assertEqual(num_trace_destroyed_calls, 0)

        del trace

        self.assertEqual(num_trace_class_destroyed_calls, 0)
        self.assertEqual(num_trace_destroyed_calls, 0)

        del stream

        self.assertEqual(num_trace_class_destroyed_calls, 0)
        self.assertEqual(num_trace_destroyed_calls, 1)
        self.assertIs(type_of_passed_trace, bt2_trace._TraceConst)

        del trace_class

        self.assertEqual(num_trace_class_destroyed_calls, 0)
        self.assertEqual(num_trace_destroyed_calls, 1)

        del stream_class

        self.assertEqual(num_trace_class_destroyed_calls, 1)
        self.assertEqual(num_trace_destroyed_calls, 1)

    def test_remove_destruction_listener_wrong_type(self):
        trace_class = get_default_trace_class()
        trace = trace_class()

        with self.assertRaisesRegex(
            TypeError, r"'int' is not a '<class 'bt2.utils._ListenerHandle'>' object"
        ):
            trace.remove_destruction_listener(123)

    def test_remove_destruction_listener_wrong_object(self):
        def on_trace_destruction(trace):
            pass

        trace_class_1 = get_default_trace_class()
        trace1 = trace_class_1()
        trace_class_2 = get_default_trace_class()
        trace2 = trace_class_2()

        handle1 = trace1.add_destruction_listener(on_trace_destruction)

        with self.assertRaisesRegex(
            ValueError,
            r"This trace destruction listener does not match the trace object\.",
        ):
            trace2.remove_destruction_listener(handle1)

    def test_remove_destruction_listener_twice(self):
        def on_trace_destruction(trace_class):
            pass

        trace_class = get_default_trace_class()
        trace = trace_class()
        handle = trace.add_destruction_listener(on_trace_destruction)

        trace.remove_destruction_listener(handle)

        with self.assertRaisesRegex(
            ValueError, r"This trace destruction listener was already removed\."
        ):
            trace.remove_destruction_listener(handle)

    def test_raise_in_destruction_listener(self):
        def on_trace_destruction(trace):
            raise ValueError("it hurts")

        trace_class = get_default_trace_class()
        trace = trace_class()
        trace.add_destruction_listener(on_trace_destruction)

        del trace


if __name__ == "__main__":
    unittest.main()