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