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