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