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 import collections
.abc
23 # Run callable `func` in the context of a component's __init__ method. The
24 # callable is passed the Component being instantiated.
26 # The value returned by the callable is returned by run_in_component_init.
27 def run_in_component_init(func
):
28 class MySink(bt2
._UserSinkComponent
):
29 def __init__(self
, params
, obj
):
31 res_bound
= func(self
)
33 def _user_consume(self
):
38 g
.add_component(MySink
, 'comp')
40 # We deliberately use a different variable for returning the result than
41 # the variable bound to the MySink.__init__ context and delete res_bound.
42 # The MySink.__init__ context stays alive until the end of the program, so
43 # if res_bound were to still point to our result, it would contribute an
44 # unexpected reference to the refcount of the result, from the point of view
45 # of the user of this function. It would then affect destruction tests,
46 # for example, which want to test what happens when the refcount of a Python
54 # Create an empty trace class with default values.
55 def get_default_trace_class():
57 return comp_self
._create
_trace
_class
()
59 return run_in_component_init(f
)
62 # Proxy sink component class.
64 # This sink accepts a list of a single item as its initialization
65 # object. This sink creates a single input port `in`. When it consumes
66 # from this port, it puts the returned message in the initialization
67 # list as the first item.
68 class TestProxySink(bt2
._UserSinkComponent
):
69 def __init__(self
, params
, msg_list
):
70 assert msg_list
is not None
71 self
._msg
_list
= msg_list
72 self
._add
_input
_port
('in')
74 def _user_graph_is_configured(self
):
75 self
._msg
_iter
= self
._create
_input
_port
_message
_iterator
(
76 self
._input
_ports
['in']
79 def _user_consume(self
):
80 assert self
._msg
_list
[0] is None
81 self
._msg
_list
[0] = next(self
._msg
_iter
)
84 # This is a helper message iterator for tests.
86 # The constructor accepts a graph and an output port.
88 # Internally, it adds a proxy sink to the graph and connects the
89 # received output port to the proxy sink's input port. Its __next__()
90 # method then uses the proxy sink to transfer the consumed message to
91 # the output port message iterator's user.
93 # This message iterator cannot seek.
94 class TestOutputPortMessageIterator(collections
.abc
.Iterator
):
95 def __init__(self
, graph
, output_port
):
97 self
._msg
_list
= [None]
98 sink
= graph
.add_component(TestProxySink
, 'test-proxy-sink', obj
=self
._msg
_list
)
99 graph
.connect_ports(output_port
, sink
.input_ports
['in'])
102 assert self
._msg
_list
[0] is None
103 self
._graph
.run_once()
104 msg
= self
._msg
_list
[0]
105 assert msg
is not None
106 self
._msg
_list
[0] = None
This page took 0.0332 seconds and 5 git commands to generate.