goto error;
}
+ graph->can_consume = BT_TRUE;
ret = init_listeners_array(&graph->listeners.port_added);
if (ret) {
BT_LOGE_STR("Cannot create the \"port added\" listener array.");
struct bt_component *upstream_component = NULL;
struct bt_component *downstream_component = NULL;
enum bt_component_status component_status;
+ const bt_bool init_can_consume = graph->can_consume;
if (!graph) {
BT_LOGW_STR("Invalid parameter: graph is NULL.");
goto end;
}
+ graph->can_consume = BT_FALSE;
+
/* Ensure appropriate types for upstream and downstream ports. */
if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
bt_put(upstream_component);
bt_put(downstream_component);
bt_put(connection);
+ graph->can_consume = init_can_consume;
return status;
}
goto end;
}
+ if (!graph->can_consume) {
+ BT_LOGW_STR("Cannot consume graph in its current state.");
+ status = BT_GRAPH_STATUS_CANNOT_CONSUME;
+ goto end;
+ }
+
+ graph->can_consume = BT_FALSE;
status = bt_graph_consume_no_check(graph);
+ graph->can_consume = BT_TRUE;
end:
return status;
goto end;
}
+ if (!graph->can_consume) {
+ BT_LOGW_STR("Cannot run graph in its current state.");
+ status = BT_GRAPH_STATUS_CANNOT_CONSUME;
+ goto end;
+ }
+
+ graph->can_consume = BT_FALSE;
BT_LOGV("Running graph: addr=%p", graph);
do {
end:
BT_LOGV("Graph ran: status=%s", bt_graph_status_string(status));
+ graph->can_consume = BT_TRUE;
return status;
}
struct bt_component *component = NULL;
enum bt_component_class_type type;
size_t i;
+ const bt_bool init_can_consume = graph->can_consume;
bt_get(params);
goto end;
}
+ graph->can_consume = BT_FALSE;
type = bt_component_class_get_type(component_class);
BT_LOGD("Adding component to graph: "
"graph-addr=%p, comp-cls-addr=%p, "
end:
bt_put(component);
bt_put(params);
+ graph->can_consume = init_can_consume;
return graph_status;
}
conn = self._graph.connect_ports(src.output_ports['out'],
sink.input_ports['in'])
+ def test_connect_ports_cannot_consume_accept(self):
+ class MyIter(bt2._UserNotificationIterator):
+ def __next__(self):
+ raise bt2.Stop
+
+ class MySource(bt2._UserSourceComponent,
+ notification_iterator_class=MyIter):
+ def __init__(self, params):
+ self._add_output_port('out')
+
+ class MySink(bt2._UserSinkComponent):
+ def __init__(self, params):
+ self._add_input_port('in')
+
+ def _consume(self):
+ raise bt2.Stop
+
+ def _accept_port_connection(self, port, other_port):
+ nonlocal exc
+
+ try:
+ self.graph.run()
+ except Exception as e:
+ exc = e
+
+ return True
+
+ exc = None
+ src = self._graph.add_component(MySource, 'src')
+ sink = self._graph.add_component(MySink, 'sink')
+ self._graph.connect_ports(src.output_ports['out'],
+ sink.input_ports['in'])
+ self.assertIs(type(exc), bt2.CannotConsumeGraph)
+
+ def test_connect_ports_cannot_consume_connected(self):
+ class MyIter(bt2._UserNotificationIterator):
+ def __next__(self):
+ raise bt2.Stop
+
+ class MySource(bt2._UserSourceComponent,
+ notification_iterator_class=MyIter):
+ def __init__(self, params):
+ self._add_output_port('out')
+
+ class MySink(bt2._UserSinkComponent):
+ def __init__(self, params):
+ self._add_input_port('in')
+
+ def _consume(self):
+ raise bt2.Stop
+
+ def _port_connected(self, port, other_port):
+ nonlocal exc
+
+ try:
+ self.graph.run()
+ except Exception as e:
+ exc = e
+
+ return True
+
+ exc = None
+ src = self._graph.add_component(MySource, 'src')
+ sink = self._graph.add_component(MySink, 'sink')
+ self._graph.connect_ports(src.output_ports['out'],
+ sink.input_ports['in'])
+ self._graph.run()
+ self.assertIs(type(exc), bt2.CannotConsumeGraph)
+
def test_cancel(self):
self.assertFalse(self._graph.is_canceled)
self._graph.cancel()
with self.assertRaises(bt2.Error):
self._graph.run()
+ def test_run_cannot_consume(self):
+ class MyIter(bt2._UserNotificationIterator):
+ pass
+
+ class MySource(bt2._UserSourceComponent,
+ notification_iterator_class=MyIter):
+ def __init__(self, params):
+ self._add_output_port('out')
+
+ class MySink(bt2._UserSinkComponent):
+ def __init__(self, params):
+ self._add_input_port('in')
+ self._at = 0
+
+ def _consume(comp_self):
+ nonlocal exc
+
+ try:
+ print('going in')
+ comp_self.graph.run()
+ print('going out')
+ except Exception as e:
+ exc = e
+
+ raise bt2.Stop
+
+ exc = None
+ src = self._graph.add_component(MySource, 'src')
+ sink = self._graph.add_component(MySink, 'sink')
+ conn = self._graph.connect_ports(src.output_ports['out'],
+ sink.input_ports['in'])
+ self._graph.run()
+ self.assertIs(type(exc), bt2.CannotConsumeGraph)
+
def test_listeners(self):
class MyIter(bt2._UserNotificationIterator):
def __next__(self):