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