Commit | Line | Data |
---|---|---|
9cf643d1 PP |
1 | from collections import OrderedDict |
2 | from bt2 import values | |
3 | import unittest | |
4 | import copy | |
5 | import bt2 | |
6 | ||
7 | ||
976c241d | 8 | @unittest.skip("this is broken") |
9cf643d1 PP |
9 | class StreamTestCase(unittest.TestCase): |
10 | def setUp(self): | |
811644b8 | 11 | self._stream = self._create_stream(stream_id=23) |
9cf643d1 | 12 | |
811644b8 PP |
13 | def tearDown(self): |
14 | del self._stream | |
15 | ||
16 | def _create_stream(self, name='my_stream', stream_id=None): | |
9cf643d1 | 17 | # event header |
b4f45851 | 18 | eh = bt2.StructureFieldClass() |
9cf643d1 | 19 | eh += OrderedDict(( |
b4f45851 SM |
20 | ('id', bt2.IntegerFieldClass(8)), |
21 | ('ts', bt2.IntegerFieldClass(32)), | |
9cf643d1 PP |
22 | )) |
23 | ||
24 | # stream event context | |
b4f45851 | 25 | sec = bt2.StructureFieldClass() |
9cf643d1 | 26 | sec += OrderedDict(( |
b4f45851 SM |
27 | ('cpu_id', bt2.IntegerFieldClass(8)), |
28 | ('stuff', bt2.FloatingPointNumberFieldClass()), | |
9cf643d1 PP |
29 | )) |
30 | ||
31 | # packet context | |
b4f45851 | 32 | pc = bt2.StructureFieldClass() |
9cf643d1 | 33 | pc += OrderedDict(( |
b4f45851 SM |
34 | ('something', bt2.IntegerFieldClass(8)), |
35 | ('something_else', bt2.FloatingPointNumberFieldClass()), | |
9cf643d1 PP |
36 | )) |
37 | ||
38 | # stream class | |
39 | sc = bt2.StreamClass() | |
b4f45851 SM |
40 | sc.packet_context_field_class = pc |
41 | sc.event_header_field_class = eh | |
42 | sc.event_context_field_class = sec | |
9cf643d1 PP |
43 | |
44 | # event context | |
b4f45851 | 45 | ec = bt2.StructureFieldClass() |
9cf643d1 | 46 | ec += OrderedDict(( |
b4f45851 SM |
47 | ('ant', bt2.IntegerFieldClass(16, is_signed=True)), |
48 | ('msg', bt2.StringFieldClass()), | |
9cf643d1 PP |
49 | )) |
50 | ||
51 | # event payload | |
b4f45851 | 52 | ep = bt2.StructureFieldClass() |
9cf643d1 | 53 | ep += OrderedDict(( |
b4f45851 SM |
54 | ('giraffe', bt2.IntegerFieldClass(32)), |
55 | ('gnu', bt2.IntegerFieldClass(8)), | |
56 | ('mosquito', bt2.IntegerFieldClass(8)), | |
9cf643d1 PP |
57 | )) |
58 | ||
59 | # event class | |
60 | event_class = bt2.EventClass('ec') | |
b4f45851 SM |
61 | event_class.context_field_class = ec |
62 | event_class.payload_field_class = ep | |
9cf643d1 PP |
63 | sc.add_event_class(event_class) |
64 | ||
65 | # packet header | |
b4f45851 | 66 | ph = bt2.StructureFieldClass() |
9cf643d1 | 67 | ph += OrderedDict(( |
b4f45851 SM |
68 | ('magic', bt2.IntegerFieldClass(32)), |
69 | ('stream_id', bt2.IntegerFieldClass(16)), | |
9cf643d1 PP |
70 | )) |
71 | ||
72 | # trace c;ass | |
73 | tc = bt2.Trace() | |
b4f45851 | 74 | tc.packet_header_field_class = ph |
9cf643d1 PP |
75 | tc.add_stream_class(sc) |
76 | ||
77 | # stream | |
811644b8 | 78 | return sc(name=name, id=stream_id) |
9cf643d1 PP |
79 | |
80 | def test_attr_stream_class(self): | |
81 | self.assertIsNotNone(self._stream.stream_class) | |
82 | ||
83 | def test_attr_name(self): | |
84 | self.assertEqual(self._stream.name, 'my_stream') | |
85 | ||
86 | def test_eq(self): | |
811644b8 PP |
87 | stream1 = self._create_stream(stream_id=17) |
88 | stream2 = self._create_stream(stream_id=17) | |
9cf643d1 PP |
89 | self.assertEqual(stream1, stream2) |
90 | ||
91 | def test_ne_name(self): | |
811644b8 PP |
92 | stream1 = self._create_stream(stream_id=17) |
93 | stream2 = self._create_stream('lel', 17) | |
94 | self.assertNotEqual(stream1, stream2) | |
95 | ||
96 | def test_ne_id(self): | |
97 | stream1 = self._create_stream(stream_id=17) | |
98 | stream2 = self._create_stream(stream_id=23) | |
9cf643d1 PP |
99 | self.assertNotEqual(stream1, stream2) |
100 | ||
101 | def test_eq_invalid(self): | |
102 | self.assertFalse(self._stream == 23) | |
103 | ||
104 | def _test_copy(self, func): | |
105 | stream = self._create_stream() | |
106 | cpy = func(stream) | |
107 | self.assertIsNot(stream, cpy) | |
108 | self.assertNotEqual(stream.addr, cpy.addr) | |
109 | self.assertEqual(stream, cpy) | |
110 | ||
111 | def test_copy(self): | |
112 | self._test_copy(copy.copy) | |
113 | ||
114 | def test_deepcopy(self): | |
115 | self._test_copy(copy.deepcopy) |