4 from utils
import run_in_component_init
, get_default_trace_class
7 class TraceClassTestCase(unittest
.TestCase
):
9 def test_create_default(self
):
11 return comp_self
._create
_trace
_class
()
13 tc
= run_in_component_init(f
)
15 self
.assertEqual(len(tc
), 0)
16 self
.assertEqual(len(tc
.env
), 0)
17 self
.assertIsNone(tc
.uuid
)
18 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
22 return comp_self
._create
_trace
_class
(uuid
=uuid
.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
24 tc
= run_in_component_init(f
)
26 self
.assertEqual(tc
.uuid
, uuid
.UUID('da7d6b6f-3108-4706-89bd-ab554732611b'))
28 def test_automatic_stream_class_id(self
):
30 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
32 tc
= run_in_component_init(f
)
33 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
35 # This should not throw.
36 sc1
= tc
.create_stream_class()
37 sc2
= tc
.create_stream_class()
39 self
.assertNotEqual(sc1
.id, sc2
.id)
41 def test_automatic_stream_class_id_raises(self
):
43 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=True)
45 tc
= run_in_component_init(f
)
46 self
.assertTrue(tc
.assigns_automatic_stream_class_id
)
48 with self
.assertRaises(ValueError):
49 sc1
= tc
.create_stream_class(23)
51 def test_no_assigns_automatic_stream_class_id(self
):
53 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
55 tc
= run_in_component_init(f
)
56 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
58 sc
= tc
.create_stream_class(id=28)
59 self
.assertEqual(sc
.id, 28)
61 def test_no_assigns_automatic_stream_class_id_raises(self
):
63 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
65 tc
= run_in_component_init(f
)
66 self
.assertFalse(tc
.assigns_automatic_stream_class_id
)
68 # In this mode, it is required to pass an explicit id.
69 with self
.assertRaises(ValueError):
70 tc
.create_stream_class()
72 def test_env_get(self
):
74 return comp_self
._create
_trace
_class
(env
={'hello': 'you', 'foo': -5})
76 tc
= run_in_component_init(f
)
78 self
.assertEqual(tc
.env
['hello'], 'you')
79 self
.assertEqual(tc
.env
['foo'], -5)
81 def test_env_get_non_existent(self
):
83 return comp_self
._create
_trace
_class
(env
={'hello': 'you', 'foo': -5})
85 tc
= run_in_component_init(f
)
87 with self
.assertRaises(KeyError):
91 def _create_trace_class_with_some_stream_classes():
93 return comp_self
._create
_trace
_class
(assigns_automatic_stream_class_id
=False)
95 tc
= run_in_component_init(f
)
96 sc1
= tc
.create_stream_class(id=12)
97 sc2
= tc
.create_stream_class(id=54)
98 sc3
= tc
.create_stream_class(id=2018)
99 return tc
, sc1
, sc2
, sc3
101 def test_getitem(self
):
102 tc
, _
, _
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
103 self
.assertEqual(tc
[2018].addr
, sc3
.addr
)
105 def test_getitem_wrong_key_type(self
):
106 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
107 with self
.assertRaises(TypeError):
110 def test_getitem_wrong_key(self
):
111 tc
, _
, _
, _
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
112 with self
.assertRaises(KeyError):
116 tc
= get_default_trace_class()
117 self
.assertEqual(len(tc
), 0)
118 tc
.create_stream_class()
119 self
.assertEqual(len(tc
), 1)
122 tc
, sc1
, sc2
, sc3
= self
._create
_trace
_class
_with
_some
_stream
_classes
()
124 for sc_id
, stream_class
in tc
.items():
125 self
.assertIsInstance(stream_class
, bt2
.stream_class
._StreamClass
)
128 self
.assertEqual(stream_class
.addr
, sc1
.addr
)
130 self
.assertEqual(stream_class
.addr
, sc2
.addr
)
132 self
.assertEqual(stream_class
.addr
, sc3
.addr
)
134 def test_destruction_listener(self
):
135 def on_trace_class_destruction(trace_class
):
136 nonlocal trace_class_destroyed
137 trace_class_destroyed
= True
139 trace_class_destroyed
= False
141 trace_class
= get_default_trace_class()
142 trace_class
.add_destruction_listener(on_trace_class_destruction
)
144 self
.assertFalse(trace_class_destroyed
)
148 self
.assertTrue(trace_class_destroyed
)