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 |
f377a958 | 21 | from utils import get_default_trace_class |
9cf643d1 PP |
22 | |
23 | ||
24 | class TraceTestCase(unittest.TestCase): | |
25 | def setUp(self): | |
f377a958 | 26 | self._tc = get_default_trace_class() |
9cf643d1 PP |
27 | |
28 | def test_create_default(self): | |
f377a958 | 29 | trace = self._tc() |
cd03c43c PP |
30 | self.assertIsNone(trace.name) |
31 | self.assertIsNone(trace.uuid) | |
32 | self.assertEqual(len(trace.env), 0) | |
b2df5780 | 33 | self.assertEqual(len(trace.user_attributes), 0) |
9cf643d1 | 34 | |
f377a958 | 35 | def test_create_invalid_name(self): |
9cf643d1 | 36 | with self.assertRaises(TypeError): |
f377a958 | 37 | self._tc(name=17) |
9cf643d1 | 38 | |
b2df5780 PP |
39 | def test_create_user_attributes(self): |
40 | trace = self._tc(user_attributes={'salut': 23}) | |
41 | self.assertEqual(trace.user_attributes, {'salut': 23}) | |
42 | ||
43 | def test_create_invalid_user_attributes(self): | |
44 | with self.assertRaises(TypeError): | |
45 | self._tc(user_attributes=object()) | |
46 | ||
47 | def test_create_invalid_user_attributes_value_type(self): | |
48 | with self.assertRaises(TypeError): | |
49 | self._tc(user_attributes=23) | |
50 | ||
a7f0580d | 51 | def test_attr_trace_class(self): |
cd03c43c | 52 | trace = self._tc() |
a7f0580d PP |
53 | self.assertEqual(trace.cls.addr, self._tc.addr) |
54 | ||
cd03c43c PP |
55 | def test_attr_name(self): |
56 | trace = self._tc(name='mein trace') | |
57 | self.assertEqual(trace.name, 'mein trace') | |
58 | ||
59 | def test_attr_uuid(self): | |
60 | trace = self._tc(uuid=uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b')) | |
61 | self.assertEqual(trace.uuid, uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b')) | |
62 | ||
63 | def test_env_get(self): | |
64 | trace = self._tc(env={'hello': 'you', 'foo': -5}) | |
65 | self.assertEqual(trace.env['hello'], 'you') | |
66 | self.assertEqual(trace.env['foo'], -5) | |
67 | ||
68 | def test_env_get_non_existent(self): | |
69 | trace = self._tc(env={'hello': 'you', 'foo': -5}) | |
70 | ||
71 | with self.assertRaises(KeyError): | |
72 | trace.env['lel'] | |
73 | ||
f377a958 SM |
74 | def test_len(self): |
75 | trace = self._tc() | |
76 | sc = self._tc.create_stream_class() | |
77 | self.assertEqual(len(trace), 0) | |
9cf643d1 | 78 | |
f377a958 SM |
79 | trace.create_stream(sc) |
80 | self.assertEqual(len(trace), 1) | |
9cf643d1 | 81 | |
f377a958 SM |
82 | def _create_trace_with_some_streams(self): |
83 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) | |
84 | trace = self._tc() | |
85 | trace.create_stream(sc, id=12) | |
86 | trace.create_stream(sc, id=15) | |
87 | trace.create_stream(sc, id=17) | |
9cf643d1 | 88 | |
f377a958 | 89 | return trace |
9cf643d1 PP |
90 | |
91 | def test_iter(self): | |
f377a958 SM |
92 | trace = self._create_trace_with_some_streams() |
93 | stream_ids = set(trace) | |
94 | self.assertEqual(stream_ids, {12, 15, 17}) | |
9cf643d1 | 95 | |
f377a958 SM |
96 | def test_getitem(self): |
97 | trace = self._create_trace_with_some_streams() | |
9cf643d1 | 98 | |
f377a958 | 99 | self.assertEqual(trace[12].id, 12) |
9cf643d1 | 100 | |
f377a958 SM |
101 | def test_getitem_invalid_key(self): |
102 | trace = self._create_trace_with_some_streams() | |
9cf643d1 | 103 | with self.assertRaises(KeyError): |
f377a958 | 104 | trace[18] |
9cf643d1 | 105 | |
f377a958 SM |
106 | def test_destruction_listener(self): |
107 | def on_trace_class_destruction(trace_class): | |
108 | nonlocal trace_class_destroyed | |
109 | trace_class_destroyed = True | |
f6a5e476 | 110 | |
f377a958 SM |
111 | def on_trace_destruction(trace): |
112 | nonlocal trace_destroyed | |
113 | trace_destroyed = True | |
f6a5e476 | 114 | |
f377a958 SM |
115 | trace_destroyed = False |
116 | trace_class_destroyed = False | |
f6a5e476 | 117 | |
f377a958 SM |
118 | trace_class = get_default_trace_class() |
119 | stream_class = trace_class.create_stream_class() | |
120 | trace = trace_class() | |
121 | stream = trace.create_stream(stream_class) | |
f6a5e476 | 122 | |
f377a958 SM |
123 | trace_class.add_destruction_listener(on_trace_class_destruction) |
124 | trace.add_destruction_listener(on_trace_destruction) | |
f6a5e476 | 125 | |
f377a958 SM |
126 | self.assertFalse(trace_class_destroyed) |
127 | self.assertFalse(trace_destroyed) | |
9cf643d1 | 128 | |
f377a958 | 129 | del trace |
9cf643d1 | 130 | |
f377a958 SM |
131 | self.assertFalse(trace_class_destroyed) |
132 | self.assertFalse(trace_destroyed) | |
9cf643d1 | 133 | |
f377a958 | 134 | del stream |
9cf643d1 | 135 | |
f377a958 SM |
136 | self.assertFalse(trace_class_destroyed) |
137 | self.assertTrue(trace_destroyed) | |
9cf643d1 | 138 | |
f377a958 | 139 | del trace_class |
9cf643d1 | 140 | |
f377a958 SM |
141 | self.assertFalse(trace_class_destroyed) |
142 | self.assertTrue(trace_destroyed) | |
9cf643d1 | 143 | |
f377a958 | 144 | del stream_class |
9cf643d1 | 145 | |
f377a958 SM |
146 | self.assertTrue(trace_class_destroyed) |
147 | self.assertTrue(trace_destroyed) |