2 # Copyright (C) 2019 EfficiOS Inc.
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
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.
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.
20 from utils
import get_default_trace_class
23 class TraceTestCase(unittest
.TestCase
):
25 self
._tc
= get_default_trace_class()
27 def test_create_default(self
):
29 self
.assertEqual(trace
.name
, None)
31 def test_create_full(self
):
32 trace
= self
._tc
(name
='my name')
33 self
.assertEqual(trace
.name
, 'my name')
35 def test_create_invalid_name(self
):
36 with self
.assertRaises(TypeError):
39 def test_attr_trace_class(self
):
40 trace
= self
._tc
(name
='my name')
41 self
.assertEqual(trace
.cls
.addr
, self
._tc
.addr
)
45 sc
= self
._tc
.create_stream_class()
46 self
.assertEqual(len(trace
), 0)
48 trace
.create_stream(sc
)
49 self
.assertEqual(len(trace
), 1)
51 def _create_trace_with_some_streams(self
):
52 sc
= self
._tc
.create_stream_class(assigns_automatic_stream_id
=False)
54 trace
.create_stream(sc
, id=12)
55 trace
.create_stream(sc
, id=15)
56 trace
.create_stream(sc
, id=17)
61 trace
= self
._create
_trace
_with
_some
_streams
()
62 stream_ids
= set(trace
)
63 self
.assertEqual(stream_ids
, {12, 15, 17})
65 def test_getitem(self
):
66 trace
= self
._create
_trace
_with
_some
_streams
()
68 self
.assertEqual(trace
[12].id, 12)
70 def test_getitem_invalid_key(self
):
71 trace
= self
._create
_trace
_with
_some
_streams
()
72 with self
.assertRaises(KeyError):
75 def test_destruction_listener(self
):
76 def on_trace_class_destruction(trace_class
):
77 nonlocal trace_class_destroyed
78 trace_class_destroyed
= True
80 def on_trace_destruction(trace
):
81 nonlocal trace_destroyed
82 trace_destroyed
= True
84 trace_destroyed
= False
85 trace_class_destroyed
= False
87 trace_class
= get_default_trace_class()
88 stream_class
= trace_class
.create_stream_class()
90 stream
= trace
.create_stream(stream_class
)
92 trace_class
.add_destruction_listener(on_trace_class_destruction
)
93 trace
.add_destruction_listener(on_trace_destruction
)
95 self
.assertFalse(trace_class_destroyed
)
96 self
.assertFalse(trace_destroyed
)
100 self
.assertFalse(trace_class_destroyed
)
101 self
.assertFalse(trace_destroyed
)
105 self
.assertFalse(trace_class_destroyed
)
106 self
.assertTrue(trace_destroyed
)
110 self
.assertFalse(trace_class_destroyed
)
111 self
.assertTrue(trace_destroyed
)
115 self
.assertTrue(trace_class_destroyed
)
116 self
.assertTrue(trace_destroyed
)