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 | ||
0bee8ea9 | 19 | import unittest |
0bee8ea9 SM |
20 | from utils import run_in_component_init, get_default_trace_class |
21 | ||
22 | ||
23 | class TraceClassTestCase(unittest.TestCase): | |
b2df5780 PP |
24 | def assertRaisesInComponentInit(self, expected_exc_type, user_code): |
25 | def f(comp_self): | |
26 | try: | |
27 | user_code(comp_self) | |
28 | except Exception as exc: | |
29 | return type(exc) | |
30 | ||
31 | exc_type = run_in_component_init(f) | |
32 | self.assertIsNotNone(exc_type) | |
33 | self.assertEqual(exc_type, expected_exc_type) | |
34 | ||
0bee8ea9 SM |
35 | def test_create_default(self): |
36 | def f(comp_self): | |
37 | return comp_self._create_trace_class() | |
38 | ||
39 | tc = run_in_component_init(f) | |
40 | ||
41 | self.assertEqual(len(tc), 0) | |
0bee8ea9 | 42 | self.assertTrue(tc.assigns_automatic_stream_class_id) |
b2df5780 PP |
43 | self.assertEqual(len(tc.user_attributes), 0) |
44 | ||
45 | def test_create_user_attributes(self): | |
46 | def f(comp_self): | |
47 | return comp_self._create_trace_class(user_attributes={'salut': 23}) | |
48 | ||
49 | tc = run_in_component_init(f) | |
50 | self.assertEqual(tc.user_attributes, {'salut': 23}) | |
51 | ||
52 | def test_create_invalid_user_attributes(self): | |
53 | def f(comp_self): | |
54 | return comp_self._create_trace_class(user_attributes=object()) | |
55 | ||
56 | self.assertRaisesInComponentInit(TypeError, f) | |
57 | ||
58 | def test_create_invalid_user_attributes_value_type(self): | |
59 | def f(comp_self): | |
60 | return comp_self._create_trace_class(user_attributes=23) | |
61 | ||
62 | self.assertRaisesInComponentInit(TypeError, f) | |
0bee8ea9 | 63 | |
0bee8ea9 SM |
64 | def test_automatic_stream_class_id(self): |
65 | def f(comp_self): | |
66 | return comp_self._create_trace_class(assigns_automatic_stream_class_id=True) | |
67 | ||
68 | tc = run_in_component_init(f) | |
69 | self.assertTrue(tc.assigns_automatic_stream_class_id) | |
70 | ||
71 | # This should not throw. | |
72 | sc1 = tc.create_stream_class() | |
73 | sc2 = tc.create_stream_class() | |
74 | ||
75 | self.assertNotEqual(sc1.id, sc2.id) | |
76 | ||
77 | def test_automatic_stream_class_id_raises(self): | |
78 | def f(comp_self): | |
79 | return comp_self._create_trace_class(assigns_automatic_stream_class_id=True) | |
80 | ||
81 | tc = run_in_component_init(f) | |
82 | self.assertTrue(tc.assigns_automatic_stream_class_id) | |
83 | ||
116779e3 | 84 | with self.assertRaises(ValueError): |
0bee8ea9 SM |
85 | sc1 = tc.create_stream_class(23) |
86 | ||
87 | def test_no_assigns_automatic_stream_class_id(self): | |
88 | def f(comp_self): | |
61d96b89 FD |
89 | return comp_self._create_trace_class( |
90 | assigns_automatic_stream_class_id=False | |
91 | ) | |
0bee8ea9 SM |
92 | |
93 | tc = run_in_component_init(f) | |
94 | self.assertFalse(tc.assigns_automatic_stream_class_id) | |
95 | ||
96 | sc = tc.create_stream_class(id=28) | |
97 | self.assertEqual(sc.id, 28) | |
98 | ||
99 | def test_no_assigns_automatic_stream_class_id_raises(self): | |
100 | def f(comp_self): | |
61d96b89 FD |
101 | return comp_self._create_trace_class( |
102 | assigns_automatic_stream_class_id=False | |
103 | ) | |
0bee8ea9 SM |
104 | |
105 | tc = run_in_component_init(f) | |
106 | self.assertFalse(tc.assigns_automatic_stream_class_id) | |
107 | ||
108 | # In this mode, it is required to pass an explicit id. | |
116779e3 | 109 | with self.assertRaises(ValueError): |
0bee8ea9 SM |
110 | tc.create_stream_class() |
111 | ||
0bee8ea9 SM |
112 | @staticmethod |
113 | def _create_trace_class_with_some_stream_classes(): | |
114 | def f(comp_self): | |
61d96b89 FD |
115 | return comp_self._create_trace_class( |
116 | assigns_automatic_stream_class_id=False | |
117 | ) | |
0bee8ea9 SM |
118 | |
119 | tc = run_in_component_init(f) | |
120 | sc1 = tc.create_stream_class(id=12) | |
121 | sc2 = tc.create_stream_class(id=54) | |
122 | sc3 = tc.create_stream_class(id=2018) | |
123 | return tc, sc1, sc2, sc3 | |
124 | ||
125 | def test_getitem(self): | |
126 | tc, _, _, sc3 = self._create_trace_class_with_some_stream_classes() | |
127 | self.assertEqual(tc[2018].addr, sc3.addr) | |
128 | ||
129 | def test_getitem_wrong_key_type(self): | |
130 | tc, _, _, _ = self._create_trace_class_with_some_stream_classes() | |
131 | with self.assertRaises(TypeError): | |
132 | tc['hello'] | |
133 | ||
134 | def test_getitem_wrong_key(self): | |
135 | tc, _, _, _ = self._create_trace_class_with_some_stream_classes() | |
136 | with self.assertRaises(KeyError): | |
137 | tc[4] | |
138 | ||
139 | def test_len(self): | |
140 | tc = get_default_trace_class() | |
141 | self.assertEqual(len(tc), 0) | |
142 | tc.create_stream_class() | |
143 | self.assertEqual(len(tc), 1) | |
144 | ||
145 | def test_iter(self): | |
146 | tc, sc1, sc2, sc3 = self._create_trace_class_with_some_stream_classes() | |
147 | ||
148 | for sc_id, stream_class in tc.items(): | |
0bee8ea9 SM |
149 | if sc_id == 12: |
150 | self.assertEqual(stream_class.addr, sc1.addr) | |
151 | elif sc_id == 54: | |
152 | self.assertEqual(stream_class.addr, sc2.addr) | |
153 | elif sc_id == 2018: | |
154 | self.assertEqual(stream_class.addr, sc3.addr) | |
155 | ||
156 | def test_destruction_listener(self): | |
157 | def on_trace_class_destruction(trace_class): | |
158 | nonlocal trace_class_destroyed | |
159 | trace_class_destroyed = True | |
160 | ||
161 | trace_class_destroyed = False | |
162 | ||
163 | trace_class = get_default_trace_class() | |
164 | trace_class.add_destruction_listener(on_trace_class_destruction) | |
165 | ||
166 | self.assertFalse(trace_class_destroyed) | |
167 | ||
168 | del trace_class | |
169 | ||
170 | self.assertTrue(trace_class_destroyed) |