8d9a7a7134c084b60c1625f3e0478fa96bcdd793
[babeltrace.git] / tests / lib / writer / test_ctf_writer_no_packet_context.py.in
1 #!/usr/bin/env python3
2 #
3 # The MIT License (MIT)
4 #
5 # Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 # SOFTWARE.
24
25 import bt_python_helper
26 import tempfile
27 import babeltrace.writer as btw
28 import babeltrace.reader as btr
29 import shutil
30 import uuid
31
32 TEST_COUNT = 4
33 EXPECTED_EVENT_COUNT = 10
34 trace_path = tempfile.mkdtemp()
35
36
37 def print_test_result(test_number, result, description):
38 result_string = 'ok' if result else 'not ok'
39 result_string += ' {} - {}'.format(test_number, description)
40 print(result_string)
41
42 def create_trace(path):
43 trace = btw.Writer(path)
44 clock = btw.Clock('test_clock')
45 trace.add_clock(clock)
46
47 integer_field_type = btw.IntegerFieldDeclaration(32)
48
49 event_class = btw.EventClass('simple_event')
50 event_class.add_field(integer_field_type, 'int_field')
51
52 stream_class = btw.StreamClass('test_stream')
53 stream_class.add_event_class(event_class)
54 stream_class.clock = clock
55
56 stream_class.packet_context_type = None
57 print_test_result(1, stream_class.packet_context_type is None,
58 'Set stream class packet context type to None')
59
60 stream = trace.create_stream(stream_class)
61
62 for i in range(EXPECTED_EVENT_COUNT):
63 event = btw.Event(event_class)
64 event.payload('int_field').value = i
65 stream.append_event(event)
66 stream.flush()
67 print_test_result(2, True,
68 'Flush a packet')
69
70 # It is not valid for a stream to contain more than one packet
71 # if it does not have content_size/packet_size info in its packet
72 # context
73 event = btw.Event(event_class)
74 event.payload('int_field').value = 42
75 stream.append_event(event)
76 try:
77 stream.flush()
78 second_flush_succeeded = True
79 except ValueError:
80 second_flush_succeeded = False
81
82 print_test_result(3, not second_flush_succeeded,
83 'Flushing a second packet in a stream without a defined packet context fails')
84
85
86 # TAP plan
87 print('1..{}'.format(TEST_COUNT))
88 print('# Creating trace at {}'.format(trace_path))
89 create_trace(trace_path)
90
91 traces = btr.TraceCollection()
92 trace_handle = traces.add_trace(trace_path, 'ctf')
93 if trace_handle is None:
94 print('Failed to open trace at {}'.format(trace_path))
95
96 event_count = 0
97 for event in traces.events:
98 event_count += 1
99
100 print_test_result(4, EXPECTED_EVENT_COUNT == event_count,
101 'Trace was found to contain {} events, expected {}'.format(
102 event_count, EXPECTED_EVENT_COUNT))
103
104 shutil.rmtree(trace_path)
This page took 0.034762 seconds and 3 git commands to generate.