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 | ||
335a2da5 | 19 | import uuid |
9cf643d1 | 20 | import unittest |
8c2367b8 | 21 | from utils import get_default_trace_class |
9cf643d1 PP |
22 | |
23 | ||
24 | class TraceTestCase(unittest.TestCase): | |
25 | def setUp(self): | |
8c2367b8 | 26 | self._tc = get_default_trace_class() |
9cf643d1 PP |
27 | |
28 | def test_create_default(self): | |
8c2367b8 | 29 | trace = self._tc() |
335a2da5 PP |
30 | self.assertIsNone(trace.name) |
31 | self.assertIsNone(trace.uuid) | |
32 | self.assertEqual(len(trace.env), 0) | |
9cf643d1 | 33 | |
8c2367b8 | 34 | def test_create_invalid_name(self): |
9cf643d1 | 35 | with self.assertRaises(TypeError): |
8c2367b8 | 36 | self._tc(name=17) |
9cf643d1 | 37 | |
05abc762 | 38 | def test_attr_trace_class(self): |
335a2da5 | 39 | trace = self._tc() |
05abc762 PP |
40 | self.assertEqual(trace.cls.addr, self._tc.addr) |
41 | ||
335a2da5 PP |
42 | def test_attr_name(self): |
43 | trace = self._tc(name='mein trace') | |
44 | self.assertEqual(trace.name, 'mein trace') | |
45 | ||
46 | def test_attr_uuid(self): | |
47 | trace = self._tc(uuid=uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b')) | |
48 | self.assertEqual(trace.uuid, uuid.UUID('da7d6b6f-3108-4706-89bd-ab554732611b')) | |
49 | ||
50 | def test_env_get(self): | |
51 | trace = self._tc(env={'hello': 'you', 'foo': -5}) | |
52 | self.assertEqual(trace.env['hello'], 'you') | |
53 | self.assertEqual(trace.env['foo'], -5) | |
54 | ||
55 | def test_env_get_non_existent(self): | |
56 | trace = self._tc(env={'hello': 'you', 'foo': -5}) | |
57 | ||
58 | with self.assertRaises(KeyError): | |
59 | trace.env['lel'] | |
60 | ||
8c2367b8 SM |
61 | def test_len(self): |
62 | trace = self._tc() | |
63 | sc = self._tc.create_stream_class() | |
64 | self.assertEqual(len(trace), 0) | |
9cf643d1 | 65 | |
8c2367b8 SM |
66 | trace.create_stream(sc) |
67 | self.assertEqual(len(trace), 1) | |
9cf643d1 | 68 | |
8c2367b8 SM |
69 | def _create_trace_with_some_streams(self): |
70 | sc = self._tc.create_stream_class(assigns_automatic_stream_id=False) | |
71 | trace = self._tc() | |
72 | trace.create_stream(sc, id=12) | |
73 | trace.create_stream(sc, id=15) | |
74 | trace.create_stream(sc, id=17) | |
9cf643d1 | 75 | |
8c2367b8 | 76 | return trace |
9cf643d1 PP |
77 | |
78 | def test_iter(self): | |
8c2367b8 SM |
79 | trace = self._create_trace_with_some_streams() |
80 | stream_ids = set(trace) | |
81 | self.assertEqual(stream_ids, {12, 15, 17}) | |
9cf643d1 | 82 | |
8c2367b8 SM |
83 | def test_getitem(self): |
84 | trace = self._create_trace_with_some_streams() | |
9cf643d1 | 85 | |
8c2367b8 | 86 | self.assertEqual(trace[12].id, 12) |
9cf643d1 | 87 | |
8c2367b8 SM |
88 | def test_getitem_invalid_key(self): |
89 | trace = self._create_trace_with_some_streams() | |
9cf643d1 | 90 | with self.assertRaises(KeyError): |
8c2367b8 | 91 | trace[18] |
9cf643d1 | 92 | |
8c2367b8 SM |
93 | def test_destruction_listener(self): |
94 | def on_trace_class_destruction(trace_class): | |
95 | nonlocal trace_class_destroyed | |
96 | trace_class_destroyed = True | |
811644b8 | 97 | |
8c2367b8 SM |
98 | def on_trace_destruction(trace): |
99 | nonlocal trace_destroyed | |
100 | trace_destroyed = True | |
811644b8 | 101 | |
8c2367b8 SM |
102 | trace_destroyed = False |
103 | trace_class_destroyed = False | |
811644b8 | 104 | |
8c2367b8 SM |
105 | trace_class = get_default_trace_class() |
106 | stream_class = trace_class.create_stream_class() | |
107 | trace = trace_class() | |
108 | stream = trace.create_stream(stream_class) | |
811644b8 | 109 | |
8c2367b8 SM |
110 | trace_class.add_destruction_listener(on_trace_class_destruction) |
111 | trace.add_destruction_listener(on_trace_destruction) | |
811644b8 | 112 | |
8c2367b8 SM |
113 | self.assertFalse(trace_class_destroyed) |
114 | self.assertFalse(trace_destroyed) | |
9cf643d1 | 115 | |
8c2367b8 | 116 | del trace |
9cf643d1 | 117 | |
8c2367b8 SM |
118 | self.assertFalse(trace_class_destroyed) |
119 | self.assertFalse(trace_destroyed) | |
9cf643d1 | 120 | |
8c2367b8 | 121 | del stream |
9cf643d1 | 122 | |
8c2367b8 SM |
123 | self.assertFalse(trace_class_destroyed) |
124 | self.assertTrue(trace_destroyed) | |
9cf643d1 | 125 | |
8c2367b8 | 126 | del trace_class |
9cf643d1 | 127 | |
8c2367b8 SM |
128 | self.assertFalse(trace_class_destroyed) |
129 | self.assertTrue(trace_destroyed) | |
9cf643d1 | 130 | |
8c2367b8 | 131 | del stream_class |
9cf643d1 | 132 | |
8c2367b8 SM |
133 | self.assertTrue(trace_class_destroyed) |
134 | self.assertTrue(trace_destroyed) |