| 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 | |
| 19 | import uuid |
| 20 | import unittest |
| 21 | import utils |
| 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 |
| 27 | |
| 28 | |
| 29 | class TraceTestCase(unittest.TestCase): |
| 30 | def setUp(self): |
| 31 | self._tc = get_default_trace_class() |
| 32 | |
| 33 | def test_create_default(self): |
| 34 | trace = self._tc() |
| 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) |
| 39 | |
| 40 | def test_create_invalid_name(self): |
| 41 | with self.assertRaises(TypeError): |
| 42 | self._tc(name=17) |
| 43 | |
| 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) |
| 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 | |
| 57 | def test_attr_trace_class(self): |
| 58 | trace = self._tc() |
| 59 | self.assertEqual(trace.cls.addr, self._tc.addr) |
| 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) |
| 65 | |
| 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): |
| 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) |
| 80 | |
| 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 | |
| 103 | def test_env_get_non_existent(self): |
| 104 | trace = self._tc(environment={'hello': 'you', 'foo': -5}) |
| 105 | |
| 106 | with self.assertRaises(KeyError): |
| 107 | trace.environment['lel'] |
| 108 | |
| 109 | def test_len(self): |
| 110 | trace = self._tc() |
| 111 | sc = self._tc.create_stream_class() |
| 112 | self.assertEqual(len(trace), 0) |
| 113 | |
| 114 | trace.create_stream(sc) |
| 115 | self.assertEqual(len(trace), 1) |
| 116 | |
| 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) |
| 123 | |
| 124 | return trace |
| 125 | |
| 126 | def test_iter(self): |
| 127 | trace = self._create_trace_with_some_streams() |
| 128 | stream_ids = set(trace) |
| 129 | self.assertEqual(stream_ids, {12, 15, 17}) |
| 130 | |
| 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) |
| 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) |
| 139 | |
| 140 | def test_getitem_invalid_key(self): |
| 141 | trace = self._create_trace_with_some_streams() |
| 142 | with self.assertRaises(KeyError): |
| 143 | trace[18] |
| 144 | |
| 145 | def test_destruction_listener(self): |
| 146 | def on_trace_class_destruction(trace_class): |
| 147 | nonlocal trace_class_destroyed |
| 148 | trace_class_destroyed = True |
| 149 | |
| 150 | def on_trace_destruction(trace): |
| 151 | nonlocal trace_destroyed |
| 152 | trace_destroyed = True |
| 153 | |
| 154 | trace_destroyed = False |
| 155 | trace_class_destroyed = False |
| 156 | |
| 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) |
| 161 | |
| 162 | trace_class.add_destruction_listener(on_trace_class_destruction) |
| 163 | trace.add_destruction_listener(on_trace_destruction) |
| 164 | |
| 165 | self.assertFalse(trace_class_destroyed) |
| 166 | self.assertFalse(trace_destroyed) |
| 167 | |
| 168 | del trace |
| 169 | |
| 170 | self.assertFalse(trace_class_destroyed) |
| 171 | self.assertFalse(trace_destroyed) |
| 172 | |
| 173 | del stream |
| 174 | |
| 175 | self.assertFalse(trace_class_destroyed) |
| 176 | self.assertTrue(trace_destroyed) |
| 177 | |
| 178 | del trace_class |
| 179 | |
| 180 | self.assertFalse(trace_class_destroyed) |
| 181 | self.assertTrue(trace_destroyed) |
| 182 | |
| 183 | del stream_class |
| 184 | |
| 185 | self.assertTrue(trace_class_destroyed) |
| 186 | self.assertTrue(trace_destroyed) |